CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Oct 2006
    Posts
    449

    Combobox control

    I use a combobox wherein the user may type his value or select from the dropdownlist. In this schenario, everytime when the user opens the form, I want the combobox control to reset/refresh (I mean no value should be displayed). Now I have my first value selected always. Please help me out. Thnx.

  2. #2
    Join Date
    Oct 2005
    Location
    Islamabad, Pakistan
    Posts
    1,277

    Re: Combobox control

    in form load event u can clear the items in combobox
    Code:
    ComboBox1.Items.Clear
    if u want the comboBox selectIndex reset
    Code:
     ComboBox1.SelectedIndex = -1

  3. #3
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    1,842

    Re: Combobox control

    This will just clear the box

    Code:
    Combo1.Text = ""

  4. #4
    Join Date
    Oct 2006
    Posts
    449

    Re: Combobox control

    It's a Databound control. Then how do I go about?

  5. #5
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    1,842

    Re: Combobox control

    The the Form_Load event you will have something like the following code -

    Code:
            Me.MyTableTableAdapter.Fill(Me.MyDatabaseDataSet.MyTable)
    Insert the instruction immediately after this, so you get -

    Code:
            Me.MyTableTableAdapter.Fill(Me.MyDatabaseDataSet.MyTable)
             ComboBox1.Text = ""

  6. #6
    Join Date
    Oct 2006
    Posts
    449

    Re: Combobox control

    Am still not getting it. Am I doing anything wrong with the control's property?

  7. #7
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    1,842

    Re: Combobox control

    The only 2 properties I touched in the ComboBox were -

    DataSource eg, MyDatabase.mdb - MyTable
    DisplayMember - MyFieldName

  8. #8
    Join Date
    Oct 2006
    Posts
    449

    Re: Combobox control

    Am posting my sample code below. Please check that out.

    -----------------------------------------------------------------------------------------------
    Public Class Form2

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Me.Close()
    End Sub

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.MyTableTableAdapter.Fill(Me.MyDatabaseDataSet.MyTable)
    ComboBox.Text = ""
    End Sub

    Private Sub ComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox.SelectedIndexChanged
    'am doing some actions here.....

    End Sub

    End Class
    -----------------------------------------------------------------------------------------------

  9. #9
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    1,842

    Re: Combobox control

    I would comment out everything except the Form Load event and see how that effects the result

    Just leave this as an active event

    Code:
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.MyTableTableAdapter.Fill(Me.MyDatabaseDataSet.MyTable)
    ComboBox.Text = ""
    End Sub

  10. #10
    Join Date
    Oct 2006
    Posts
    449

    Re: Combobox control

    It works fine. But after I type some value close the form and then reopen the form then the first value from the dropdownlist is getting displayed. Actually I want this control to be blank whenever I close and open the form. Hope am clear.

  11. #11
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    1,842

    Re: Combobox control

    Try shifting the instructions to the

    Form1_Activated event


    Code:
    Private Sub Form2_Activated (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.MyTableTableAdapter.Fill(Me.MyDatabaseDataSet.MyTable)
    ComboBox.Text = ""
    End Sub

    By doing this it should load and clear the combobox each time the form is activated (rather than just once - only when it is loaded the first time)

  12. #12
    Join Date
    Oct 2006
    Posts
    449

    Re: Combobox control

    No luck! It still displays the first value from the dropdownlist. Is it because of the actions that am doing inside the 'ComboBox_SelectedIndexChanged'. I have given below the complete and final code. I kindly request you to check out. Thank you.

    ************************************************************
    Public Class Form2

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    'This is Cancel button
    Me.Close()
    End Sub


    Private Sub Form2_Activated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.MyTableTableAdapter.Fill(Me.MyDatabaseDataSet.MyTable)
    ComboBox.Text = ""
    End Sub


    Private Sub ComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox.SelectedIndexChanged
    If (ComboBox.Text = "xxxx xxxx xxxx xxxx xxxx") Then
    ' .... am doing some actions here.....
    End If

    End Sub

    End Class

  13. #13
    Join Date
    Oct 2005
    Location
    Islamabad, Pakistan
    Posts
    1,277

    Re: Combobox control

    Use selected index property as combobox style is dropdownlist.
    Code:
    Public Class Form1
    
        Dim conn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\attendence.mdb;Persist Security Info=False")
        Dim dt As New DataTable
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Dim adapter As New OleDb.OleDbDataAdapter("SELECT * FROM student", conn)
    
            conn.Open()
            adapter.Fill(dt)
            dt.TableName = "Student"
            conn.Close()
    
            ComboBox1.DataSource = dt
            ComboBox1.DisplayMember = "rno"
    
            ComboBox1.SelectedIndex = -1
    
        End Sub
    
    End Class

  14. #14
    Join Date
    Oct 2006
    Posts
    449

    Re: Combobox control

    No luck!!

  15. #15
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    1,842

    Re: Combobox control

    Why do you need ?


    Code:
    Private Sub ComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox.SelectedIndexChanged
    If (ComboBox.Text = "xxxx xxxx xxxx xxxx xxxx") Then
    ' .... am doing some actions here.....
    End If
    You tell me that if this event is not in the code then everything is fine -
    So .... why do you need THIS event - I'm sure there are other ways to do what you are doing in THIS event


    Also, you may like to explain what you are trying to achieve overall - there may be a better way to deal with your problem.

    The fact you are having big problems with a rather basic control (ComboBox) indicates that you just may be trying to do something it was not designed to do

+ Reply to Thread
Page 1 of 2 1 2 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts



HTML5 Development Center

Click Here to Expand Forum to Full Width