Results 1 to 7 of 7

Thread: Loop to check checkbox

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2010
    Posts
    478

    Loop to check checkbox

    There are 30 check box in a form, ckb0501, ckb0502...ckb0530. How to make a loop to check if any one of check boxes was checked?

  2. #2

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2010
    Posts
    478

    Re: Loop to check checkbox

    Sorry. The event is for one by one. I want to make a loop like below:

    for i = 1 to 30
    if ckb050i.checked = true then
    msgbox "ok"
    end if
    next

    But, I do not know how to make ckb050i working.

  4. #4
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Loop to check checkbox

    Are they all in their own container? or just right on the form? Are there other checkbox's with them?
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  5. #5
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Loop to check checkbox

    For any:
    vb Code:
    1. Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
    2.     For Each chk As CheckBox In Me.Controls.OfType(Of CheckBox)()
    3.         If Not chk Is DirectCast(sender, CheckBox) AndAlso chk.Checked Then
    4.             MsgBox("OK")
    5.             Exit For
    6.         End If
    7.     Next
    8. End Sub

    For all:
    vb Code:
    1. Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
    2.     For Each chk As CheckBox In Me.Controls.OfType(Of CheckBox)()
    3.         If Not chk Is DirectCast(sender, CheckBox) AndAlso Not chk.Checked Then Exit Sub
    4.     Next
    5.     MsgBox("OK")
    6. End Sub

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2010
    Posts
    478

    Re: Loop to check checkbox

    Thank you for help. There are another checkbox like ckb0601, ckb0701... and I do not want to check them. The cicatrix's code is for all checkbox.
    My question is how change ckb050i to ckb0501, ckb0502...ckb0530 using a loop.

  7. #7
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Loop to check checkbox

    The easiest way is to put them (groups) in different containers (panels, groupboxes, etc).
    Then you can check only the collection of the checkboxes of the needed container (use Panel1.Controls instead of Me.Controls if Panel1 is the container for them) in the examples above.

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