2 Attachment(s)
[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.
Attachment 157939
Attachment 157941
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
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/
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.