CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2009
    Posts
    9

    Combo Box, If Then Statement

    Hi,

    I'm trying to create a combo box that, when I select an item, certain checkboxes become checked. I'm sure I can use an If Then statement to do that, but I'm running into a problem and I can't figure it out. I'll post the code below. If my code looks fine, I'll post the error. But, I'm sure I'm messing up somewhere

    Thanks
    Code:
    Private Sub ConfigComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConfigComboBox.SelectedIndexChanged
            If ConfigComboBox.SelectedItem = 0 Then
                chkbxKas.Checked = True
            End If
        End Sub

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Combo Box, If Then Statement

    It looks like it will compile, but without trying it, it's not worth it to look. OTOH, if you had posted the error and the line # along with the code, it would probably have been answered by now.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jan 2009
    Posts
    9

    Re: Combo Box, If Then Statement

    Sorry. I just thought I may have messed the code up.

    Here's the error:

    An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll

    Additional information: Conversion from string "Minimal Removals [Quick]" to type 'Double' is not valid.

    It occurs on line:
    Code:
    If ConfigComboBox.SelectedItem = 0 Then
    Thanks

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Combo Box, If Then Statement

    Code:
    Minimal Removals [Quick]" to type 'Double'
    That says it all. Your item selected isn't type Double, and there is no conversion available.

    After you type the = sign, it should give you the available options. Use one of them!
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Combo Box, If Then Statement

    The question here is what type of data is bound to the combobox.. You probably got a "List (Of {Object})" as the bound data.. the better bet here then would be to use "Selectedindex" ..

    Code:
        Private Sub ConfigComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConfigComboBox.SelectedIndexChanged
            If ConfigComboBox.SelectedIndex = 0 Then
                chkbxKas.Checked = True
            End If
        End Sub
    "Selecteditem" will return the selected {Object} , of the bound data .. where you can then use
    Code:
     Dim ThisItem as {Object} = Ctype(ConfigComboBox.SelectedItem, {Object})
    where you can now check exactly what item has been selected..

    Gremmy..
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  6. #6
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    Re: Combo Box, If Then Statement

    Yup, selectedindex works fine.

    One note though:
    Code:
    chkbxKas.Checked = True
    Works.

    But I've found that it's inconsistent, and this works every time:
    Code:
    chkbxKas.CheckState = CheckState.Checked
    I assume because there is 3rd possible state, it's best to be explicit.
    Last edited by TT(n); January 29th, 2009 at 05:29 AM. Reason: speculative

  7. #7
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Combo Box, If Then Statement

    From the VB6 tags:

    Code:
    vbChecked
    vbUnchecked
    cbGrey
    1,0,-1

    as I recall.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  8. #8
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    Re: Combo Box, If Then Statement

    From the VB6 tags:
    Ahh the good old days,....
    I still love to backport to vb6 today though.

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

Featured