Results 1 to 8 of 8

Thread: [RESOLVED] [2005] GroupBox Container issue?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Resolved [RESOLVED] [2005] GroupBox Container issue?

    I have a Groupbox control on my form in which I have about 8 checkbox controls. One of the checkboxes, if checked, will make the rest of the checkboxes become checked. I have the following code that isn't working.
    Code:
                    For Each ctl As Control In gb1.Container.Components
                        If TypeOf (ctl) Is CheckBox Then
                            ctl.Checked = True   'Isn't working  
                        End If
                    Next
    With what I have above, "Checked" isn't a property of the "ctl". What am I doing wrong in this construct?

    Thanks,
    Blake

  2. #2
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: [2005] GroupBox Container issue?

    Once you find that your Control is of type CheckBox, you have to cast that Control to an actual CheckBox. You don't actually have a CheckBox until you cast that Control to a CheckBox.

    You get "Checked" isn't a property of the "ctl" because Checked really isn't a property of Control. Checked is a property of a CheckBox.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: [2005] GroupBox Container issue?

    nmadd,

    I did what you suggested and still got an error, unless I did it wrong! Here's the code:
    Code:
                    For Each ctl As Control In gb1.Container.Components
                        If TypeOf (ctl) Is CheckBox Then
                            ctl = DirectCast(ctl, CheckBox)
                            ctl.checked = True        'Doesn't like this statement
                        End If
                    Next
    Blake

  4. #4
    Lively Member
    Join Date
    Apr 2006
    Location
    Local
    Posts
    112

    Re: [2005] GroupBox Container issue?

    This should work......

    Code:
    CType(ctrl, CheckBox).Checked = True
    Cheers
    Microsoft Visual Basic 2008

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: [2005] GroupBox Container issue?

    Ok,

    Now I'm getting the attached error message on the highlighted line:
    Code:
                    For Each ctl As Control In gb1.Container.Components
                        If TypeOf (ctl) Is CheckBox Then
                            ctl = DirectCast(ctl, CheckBox)
                            CType(ctl, CheckBox).Checked = False
                        End If
                    Next


    Thanks,
    Blake

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: [2005] GroupBox Container issue?

    nevermind, I saw my problem. SunglassesRon, your code worked to.

    Thanks,
    Blake

  7. #7
    Lively Member
    Join Date
    Apr 2006
    Location
    Local
    Posts
    112

    Re: [2005] GroupBox Container issue?

    You don't need this line...

    Code:
    ctl = DirectCast(ctl, CheckBox)
    Microsoft Visual Basic 2008

  8. #8
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: [2005] GroupBox Container issue?

    On a side note, it won't make much difference unless you're doing this a million times, but if you are certain that you are casting to the right type, DirectCast is faster.

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