Results 1 to 25 of 25

Thread: Control looping.

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Control looping.

    Hi all,

    I am trying to do something and not able to get to where I need to go. I have 12 checkboxes on a form. 4 for "And", 4 for "Or" and 4 for "Exclude".

    I am trying to check to see if all checkboxes that contain the word "Exclude" in the control name are checked. If they are ALL checked, to show a messagebox. I am getting there but when I try to loop through the controls to check the name, I am getting a blue line under the code that says:

    Class 'System.Windows.Form.Control' cannot be indexed because it has no default property.

    Screenshots attached.
    Attached Images Attached Images   

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Control looping.

    Ok, I have tried the following and the messagebox comes up when I go to make a different selection other than what is default on form load. Hmmm.

    vb Code:
    1. <CLSCompliant(True)> Private Sub DisallowAllExclude()
    2.         Dim CheckBoxArr() As Elegant.Ui.CheckBox = {chkResolutionTypeExclude, _
    3.                                                     chkDateToFromExclude, _
    4.                                                     chkClientNameExclude, _
    5.                                                     chkClientPhoneNumberExclude}
    6.  
    7.         Dim CheckBoxArr2() As Elegant.Ui.CheckBox = {chkResolutionTypeAnd, _
    8.                                                      chkDateToFromAnd, _
    9.                                                      chkClientNameAnd, _
    10.                                                      chkClientPhoneNumberAnd}
    11.  
    12.         For Each chk As Elegant.Ui.CheckBox In CheckBoxArr
    13.             If chk.CheckState = CheckState.Checked Then
    14.                 If MessageBox.Show("You cannot exclude all options when trying to search.  Please modify your selection and try again.", _
    15.                                    "Exclusion Issue Encountered", _
    16.                                    MessageBoxButtons.OK, _
    17.                                    MessageBoxIcon.Exclamation, _
    18.                                    MessageBoxDefaultButton.Button1, _
    19.                                    MessageBoxOptions.DefaultDesktopOnly, False) = Windows.Forms.DialogResult.OK Then
    20.  
    21.                     'Check all the And checkboxes.
    22.                     For Each chk2 As Elegant.Ui.CheckBox In CheckBoxArr2
    23.                         chk2.CheckState = CheckState.Checked
    24.                     Next chk2
    25.                 Else 'Do nothing
    26.                     '
    27.                 End If
    28.             End If
    29.         Next chk
    30.     End Sub

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Control looping.

    here's an example using vb2005 + standard checkboxes:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim CheckBoxArr() As CheckBox
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         CheckBoxArr = New CheckBox() {chkResolutionTypeExclude, _
    7.                                                     chkDateToFromExclude, _
    8.                                                     chkClientNameExclude, _
    9.                                                     chkClientPhoneNumberExclude}
    10.  
    11.     End Sub
    12.  
    13.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    14.         MsgBox(Array.FindAll(CheckBoxArr, AddressOf isChecked).Length)
    15.     End Sub
    16.  
    17.     Private Function isChecked(ByVal cb As CheckBox) As Boolean
    18.         Return cb.Checked
    19.     End Function
    20.  
    21. End Class

    this is how i'd do it in vb2008:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim CheckBoxArr() As CheckBox
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         CheckBoxArr = New CheckBox() {chkResolutionTypeExclude, _
    7.                                                     chkDateToFromExclude, _
    8.                                                     chkClientNameExclude, _
    9.                                                     chkClientPhoneNumberExclude}
    10.  
    11.     End Sub
    12.  
    13.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    14.         MsgBox(CheckBoxArr.Count(Function(cb) cb.Checked))
    15.     End Sub
    16.  
    17. End Class

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

    Re: Control looping.

    How about ...:
    vb Code:
    1. If GrandChildControl.name.Contains("Exclude") Then
    2.  
    3. Dim cb = Directcast(GrandCHildControl, Elegant.Ui.CheckBox)
    4. If cb.Checked = CheckedState.Checked Then...
    5.  
    6. End If
    7.  
    8. End If

    Or
    vb Code:
    1. If GrandChildControl.name.Contains("Exclude") Then
    2.  
    3. If Directcast(GrandCHildControl, Elegant.Ui.CheckBox).Checked = CheckedState.Checked Then ...
    4.  
    5. End If
    6.  
    7. End If


    Syntax may be off as I typed it in NotePad.

    Note, you don't need an index as you already iterating the CheckBox's named Exclude, and in this case there would br four.
    Last edited by Bruce Fox; Jan 13th, 2011 at 03:18 PM.

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Control looping.

    Quote Originally Posted by .paul. View Post
    here's an example using vb2005 + standard checkboxes:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim CheckBoxArr() As CheckBox
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         CheckBoxArr = New CheckBox() {chkResolutionTypeExclude, _
    7.                                                     chkDateToFromExclude, _
    8.                                                     chkClientNameExclude, _
    9.                                                     chkClientPhoneNumberExclude}
    10.  
    11.     End Sub
    12.  
    13.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    14.         MsgBox(Array.FindAll(CheckBoxArr, AddressOf isChecked).Length)
    15.     End Sub
    16.  
    17.     Private Function isChecked(ByVal cb As CheckBox) As Boolean
    18.         Return cb.Checked
    19.     End Function
    20.  
    21. End Class

    this is how i'd do it in vb2008:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim CheckBoxArr() As CheckBox
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         CheckBoxArr = New CheckBox() {chkResolutionTypeExclude, _
    7.                                                     chkDateToFromExclude, _
    8.                                                     chkClientNameExclude, _
    9.                                                     chkClientPhoneNumberExclude}
    10.  
    11.     End Sub
    12.  
    13.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    14.         MsgBox(CheckBoxArr.Count(Function(cb) cb.Checked))
    15.     End Sub
    16.  
    17. End Class
    I tried that too and I got the following error.
    Attached Images Attached Images  

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Control looping.

    you declared ExcludeArr as a variable + not an array. try this:

    vb Code:
    1. Dim ExcludeArr() as Elegant.UI.Checkbox

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Control looping.

    Quote Originally Posted by Bruce Fox View Post
    How about ...:
    vb Code:
    1. If GrandChildControl.name.Contains("Exclude") Then
    2.  
    3. Dim cb = Directcast(GrandCHildControl, Elegant.Ui.CheckBox)
    4. If cb.Checked = CheckedState.Checked Then...
    5.  
    6. End If
    7.  
    8. End If

    Or
    vb Code:
    1. If GrandChildControl.name.Contains("Exclude") Then
    2.  
    3. If Directcast(GrandCHildControl, Elegant.Ui.CheckBox).Checked = CheckedState.Checked Then ...
    4.  
    5. End If
    6.  
    7. End If


    Syntax may be off as I typed it in NotePad.

    Note, you don't need an index as you already iterating the CheckBox's named Exclude, and in this case there would br four.
    I had tried that too before posting the thread. This doesnt work because as soon as I check one checkbox for Exclude, the messagebox comes up.

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Control looping.

    Ok, I tried Pauls code and I am not getting far right now. Screen shots attached.
    Attached Images Attached Images    

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Control looping.

    the length property of an array is an integer property + you need to test it is = 4 to test if all your checkboxes are checked:

    vb Code:
    1. if Array.FindAll(ArrExclude, AddressOf isChecked).length = 4 then
    2. ...

  10. #10
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Control looping.

    Try this:
    Code:
        Private excludeCheckBoxes() As Elegant.Ui.CheckBox = Nothing
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.excludeCheckBoxes = New Elegant.Ui.CheckBox() {chkResolutionTypeExclude, _
                                                                                     chkDateToFromExclude, _
                                                                                     chkClientNameExclude, _
                                                                                     chkClientPhoneNumberExclude}
    
        End Sub
    
        Private Function AreAllExcludeCheckBoxesChecked() As Boolean
              Dim result As Boolean = True
              For Each chkBox As Elegant.Ui.CheckBox In Me.excludeCheckBoxes
                   If chkBox.Checked = False Then
                         result = False
                         Exit For
                   End If
              Next
              Return result
        End Function
    
       <CLSCompliant(True)> Private Sub DisallowAllExclude()
              If Me.AreAllExcludeCheckBoxesChecked() = True Then
                    MessageBox.Show("Hey, you can't exclude every options like that.")
              End If
       End Sub
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  11. #11

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Control looping.

    I did a messagebox to show the value and verified is 0 for false and 1 for true. I still get the same error when doing:

    If Array.FindAll(ArrExclude, AddressOf isChecked).Length = 1 Then

    hm!

  12. #12

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Control looping.

    Quote Originally Posted by .paul. View Post
    the length property of an array is an integer property + you need to test it is = 4 to test if all your checkboxes are checked:

    vb Code:
    1. if Array.FindAll(ArrExclude, AddressOf isChecked).length = 4 then
    2. ...
    i tried that too to see what happens and i got an InvalidOperationException.

  13. #13

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Control looping.

    Quote Originally Posted by stanav View Post
    Try this:
    Code:
        Private excludeCheckBoxes() As Elegant.Ui.CheckBox = Nothing
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.excludeCheckBoxes = New Elegant.Ui.CheckBox() {chkResolutionTypeExclude, _
                                                                                     chkDateToFromExclude, _
                                                                                     chkClientNameExclude, _
                                                                                     chkClientPhoneNumberExclude}
    
        End Sub
    
        Private Function AreAllExcludeCheckBoxesChecked() As Boolean
              Dim result As Boolean = True
              For Each chkBox As Elegant.Ui.CheckBox In Me.excludeCheckBoxes
                   If chkBox.Checked = False Then
                         result = False
                         Exit For
                   End If
              Next
              Return result
        End Function
    
       <CLSCompliant(True)> Private Sub DisallowAllExclude()
              If Me.AreAllExcludeCheckBoxesChecked() = True Then
                    MessageBox.Show("Hey, you can't exclude every options like that.")
              End If
       End Sub
    Invalid Operation Exception. Frustrating. I can get it to work without an issue when using an If block but I am sure there is an easier way of doing this.
    Attached Images Attached Images  

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Control looping.

    what sort of size is your project? could you upload it (remove any confidential information)?

  15. #15
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Control looping.

    The last error you got is a null reference exception. Do you know which line in your code that it causes it?
    The code I posted will not cause it provided that you have the checkboxes as named on your form. And If you don't, it won't compile and thus you can't get an exception from it.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  16. #16

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Control looping.

    Those errors happen when I launch the form in run-time via the button. If I remove Pauls code or comment it out, the form loads without error.

  17. #17

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Control looping.

    Quote Originally Posted by .paul. View Post
    what sort of size is your project? could you upload it (remove any confidential information)?
    The whole folder is 98.3MB in size.

  18. #18

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Control looping.

    Quote Originally Posted by stanav View Post
    The last error you got is a null reference exception. Do you know which line in your code that it causes it?
    The code I posted will not cause it provided that you have the checkboxes as named on your form. And If you don't, it won't compile and thus you can't get an exception from it.
    The control names are correct. I made sure of it and double checked. The error happens on the loading of the form in the button click event.

  19. #19

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Control looping.

    Quote Originally Posted by .paul. View Post
    what sort of size is your project? could you upload it (remove any confidential information)?
    i am about to send you a PM with the URL i uploaded the project to. i am using two custom controls on the forms so you will need to install them. ill explain more in the PM.

  20. #20
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Control looping.

    ok...

  21. #21
    Fanatic Member
    Join Date
    Aug 2006
    Location
    In my head
    Posts
    913

    Re: Control looping.

    Here is my go at it, I have used this type of loop before and it has worked very well for me (well enough that I include a more enhanced variation of it with my base dll in all projects):
    Code:
     Private Function All_Exclude_checked(ByVal ctrl As Control) As Boolean
            'the thought process behind this is to send your panel containing the controls
            ' in as the initial control and allow this to loop. If there are any sub controls
            ' then they are handled with the loop below. Otherwise, it will check the control
            ' itself. if it is the type of control you are looking for then check the text. If
            ' the text is NOT "Excluded" then return false. This would mean that if ANY other
            ' checkbox was marked then it would return false.
            If CType(ctrl, Control).HasChildren = True Then
                For Each child As Control In ctrl.Controls
                    All_Exclude_checked(child)
                Next
            End If
    
            If TypeOf ctrl Is CheckBox Then
                If CType(ctrl, CheckBox).Text <> "Excluded" Then
                    Return False
                Else
                    Return True
                End If
            End If
        End Function
    Hope that this helps some!

    D
    Platforms of choice: Visual Studio 2005/2008 Professional : Visual Studio 2010 Enterprise : PHP - Notepad++/WAMP

    Please Rate If I helped you.
    Please remember to mark threads as closed if your issue has been resolved.

    Reserved Words in Access | Connection Strings

  22. #22
    Fanatic Member
    Join Date
    Aug 2006
    Location
    In my head
    Posts
    913

    Re: Control looping.

    After looking back over the code, I noticed that I REALLY messed up on it. Here is a revised version that works - with checkboxes anyways!!
    Code:
     Private trigger As Boolean = True
    
        Private Function All_Exclude_checked(ByVal ctrl As Control) As Boolean
            If CType(ctrl, Control).HasChildren = True Then
                For Each child As Control In ctrl.Controls
                    All_Exclude_checked(child)
                Next
            ElseIf TypeOf ctrl Is CheckBox Then
                If CType(ctrl, CheckBox).Checked = True Then
                    If CType(ctrl, CheckBox).Text <> "Excluded" Then
                        trigger = False
                    End If
                End If
            End If
            Return trigger
        End Function
    I am using an external variable to keep track of whether another box was checked. Be SURE to reset that variable (trigger) BEFORE you run it again because it will retain the last value it was set to!!!

    Again, sorry for the sloppiness - I really need to watch when I am copying and pasting and trimming stuff down!!!

    D
    Platforms of choice: Visual Studio 2005/2008 Professional : Visual Studio 2010 Enterprise : PHP - Notepad++/WAMP

    Please Rate If I helped you.
    Please remember to mark threads as closed if your issue has been resolved.

    Reserved Words in Access | Connection Strings

  23. #23

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Control looping.

    OK, I am using FxCop from Microsoft to scan my built exe and it is giving me an error. It is saying:

    'Ctrl', a parameter, is cast to type 'CheckBox' multiple times in method 'FrmRecords.AllExcludedChecked(Control)'. Cache the result of the 'as' operator or direct cast in order to eliminate the redundant isint instruction."
    It is giving this URL as a help topic.

    So as per the topic suggested, I went into my code and changed it to the following and it still says the same thing when I rebuild the project and scan it with FxCop.

    vb Code:
    1. <CLSCompliant(True)> Private Function AllExcludedChecked(ByVal Ctrl As Control) As Boolean
    2.         Dim Chk As Elegant.Ui.CheckBox = DirectCast(Ctrl, Elegant.Ui.CheckBox)
    3.  
    4.         If Ctrl.HasChildren = True Then
    5.             For Each Child As Control In Ctrl.Controls
    6.                 AllExcludedChecked(Child)
    7.             Next Child
    8.         ElseIf TypeOf Ctrl Is Elegant.Ui.CheckBox Then
    9.             If Chk.Checked = True Then
    10.                 If Chk.Text = "Exclude" Then
    11.                     Trigger = True
    12.                 Else
    13.                     Trigger = False
    14.                 End If
    15.             End If
    16.         End If
    17.  
    18.         Return Trigger
    19.     End Function

    Any ideas?

  24. #24
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Control looping.

    Something to think about...

    Code:
    Public Class Form1
    
        Dim ctlDict As New Dictionary(Of String, Control)
    
        Private Sub Form1_Load(ByVal sender As System.Object, _
                               ByVal e As System.EventArgs) Handles MyBase.Load
    
            'build a dictionary of all controls
            Dim ctl As Control = Me.GetNextControl(Me, True) 'Get the first control in the tab order.
            Do Until ctl Is Nothing
                ctlDict.Add(ctl.Name, ctl)
                ctl = Me.GetNextControl(ctl, True) 'Get the next control in the tab order.
            Loop
        End Sub
    
    
        Private Sub Button1_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) Handles Button1.Click
            'query the dictionary of controls
            Dim foo As IEnumerable(Of Generic.KeyValuePair(Of String, Control))
            foo = From de In ctlDict _
                  Where de.Key.Contains("Exclude") AndAlso _
                  TypeOf de.Value Is CheckBox AndAlso _
                  CType(de.Value, CheckBox).Checked Select de
    
            'the count
            Debug.WriteLine(foo.Count.ToString)
        End Sub
    End Class
    Last edited by dbasnett; Jan 30th, 2011 at 10:27 AM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  25. #25

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Control looping.

    thanks, ill check it out.

    Quote Originally Posted by dbasnett View Post
    Something to think about...

    Code:
    Public Class Form1
    
        Dim ctlDict As New Dictionary(Of String, Control)
    
        Private Sub Form1_Load(ByVal sender As System.Object, _
                               ByVal e As System.EventArgs) Handles MyBase.Load
    
            'build a dictionary of all controls
            Dim ctl As Control = Me.GetNextControl(Me, True) 'Get the first control in the tab order.
            Do Until ctl Is Nothing
                ctlDict.Add(ctl.Name, ctl)
                ctl = Me.GetNextControl(ctl, True) 'Get the next control in the tab order.
            Loop
        End Sub
    
    
        Private Sub Button1_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) Handles Button1.Click
            'query the dictionary of controls
            Dim foo As IEnumerable(Of Generic.KeyValuePair(Of String, Control))
            foo = From de In ctlDict _
                  Where de.Key.Contains("Exclude") AndAlso _
                  TypeOf de.Value Is CheckBox AndAlso _
                  CType(de.Value, CheckBox).Checked Select de
    
            'the count
            Debug.WriteLine(foo.Count.ToString)
        End Sub
    End Class

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