Results 1 to 4 of 4

Thread: [RESOLVED] Combobox display properties

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Resolved [RESOLVED] Combobox display properties

    I have a combobox that needs to display the text, "Department", when displayed on a form. The combobox is data bound and displays the values from a lookup table in the dataset. I have set the following properties for the combobox, in the code, as follows:

    Code:
                    .cmbDepartment.DataSource = subPickList.lkpDepartmentBindingSource
                    .cmbDepartment.DisplayMember = "chrDepartment"
                    .cmbDepartment.ValueMember = "chrDepartment"
                    .cmbDepartment.Text = "Department"
    The combobox works as required, but the text of the combobox displays the first record value instead of the text, "Department". Department is displayed the first time the form is called, but not after that.

    Name:  Combobox1.jpg
Views: 368
Size:  6.8 KB

    Name:  combobox2.jpg
Views: 442
Size:  6.8 KB

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Combobox display properties

    I just found a simple way...

    Code:
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ComboBox1.DataSource = New String() {"one", "two", "three"}
            'change text
            ComboBox1.SelectedIndex = -1
        End Sub
    
        Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
            If ComboBox1.SelectedIndex = -1 Then
                'if nothing selected, display "Department"
                ComboBox1.Text = "Department"
            End If
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'change text
            ComboBox1.SelectedIndex = -1
        End Sub
    
    End Class

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Combobox display properties

    Is the DropDownStyle set to DropDown or DropDownList? If it's DropDown then you should be able to display text that isn't in the drop-down list but that also means that the user can type anything they want into the control. If it's DropDownList then the control can only display text from the property/column of the DataSource whose name is assigned to the DisplayMember.

    Another option is to use a watermark, although this might only be possible with a ComboBox if the DropDownStyle is DropDown too.

    http://www.aaronlerch.com/blog/2007/...edit-controls/

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Re: Combobox display properties

    JM, the property is set to DropDown. I am not at all familiar with a watermark.

    Paul, setting the .SelectedIndex then setting the text property value works. Thanks.

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width