Results 1 to 3 of 3

Thread: ComboBox question

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2002
    Location
    Seattle, WA USA
    Posts
    216

    ComboBox question

    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 --
    VB6, VB.NET, C#, SQL, XML, ADO.NET, ASP.NET, HTML.
    MCP & A+ Certified. Looking for a job in the Seattle, WA area.

  2. #2
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870
    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

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    This is a bit shorter:

    VB Code:
    1. If Not ComboBox1.Items.Contains(TextBox1.Text) Then
    2.             ComboBox1.Items.Add(TextBox1.Text)
    3.         End If

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width