I have a string "Hello". I want to add the string "Hello" as an item into a ComboBox. But if the string already exists, I want a MsgBox to popup saying "Sorry, item already exists". How do I do that?
-- Ethan --
Printable View
I have a string "Hello". I want to add the string "Hello" as an item into a ComboBox. But if the string already exists, I want a MsgBox to popup saying "Sorry, item already exists". How do I do that?
-- Ethan --
add a combobox to your form
Code:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
ComboBox1.Items.Add("hello")
ComboBox1.Items.Add("nick")
ComboBox1.Items.Add("swan")
Dim i As Integer
Dim temp As Boolean
temp = True
For i = 0 To ComboBox1.Items.Count - 1
If StrComp("hello", ComboBox1.Items.Item(i)) = 0 Then
temp = False
End If
Next
If temp = True Then
ComboBox1.Items.Add("hello")
Else
MessageBox.Show("already exists")
End If
End Sub
hope it helps
Nick
This is a bit shorter:
VB Code:
If Not ComboBox1.Items.Contains(TextBox1.Text) Then ComboBox1.Items.Add(TextBox1.Text) End If