How to check if user doesn't select anything in combobox.
-
The combobox I populated from DB. WHen I add, update DB, this combobox is one field but if user doesn't select anything. The insert/update sql will not correct. So how to detect this situation then warning the user.
-
If nothing is selected, the
SelectedIndex
property of theComboBox
instance will equal -1.If (combo.SelectedIndex = -1)
'' Nothing selected
End If"I'm neither for nor against, on the contrary." John Middle
I have tried. If no selected (user does not do anything with the combobox) selected.index=0 the same when user select the first row. I use Visual Studio 2013 and populate combobox from database using datatable like this
Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmd)
Dim myDataSet As DataSet = New DataSet()
myDA.Fill(myDataSet, "MyTable")
Cbx_PhoneType.DataSource = myDataSet.Tables("MyTable").DefaultView
Cbx_PhoneType.ValueMember = "ID"
Cbx_PhoneType.DisplayMember = "Model Name"
cmd.Dispose()
con.Close() -
I have tried. If no selected (user does not do anything with the combobox) selected.index=0 the same when user select the first row. I use Visual Studio 2013 and populate combobox from database using datatable like this
Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmd)
Dim myDataSet As DataSet = New DataSet()
myDA.Fill(myDataSet, "MyTable")
Cbx_PhoneType.DataSource = myDataSet.Tables("MyTable").DefaultView
Cbx_PhoneType.ValueMember = "ID"
Cbx_PhoneType.DisplayMember = "Model Name"
cmd.Dispose()
con.Close()You could do the Following : After populating the Combobox with new data you set an additional boolean Variable (ComboBoxHelper) to false. If the Combobox is Clicked or SelectedIndexChanged you set this Variable to True. Now you know, that the Combobox was used by the Operator ...
-
You could do the Following : After populating the Combobox with new data you set an additional boolean Variable (ComboBoxHelper) to false. If the Combobox is Clicked or SelectedIndexChanged you set this Variable to True. Now you know, that the Combobox was used by the Operator ...
-
No ... sorry ... but you could reply here and also we could discuss anything ...
-
No ... sorry ... but you could reply here and also we could discuss anything ...
-
I have tried your suggestion but I fail. Do you have any OTT application such as Viber, Whatsapp, line...... We can contact easier. Thank for your help.
It either happens here on Code Project or it doesn't happen at all.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
I have tried your suggestion but I fail. Do you have any OTT application such as Viber, Whatsapp, line...... We can contact easier. Thank for your help.
I think you read what Dave has written. My answer is the same - sorry ... Back to your Requirement : You have some ComboBoxes, ListBoxes and/or TextBoxes on your Form. All of them are assigned with some values. Your problem now (as I understood) is that the User has made some inputs but sometimes he forgot (or not realize) to make a selection on the Combobox. By default your Combobox has not SelectedItem= -1 - it has SelectedItem=0 (and this could be a valid selection). So ... what could you do now ? One possibility is to force the user to make a selection or to touch the Combobox. The Suggestion I gave you is to touch the Combobox. But perhaps that doesn't matches to your Requirement (if you coded I in the right way it will work). So we could change something : the content of your Combobox don't have a valid data on Item=0. This could be done if you first insert a "-" as Item and then the correct data. Perhaps you think about that ...
-
The combobox I populated from DB. WHen I add, update DB, this combobox is one field but if user doesn't select anything. The insert/update sql will not correct. So how to detect this situation then warning the user.
Right after you populate the combo box, set the SelectedIndex property to -1. Then it will be -1 if the user hasn't changed it.
The difficult we do right away... ...the impossible takes slightly longer.
-
The combobox I populated from DB. WHen I add, update DB, this combobox is one field but if user doesn't select anything. The insert/update sql will not correct. So how to detect this situation then warning the user.
If by chance you're using Windows Forms, the combobox has an event that fires only when the user makes a selection. It's independent of the SelectedIndexchanged event. Handle the event and get the SelectedIndex property from there. ComboBox.SelectionChangeCommitted[^]
Sometimes the true reward for completing a task is not the money, but instead the satisfaction of a job well done. But it's usually the money.