ComboBox.SelectedIndex bug?

  • Thread starter Thread starter Strahimir Antoljak
  • Start date Start date
S

Strahimir Antoljak

Has anyone experienced problems with
a combo box SelectIndex property?
Is there a limit to the number of Items
for a combo box?

Namely, when I set programmatically
ComboBox.SelectIndex property
with 2000 Items listed in the ComboBox,
SelectIndex property accepts assigned
value. However, when I try to use SelectIndex
property with 5000 Items populating
the ComboBox, the SelectIndex property
does not pick up the programmatically
assigned value?

This is amazingly annoying feature/bug(?).
Anyone knows any kind of a workaround/fix?

Thanks,
 
Hi Strahmir,

I did made this code and it did fire

I hope this helps?

Cor
\\\
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim statesTable As New DataTable
statesTable.Columns.Add(New DataColumn("Name"))
Dim dr As DataRow
For i As Integer = 0 To 4999
dr = statesTable.NewRow()
dr(0) = i
statesTable.Rows.Add(dr)
Next
ComboBox1.DataSource = statesTable.DefaultView
ComboBox1.DisplayMember = "Name"
flag = True
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender _
As Object, ByVal e As System.EventArgs) Handles
ComboBox1.SelectedIndexChanged
If flag = True Then
MessageBox.Show("fired")
End If
End Sub
///
 
Thanks Cor for your effort and help.
Something strange is happening to ComboBox
and I my question was not probably put right.

By additionally playing around with my code
I figured, that after all, it is not a number of
Items in the ComboBox, but something in
relation with ComboBox 'default' select index
if there is such thing. Here is what I mean;
I pull 5000+ Items from the database and
populate a ComboBox. I want to utilize
the ComboBox_KeyPress - see code below.

There was much more code in the ComboBox_KeyPress
event but I reduced it to what troubles me.
So what happens is that, I populate the Combobox,
and when I start using keyboard, I would like to set
SelectedIndex to a certain value.

With the code below, the behavior is really strange.
Fore example, if I start typing 3, and there is an Item
starting with 3 it will select that Item instead of
the Item with index = 3. Why is that? On the other
hand, if I start with 7, and there is no Item starting
with 7, it will select Item with index = 3 (?). Why?
And finally, Item with index = 3, starts with 1, and
if I start typing 1, it does not select Item with index =3
but rather Item with index = 4. I find this behavior
very strange. Should I not be able to set
ComboBox.SelectedIndex in the ComboBox_KeyPress
event? Any reason for that? Why my index, in the
ComboBox.SelectedIndex gets overwritten?
What am I missing?

Thanks



Private dbconn As OleDb.OleDbConnection

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.ComboBox1.DataSource = GetJobNumbers()
AddHandler ComboBox1.KeyPress, AddressOf ComboBox_KeyPress
End Sub


Private Function GetJobNumbers() As System.Collections.ArrayList
Dim al As New ArrayList()
Dim drJobNumbers As OleDb.OleDbDataReader
Try
' Get connection
Dim strConnectionString As String =
"Provider=SQLOLEDB;Driver={SQL Server}; Server=ServName;
Database=dbname;UID=username;PWD=password"
dbconn = New OleDb.OleDbConnection(strConnectionString)

Dim sGetJobNumbers As String = "SELECT * FROM PR WHERE prTask =
'' AND prStatus = 'A' ORDER BY prProject ASC"
Dim cmd As OleDb.OleDbCommand = New
OleDb.OleDbCommand(sGetJobNumbers, dbconn)
dbconn.Open()
drJobNumbers = cmd.ExecuteReader
Dim sEvalJobNumber As String
Dim iEvalJobNumber As Integer
Do While drJobNumbers.Read
sEvalJobNumber = drJobNumbers.Item(0)
If Microsoft.VisualBasic.Len(sEvalJobNumber) = 7 And _
Microsoft.VisualBasic.IsNumeric(sEvalJobNumber) Then
iEvalJobNumber = CType(sEvalJobNumber, Integer)
If iEvalJobNumber > 1000123 Then
al.Add(drJobNumbers.Item(0).ToString)
End If
End If
Loop
Catch ex As Exception
Debug.WriteLine(ex.Message)
Finally
drJobNumbers.Close()
dbconn.Close()
End Try
Return al
End Function


Private Sub ComboBox_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs)
Dim cbx As ComboBox = CType(sender, ComboBox)
cbx.SelectedIndex = 3
End Sub
 
Cor,

thanks again. Just having you said "But I have seen many strange things
with the combobox", makes me feel better, knowing I am not the only one.
I am on the edge of frustration and has found some workaround, but it
should not be that difficult to deal with. Thanks,

Strah
 
Back
Top