[RESOLVED] ComboBox loses its value when clicked???
I have a form that has several comboboxes on them. They are loaded with a single column from a table by a SQL Query. Problem is, when I click on the ComboBox and select a value....it goes blank and the ComboBox itself clears out. Why does this happen?
Thanks,
Re: ComboBox loses its value when clicked???
Set the valuemember and displaymember in the Properties Window. Usually the valuemember will a unique value, an ID of some sort. Display member is whatever you want it to be. To do this you must have them bound to a dataset in the IDE. If not then you must manually do it in the code.
Re: ComboBox loses its value when clicked???
Why doesn't it work like VB6. Does it always clear out like that??? And since it's loaded with table data, how do I make the default value the 1st value?
Re: ComboBox loses its value when clicked???
What exactly are you trying to do? Use the Query to populate the Item Collection on the Combo Box?
Re: ComboBox loses its value when clicked???
Here's my query:
VB Code:
Private Sub LoadTerminalComboBox()
Try
Dim iRow As DataRow
Dim tmpTerminal As String = ""
strProcedure = "LoadTerminalComboBox"
cmbTerminal.Items.Clear()
For Each iRow In dtBatchHeader.Rows
If iRow.Item("terminal") = Convert.ToString(tmpTerminal) Then
Else
cmbTerminal.Items.Add(iRow("terminal"))
tmpTerminal = iRow("terminal")
End If
Next
Catch ex As Exception
ErrorHandler(strProgram, strModule, strProcedure, Msg)
End Try
End Sub
All I'm doing is loading a combobox from a Datatable.
Re: ComboBox loses its value when clicked???
VB Code:
Private Sub LoadTerminalComboBox()
Try
Dim iRow As DataRow
Dim tmpTerminal As String = ""
strProcedure = "LoadTerminalComboBox"
cmbTerminal.Items.Clear()
For Each iRow In dtBatchHeader.Rows
If iRow.Item("terminal") = Convert.ToString(tmpTerminal) Then
Else
cmbTerminal.Items.Add(iRow("terminal"))
tmpTerminal = iRow("terminal")
End If
Next
Catch ex As Exception
ErrorHandler(strProgram, strModule, strProcedure, Msg)
End Try
End Sub
Where are you calling this from? And I am not sure that I follow the logic statement your using.
VB Code:
if iRow.items("Terminal") = Convert.ToString(tmpTerminal) then
Is that like??????
VB Code:
if not iRow.Items("Terminal").ToString = String.Empty
Plus you clear the box everytime you clear this function. Even if it can not be repopulated.
Re: ComboBox loses its value when clicked???
All this procedure does is read data from a DataTable. It checks each value of "terminal" and compares it to an empty variable tmpTerminal. If they match bypass that record and move the "terminal" into the tmpTerminal variable and compare another record.
As far as cmbTerminal.Items.Clear....I'm assuming it clears out the ComboBox and reloads it via the For Next Loop.
Re: ComboBox loses its value when clicked???
Yes it should be cleared then reloaded. Unless nothing pass the IF Statement. But that is neither here nor there. Do you have any code on the combobox itself?