duplicate in combobox.....

S

Supra

does ne 1 know how to prevent duplicated in combobox? i convert from
vb6 to vb.net. but in vb6 would work.

Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress
If Asc(e.KeyChar) = Keys.Enter Then
e.Handled = True
AxWebBrowser1.Navigate(ComboBox1.Text.ToString)
Dim bFound As Boolean
bFound = False
For i As Integer = 1 To ComboBox1.Items.Count
If ComboBox1.SelectedItem = ComboBox1.Text.ToString Then
bFound = True
Exit For
End If
Next
If bFound = False Then
ComboBox1.Items.Add(ComboBox1.Text.ToString)
End If
End If
End Sub
 
K

Ken Dopierala Jr.

Hi,

Try changing the For line to:

For i As Integer = 0 to (ComboBox1.Items.Count - 1)

Good luck! Ken.
 
S

Supra

i solved it.....i found another newsgroup in windowform

it is item(i) instead of ComboBox1.SelectedItem

If ComboBox1.Items(i) = ComboBox1.Text Then

regards,
 
C

Cor Ligthert

Supra,

That part you show is as well not right, however I will advice you to take
the answer from Ken as well. You solution should not work (when there is no
match) when you do not add that and you never take the first one. It is as
well not needed to convert the Text to String, while Text is already a
String. But that does not give errors.

And to confuse you even more, there is a much easier instruction for what
you are doing.
http://msdn.microsoft.com/library/d...owsformscomboboxclassfindstringexacttopic.asp

I hope this gives you an idea?

Cor



Cor
 
B

Barry

You are looping with "For i as Integer =1 to ...." but you don't use
it. You are checking SelectedItem (and you would want to check
SelectedItem.ToString rather than the object itself) where you should
check each item in the ComboBox whether it is selected or not - use
ComboBox1.items(i).ToString. Now maybe you are checking only the
selectedItem to see if it's text matches what you are inputing, but
what if the dup is somewhere else??

So fix both of those things and see which is needed...-

SelectedItem.ToString
and
ComboBox1.items(i).ToString

to see what fits with your logic....


does ne 1 know how to prevent duplicated in combobox? i convert from
vb6 to vb.net. but in vb6 would work.

Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress
If Asc(e.KeyChar) = Keys.Enter Then
e.Handled = True
AxWebBrowser1.Navigate(ComboBox1.Text.ToString)
Dim bFound As Boolean
bFound = False
For i As Integer = 1 To ComboBox1.Items.Count
If ComboBox1.SelectedItem = ComboBox1.Text.ToString Then
bFound = True
Exit For
End If
Next
If bFound = False Then
ComboBox1.Items.Add(ComboBox1.Text.ToString)
End If
End If
End Sub

bceggersATcomcastDOTnet
 
T

Tom Shelton

does ne 1 know how to prevent duplicated in combobox? i convert from
vb6 to vb.net. but in vb6 would work.

Private Sub ComboBox1_KeyPress _
(ByVal sender As Object, ByVal e As KeyPressEventArgs) _
Handles ComboBox1.KeyPress

If Asc(e.KeyChar) = Keys.Enter Then
e.Handled = True
AxWebBrowser1.Navigate(ComboBox1.Text.ToString)

If ComboBox1.FindStringExact (ComboBox1.ComboBox1.Text) = -1 Then
ComboBox1.Items.Add(ComboBox1.Text.ToString)
End If
End If

End Sub


FindString and FindStringExact are your friends...
 
S

Supra

thank
You are looping with "For i as Integer =1 to ...." but you don't use
it. You are checking SelectedItem (and you would want to check
SelectedItem.ToString rather than the object itself) where you should
check each item in the ComboBox whether it is selected or not - use
ComboBox1.items(i).ToString. Now maybe you are checking only the
selectedItem to see if it's text matches what you are inputing, but
what if the dup is somewhere else??

So fix both of those things and see which is needed...-

SelectedItem.ToString
and
ComboBox1.items(i).ToString

to see what fits with your logic....




bceggersATcomcastDOTnet
 
S

Supra

thank
You are looping with "For i as Integer =1 to ...." but you don't use
it. You are checking SelectedItem (and you would want to check
SelectedItem.ToString rather than the object itself) where you should
check each item in the ComboBox whether it is selected or not - use
ComboBox1.items(i).ToString. Now maybe you are checking only the
selectedItem to see if it's text matches what you are inputing, but
what if the dup is somewhere else??

So fix both of those things and see which is needed...-

SelectedItem.ToString
and
ComboBox1.items(i).ToString

to see what fits with your logic....




bceggersATcomcastDOTnet
 

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