Results 1 to 5 of 5

Thread: [2005] Loop through checkboxes

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    3

    Unhappy [2005] Loop through checkboxes

    I am having some difficulty figuring out how to loop through checkbox selections to be placed into a text box.

    Design of the form looks like this: http://www.stelth2000inc.com/images/screen.png

    Each checkbox I'm looking to have associated with a string.

    HTML Code:
    Name: [color=blue][size=10]Name: character name[/size][/color]
    World: [color=blue][size=10]World: World[/size][/color]
    character name comes from the chNameTxt.Text from the general tab
    world comes from worldCombo.SelectedText from the general tab

    Sex check box will be pulling from either maleRadio or femaleRadio based on which ever is checked.

    The user can select all the check boxes if they want or as little as just a single check box.

    I've been trying to keep in mind of the user changing selection after clicking the "create signature" button and re-clicking the button for their alterations. So I know I will want to clear the text box prior to each click of the button.

    I think I've explained this well enough for some help. If not, let me know.
    Last edited by Godko; Apr 10th, 2006 at 03:21 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Loop through checkboxes

    Put the value you want to associate with each check box in its Tag property. You can then use the code in my "Visit All Controls" Codebank submission (see my signature for link). As you visit each control you can get the value associated with it something like this:
    VB Code:
    1. If TypeOf ctl Is CheckBox AndAlso DirectCast(ctl, CheckBox).Checked Then
    2.     MessageBox.Show(CStr(ctl.Tag))
    3. End If
    Obviously you'll want to do something other than display the value in a message box but the principle is the same.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    3

    Re: [2005] Loop through checkboxes

    okay, I see where your coming from with that. But, by placing info in the Tag property, how do I go about pulling information from other fields.

    As of this moment, I have in the Tag property of the Character Name check box

    HTML Code:
    "[color=blue][size=10]Name: " & charNameTxt.Text & "[/size][/color] " & vbCrLf
    When I click the button to perform the action what ends up in the text box is exactly what I place in that Tag property. Quotes and all. I need the charNameTxt.Text to read what I have in that text box and place it there

    And the issue with when someone makes a selection of Sex, how do I get it to read if someone selected the Male radio button or the Female radio button.

    My Current code based on what you gave me is:

    VB Code:
    1. Dim ctl As Control = Me.GetNextControl(Me, True) 'Get the first control in the tab order.
    2.         Do Until ctl Is Nothing
    3.             If TypeOf ctl Is CheckBox AndAlso DirectCast(ctl, CheckBox).Checked Then
    4.                 signatureTxt.Text = CStr(ctl.Tag)
    5.             End If
    6.             ctl = Me.GetNextControl(ctl, True) 'Get the next control in the tab order.
    7.         Loop

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Loop through checkboxes

    You could handle the CheckChanged event of your Male and Female RadioButtons and set the Tag of the Sex (maybe Gender would be better) CheckBox then, or else handle the CheckChanged event of the CheckBox and set the Tag based on which RadioButton is selected. The second option will only work if you cannot change the RadioButtons after checking the CheckBox.

    Also note that you should be appending each value in the Do loop. As it is you are overwriting the existing Text each time.

    You should also note that you don't have to change the signature only when those CheckBoxes change. You might also handle the Leave event of the charNameTxt control to update the signature so that the current value is always used without you having to put the literal value in the Tag property of the corresponding CheckBox.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    3

    Re: [2005] Loop through checkboxes

    could u explain in more detail please, possibly provide examples?

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