Problem with ComboBox

G

Guest

Hello

I’m experience a problem with a combo at VB .NET.

I’m developing a window application and I’m using a combobox.

I’m populating the combo with a dataset. I set its ValueMenber and
DisplayMenber, and the selectedindex to -1. The dropdownstyle is set to
DropDown, but the problem persists whit DropDownList.
When I click in the button, it shows a message box whit theValueMenber and
DisplayMenber. Them I set again the selectindex to -1. The problem is that
the selectedIndex that the combo assume is 0, therefore it show its first
value.

This is the code that I use.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim objset As New DataSet
Dim str1 As String
objset = SP_Combo(strcon)
'cb1.DataSource = objset.Tables("Destinos")
cb1.DataSource = objset.Tables("Empregados")
'cb1.DisplayMember = "nome"
cb1.DisplayMember = "FirstName"
cb1.SelectedIndex = -1
Match = -1
objset.Dispose()
objset = Nothing


End Sub

Public Function SP_Combo(ByVal strConn As String) As DataSet
Dim a As String
Dim objComm As New SqlCommand
Dim objDataSet As New DataSet
Dim ObjdataAd As New SqlDataAdapter
Dim objConn As New SqlConnection(strConn)
objConn.Open()
With objComm
.Connection = objConn
.CommandType = CommandType.Text
.CommandText = "Select EmployeeID, FirstName from Employees
order by FirstName"
'.CommandType = CommandType.StoredProcedure
'.CommandText = "SpNet_DestRecp"
'.Parameters.Add(New SqlParameter("@Var_Onde",
"Combo")).Direction = ParameterDirection.Input
End With
ObjdataAd.SelectCommand = objComm
'ObjdataAd.Fill(objDataSet, "Destinos")
ObjdataAd.Fill(objDataSet, "Empregados")
objComm.Dispose()
objComm = Nothing
ObjdataAd.Dispose()
ObjdataAd = Nothing
objConn.Close()
objConn.Dispose()
objConn = Nothing
Return objDataSet
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If Match <> -1 And cb1.SelectedIndex = -1 Then
cb1.SelectedIndex = Match
Match = -1
End If


If cb1.SelectedIndex <> -1 Then
MsgBox("Numero: " & cb1.SelectedValue & " com o nome " &
cb1.GetItemText(cb1.SelectedItem))
MsgBox("Indice: " & cb1.SelectedIndex)
End If

cb1.SelectedIndex = -1

End Sub

To resolve this issue I populate the combo again. But this makes the
application slower.

There’s anyone who knows to resolve this little problem?

Thanks to all.

Ana Rita
 
H

Herfried K. Wagner [MVP]

* "=?Utf-8?B?QW5hIFJpdGE=?= said:
I’m populating the combo with a dataset. I set its ValueMenber and
DisplayMenber, and the selectedindex to -1. The dropdownstyle is set to
DropDown, but the problem persists whit DropDownList.
When I click in the button, it shows a message box whit theValueMenber and
DisplayMenber. Them I set again the selectindex to -1. The problem is that
the selectedIndex that the combo assume is 0, therefore it show its first
value.

That's a known bug:

BUG: ComboBox Does Not Clear When You Set SelectedIndex to -1
<URL:http://support.microsoft.com/?scid=kb;EN-US;327244>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top