Compare textfield against listbox rows?

Hi,
I have a textfield where the user enters a language name.
I then have the code below which I thought would check all the existing values in my listbox and then compare them against user entered data in the textfield.

The code produces no errors - but still allows me to create an unwanted duplicate language name.
I presume this is due to i returning an integer?

I cannot work out how to fetch the value of column1 of i - and compare that against the user entered string :frowning:

[code]// LOOP THROUGH THE LanguageListbox
for i As Integer= myWindow.LanguageListbox.ListCount - 1 DownTo 0

// IF NEW NAME ALREADY EXISTS
if myWindow.LanguageListbox.(i) = NewLanguageNameField.text then

// DISPLAY THE ERROR MSGBOX
MsgBox(“This language already exists - please rename and then retry!”)
end if

next[/code]

Thank you.

Use Cell(row, column) instead of List(row).

I don’t quite understand Eli - my code does not have a List(row) ??

Do you mean like this:

[code]// LOOP THROUGH THE LanguageListbox
for i As Integer= myWindow.LanguageListbox.ListCount - 1 DownTo 0

// IF NEW NAME ALREADY EXISTS
if myWindow.LanguageListbox.Cell(i, 1) = NewLanguageNameField.text then

// DISPLAY THE ERROR MSGBOX
MsgBox(“This language already exists - please rename and then retry!”)
end if

next[/code]

That is correct as long as the language entry that you’re trying to match is in the second column (listbox columns are 0-based).

Yes - it is.

Thank you everyone - much appreciated :slight_smile:

I’d add an ‘Exit For’ or ‘Exit For i’ after your messagebox, so that the rest of the rows are not processed after finding a match.

// LOOP THROUGH THE LanguageListbox
for i As Integer= myWindow.LanguageListbox.ListCount - 1 DownTo 0

// IF NEW NAME ALREADY EXISTS
if myWindow.LanguageListbox.Cell(i, 1) = NewLanguageNameField.text then

// DISPLAY THE ERROR MSGBOX
MsgBox(“This language already exists - please rename and then retry!”)
Exit For i
end if

next

Thank you all.

Richard: may I tell you something? Your comments should describe why you did something. They should not restate the code.

You can see that the loop is over the languages. Same for the msgbox. If you have some interest to improve your coding style read “Code complete”.

Thanks Beatrix - my comments do normally explain what the code does AND why it’s doing it.
What you see is simply quick initial comments.

Plus - what people seem to forget is that what may look like obvious code to a professional, is not so clear to the novice.
Therefore, comments which may look like they are restating the code - are actually making it easier for me to see at a glance what is happening :slight_smile:

Thanks for the link - I will take a look anyway :slight_smile:
I appreciate ALL tips, so thank you!