Results 1 to 7 of 7

Thread: [RESOLVED] [02/03] Simple combobox question

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2004
    Location
    U.K
    Posts
    752

    Resolved [RESOLVED] [02/03] Simple combobox question

    Hi people,

    I was hoping someone could help me out please, the question is really simple.

    I have 2 forms, on form 1 I have 2 buttons which both open up form 2. What I want is when button 1 opens up form2, I want to display text 1,3 and 4. And when button 2 opens up form 2 I want to display only text 2 in the combobox.

    In the combobox collection I have the following text one, two, three, four.

    So far I have the following coding which doesn't do much apart from display two in the combobox when button 2 is selected:

    Code:
    'button1
     frm2.ShowDialog()
     frm2.Dispose()
    
    'button2
     frm2.Tag = "Two"
     frm2.ShowDialog()
     frm2.Dispose()
    
    
    frm2 load event'
    
      If Me.Tag = "Two" Then
                cmbnum.SelectedIndex = 1
                cmbnum.Text = "Two"
    Any help please? thanks

  2. #2
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: [02/03] Simple combobox question

    Remove all the text from the combobox, so the list is empty.

    Then, if Tag = "Two"
    cmbnum.Items.Add("Two")
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2004
    Location
    U.K
    Posts
    752

    Cool Re: [02/03] Simple combobox question

    Oh, sorry how would I empty the list in the combobox?

  4. #4
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [02/03] Simple combobox question

    vb Code:
    1. Me.ComboBox1.Items.Clear()
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2004
    Location
    U.K
    Posts
    752

    Re: [02/03] Simple combobox question

    Oh ok, So far I deleted the items in the cmbnum collection (one, two, three, four as the text is going to be hard coded.) Then I have the following coding on the form2 load event:

    Code:
            Me.cmbnum.Items.Clear()
            If Me.Tag = "Two" Then
                cmbnum.Text = "Two"
                If cmbnum.Items.Count > -1 Then cmbnum.SelectedIndex = 0
            Else
                Me.cmbnum.Items.Add("One")
                Me.cmbnum.Items.Add("Three")
                Me.cmbnum.Items.Add("Four")
                If cmbnum.Items.Count > -1 Then cmbstatus.SelectedIndex = 0
            End If
    Now when button one opens up form 2 it shows the correct data in the combobox, however when button two opens up form 2 it breaks on the following line:

    cmbnum.SelectedIndex = 0

    Additional information: Specified argument was out of the range of valid values.

    Any help please?

  6. #6
    Member Daarklord's Avatar
    Join Date
    Jul 2008
    Posts
    54

    Re: [02/03] Simple combobox question

    You did not add "Two" to the combox when button 2 is clicked. Setting the text does not add to the list of items.

    Code:
            Me.cmbnum.Items.Clear()
            If Me.Tag = "Two" Then
                cmbnum.Text = "Two"
                Me.cmbnum.Items.Add("Two")
                If cmbnum.Items.Count > -1 Then cmbnum.SelectedIndex = 0
            Else
                Me.cmbnum.Items.Add("One")
                Me.cmbnum.Items.Add("Three")
                Me.cmbnum.Items.Add("Four")
                If cmbnum.Items.Count > -1 Then cmbstatus.SelectedIndex = 0
            End If
    Also, your If cmbnum.Items.Count > -1 doesn't check for empty list, in which case the count would be 0.
    Last edited by Daarklord; Jul 18th, 2008 at 06:02 AM.
    0xABADA55D00D

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2004
    Location
    U.K
    Posts
    752

    Re: [02/03] Simple combobox question

    thanks

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