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