I am using a form with a combobox which is populated from an SQL database table. This works OK but is it possible to leave the box blank on the form until a value is selected.By default the box has the content of the first record.
Printable View
I am using a form with a combobox which is populated from an SQL database table. This works OK but is it possible to leave the box blank on the form until a value is selected.By default the box has the content of the first record.
yes it is possible , provided your combobox dropdownstyle property is set to dropdown or simple
if it is set to dropdownlist it is not possible
vb Code:
With Me.ComboBox1 .DropDownStyle = ComboBoxStyle.DropDown .Text = "" End With
'CRAP'
Not the answer I wanted to see but it happens.
At least I now know how to do it but I also wanted to prevent the end user from making any changes so I had a dropdownlist. Thanks for your reply.:(
combobox dropdownlist property is not the ultimate to restrict the users from entering their own choice
use this function in combobox lostfocus event, which will tell whether the user has chosen the corrent value or else entered his own text
vb Code:
Private Function LocokComboForEdit() As Boolean 'locks the combobox for edit If Me.ComboBox1.Text = "" Then Exit Function Dim Sel_Item_index As Integer = Me.ComboBox1.SelectedIndex If Sel_Item_index < 0 Then MessageBox.Show("Invalid entry ") Else MessageBox.Show("DCorrect entry") End If End Function
use the return values of this function as per your connivance.
Hi.
You can also try this other method while using the dropdownstyle as dropdownlist.
1. At the form load, query your database for the values you want to fill the combobox with, and fill it through a loop
2. Before you fill the combobox with the values, add a blank one i.e. ( combobox1.items.add("") )
3. After a user selects a value from the combobox, delete the first one, which will be the blank one, something like this :
VB Code:
private sub combobox1_selectedindexchanged() if combobox1.items(0) = "" then combobox1.items.removeat(0) end if end sub
That way the combobox will have a blank value untill the user selects one, while restricting the user from inputting his own values.