By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,082 Members | 2,108 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,082 IT Pros & Developers. It's quick & easy.

Compare text in a combobox

P: n/a
Hello,
I'm trying to get te index of a specific text in a ComboBox.
The ComboBox is filled by searching for filenames in a directory.
The text of the last item selected in the ComboBox is saved in a
textfile in the OnExit Event of the Mainform.
While loading the Mainform I want the text that was saved in the file
to be compared with the items in the ComboBox ad get the index of the
found item, so that I can set it as selected.

I thought i could do it something like this, but the program hangs
before I can see anything.
---------
Code
---------
Dim i, As Integer
Dim line As String
For i = 0 To ComboBox1.Items.Count
If ComboBox1.Items(i) = line Then
IndexFound = i
Exit For
End If
Next
---------
Code
---------

Can anyone help me please,

Thanx
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
I'll bet the program does not hang
- it should come up with a ruddy great Error Message

For i = 0 To ComboBox1.Items.Count - 1

Note the minus one
- Zero Based arrays are a PITA
On 10 May 2004 03:50:30 -0700, ni****@hollandridderkerk.nl (Niels)
wrote:
Hello,
I'm trying to get te index of a specific text in a ComboBox.
The ComboBox is filled by searching for filenames in a directory.
The text of the last item selected in the ComboBox is saved in a
textfile in the OnExit Event of the Mainform.
While loading the Mainform I want the text that was saved in the file
to be compared with the items in the ComboBox ad get the index of the
found item, so that I can set it as selected.

I thought i could do it something like this, but the program hangs
before I can see anything.
---------
Code
---------
Dim i, As Integer
Dim line As String
For i = 0 To ComboBox1.Items.Count
If ComboBox1.Items(i) = line Then
IndexFound = i
Exit For
End If
Next
---------
Code
---------

Can anyone help me please,

Thanx


Jul 17 '05 #2

P: n/a
The easiest way, if there can be only one match, is by using the
CB_FINDSTRING or CB_FINDSTRINGEXACT messages ...

sItemToFind = "Hello World"

matchIndex = SendMessage(combo1.hwnd, CB_FINDSTRINGEXACT, -1, ByVal
sItemToFind)

if matchIndex >-1 then msgbox "the item is index " & matchIndex
CB_FINDSTRINGEXACT (or LB_* for list boxes) does just that ... if the combo
entry was "Hello World here I come" CB_FINDSTRINGEXACT would fail above.
CB_FINDSTRING on the other hand would locate the item. Note however if the
item was "I'd like to say Hello World", neither messages would find the item
since the item does not begin with the words "Hello World".
--

Randy Birch
MVP Visual Basic
http://vbnet.mvps.org/
Please respond only to the newsgroups so all can benefit.
"Niels" <ni****@hollandridderkerk.nl> wrote in message
news:b1*************************@posting.google.co m...
: Hello,
: I'm trying to get te index of a specific text in a ComboBox.
: The ComboBox is filled by searching for filenames in a directory.
: The text of the last item selected in the ComboBox is saved in a
: textfile in the OnExit Event of the Mainform.
: While loading the Mainform I want the text that was saved in the file
: to be compared with the items in the ComboBox ad get the index of the
: found item, so that I can set it as selected.
:
: I thought i could do it something like this, but the program hangs
: before I can see anything.
: ---------
: Code
: ---------
: Dim i, As Integer
: Dim line As String
: For i = 0 To ComboBox1.Items.Count
: If ComboBox1.Items(i) = line Then
: IndexFound = i
: Exit For
: End If
: Next
: ---------
: Code
: ---------
:
: Can anyone help me please,
:
: Thanx

Jul 17 '05 #3

P: n/a
I've just upgraded 90% of my stuff to VB.NET.. if you hate 0 based array's
steer clear... EVERY array is 0 based in .NET :((

"J French" <er*****@nowhere.com> wrote in message
news:40***************@news.btclick.com...
I'll bet the program does not hang
- it should come up with a ruddy great Error Message

For i = 0 To ComboBox1.Items.Count - 1

Note the minus one
- Zero Based arrays are a PITA
On 10 May 2004 03:50:30 -0700, ni****@hollandridderkerk.nl (Niels)
wrote:
Hello,
I'm trying to get te index of a specific text in a ComboBox.
The ComboBox is filled by searching for filenames in a directory.
The text of the last item selected in the ComboBox is saved in a
textfile in the OnExit Event of the Mainform.
While loading the Mainform I want the text that was saved in the file
to be compared with the items in the ComboBox ad get the index of the
found item, so that I can set it as selected.

I thought i could do it something like this, but the program hangs
before I can see anything.
---------
Code
---------
Dim i, As Integer
Dim line As String
For i = 0 To ComboBox1.Items.Count
If ComboBox1.Items(i) = line Then
IndexFound = i
Exit For
End If
Next
---------
Code
---------

Can anyone help me please,

Thanx

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.