Results 1 to 7 of 7

Thread: [RESOLVED] Refer to a checkbox using the counter

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2011
    Posts
    53

    Resolved [RESOLVED] Refer to a checkbox using the counter

    Hello

    What I have is 10 checkboxes 1 to 10 which all can be one of several options (the options are constant for all the checkboxes) and I need to change a variable according to what the checkbox.text preperty contains.

    I plan to use a For to next statement

    Code:
    For i = 0 to 9 
    If Checkbox(i+1).text = "hello" then
    String(i) = "bye"
    End If
    
    If Checkbox(i+1).text = "sad" then
    String(i) = "happy"
    End If
    
    Next i
    However I cannot refer to a checkbox in the way i have done above.
    Essentially how do get what the checkbox contains based on on my counter, i.

    Note: it is i+1 because the first check box starts at one yet my counter starts at zero.

    Cheers
    TKP

  2. #2
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Refer to a checkbox using the counter

    There a few ways to go here. Are there any other Checkbox's on the Form? Are they in a GroupBox (would be good if they were)?

  3. #3
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Refer to a checkbox using the counter

    In line with what you had, here is a possible solution:
    vb Code:
    1. For i = 0 To 9
    2.             If Me.Controls("CheckBox" & i + 1).Text = "hello" Then
    3.                 String(i) = "bye"
    4.             End If
    5.  
    6.             If Me.Controls("CheckBox" & i + 1).Text = "sad" Then
    7.                 String(i) = "happy"
    8.             End If
    9.  
    10.         Next i

    Now, reduced to:
    vb Code:
    1. For i = 0 To 9
    2.             If Me.Controls("CheckBox" & i + 1).Text = "hello" Then
    3.                 String(i) = "bye"
    4.             ElseIf Me.Controls("CheckBox" & i + 1).Text = "sad" Then
    5.                 String(i) = "happy"
    6.             End If
    7.         Next i
    Last edited by Bruce Fox; Jan 11th, 2011 at 08:47 PM.

  4. #4

    Thread Starter
    Member
    Join Date
    Jan 2011
    Posts
    53

    Re: Refer to a checkbox using the counter

    Yes there are 10 CheckBoxes in a GroupBox called UserInput

  5. #5

    Thread Starter
    Member
    Join Date
    Jan 2011
    Posts
    53

    Re: Refer to a checkbox using the counter

    Thanks Bruce Fox it worked amazing

    TKP

  6. #6
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: [RESOLVED] Refer to a checkbox using the counter

    What your also missing here is that you need to check those CheckBoxes that are selected, not just their Text property.

  7. #7
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: [RESOLVED] Refer to a checkbox using the counter

    Here is probably a better (dynamic) way, in that you can add or remove CheckBoxes without ther need to adjust Loop counters etc:
    vb Code:
    1. For Each ctl In Me.gbUserInput.Controls
    2.             ' If the Control is a CheckBox, do something
    3.             If TypeOf ctl Is CheckBox Then
    4.                 Dim cb = DirectCast(ctl, CheckBox)
    5.                 ' If the CheckBox is Selected, do something
    6.                 If cb.CheckState = CheckState.Checked Then
    7.                     If cb.Text = "hello" Then
    8.                         String(i) = "bye"
    9.                     ElseIf cb.Text = "sad" Then
    10.                         String(i) = "happy"
    11.                     End If
    12.                 End If
    13.             End If
    14.         Next

    Note, I prefixed 'UserInput' with gb in line with Hungarian Notation (to help identify the Control).

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