locked
Panel Control RRS feed

  • Question

  • Hi all.

    I have a question. Using for each construct, how can i select a group of controls in a panel control? That is i have labels and check boxes and i want to check and use only the checked check boxes ..

    This is my code ..

    for each aCtrl as CheckBox in myPanel.Controls

    if aCtrl.Checked Then

    ' I do a little stuff here ..

    end if

    Next

    Where am i going wrong in this approach?

    • Moved by Cookie Luo Monday, February 14, 2011 6:09 AM (From:.NET Base Class Library)
    Friday, February 11, 2011 2:03 PM

Answers

  • Did you get this exception?

    "Unable to cast object of type 'System.Windows.Forms.TextBox' to type 'System.Windows.Forms.CheckBox'."

    or something similar?

    Does your Panel instance contain controls other than checkboxes? If so, you'll get an invalid cast exception because of the As Checkbox clause in your For Each.

    Instead, use the GetType method in your loop to determine control type before testing the Checked property.

    Sort of like this:

    For Each aCtrl In MyPanel.Controls
          
          If aCtrl.GetType.ToString = "System.Windows.Forms.CheckBox" Then
    
            If aCtrl.Checked Then
    
              ' I do a little stuff here ..
              MessageBox.Show(aCtrl.GetType.ToString)
    
            End If
          End If
    
        Next

    If the above is not your situation, please post back with the details, such as controls in your panel and a quote of any error message received.

    • Edited by pvdg42 Friday, February 11, 2011 2:55 PM add code
    • Proposed as answer by Aspen VJ Monday, February 14, 2011 6:09 AM
    • Marked as answer by Aspen VJ Monday, February 21, 2011 3:52 AM
    Friday, February 11, 2011 2:40 PM

All replies

  • I am making the following assumptions:

    1. myPanel is a stock VS Panel control, and therefore a container control.

    2. All of the controls in the panel are check boxes.  If they aren't, when you test aCtrl.Checked on a non-checkbox, the value of aCtrl will be null, you should get a null reference exception.

    Beyond that, I see nothing wrong with your approach.

    The only way that I can see that this would be a problem is if the checkboxes that you are attempting to test were added dynamically and not added to the controls collection of the myPanel control.

    When you step through, does it step through the foreach block?  Or does it just bounce out to the next instruction under the block?

    Friday, February 11, 2011 2:29 PM
  •  use like this in c#          

     foreach (Control ctrl in this.panel1.Controls)
                {
                    CheckBox chk = ctrl as CheckBox;
                    if (chk != null)
                        chk.Checked = true;
                }

    in VB check for null, i am not sure for VB syntax

    if aCtrl <> null and aCtrl.Checked Then

     


    Please mark as answer if it is answer for you problem

    Bimbim.in

    bimbim.in

    Friday, February 11, 2011 2:32 PM
  • Did you get this exception?

    "Unable to cast object of type 'System.Windows.Forms.TextBox' to type 'System.Windows.Forms.CheckBox'."

    or something similar?

    Does your Panel instance contain controls other than checkboxes? If so, you'll get an invalid cast exception because of the As Checkbox clause in your For Each.

    Instead, use the GetType method in your loop to determine control type before testing the Checked property.

    Sort of like this:

    For Each aCtrl In MyPanel.Controls
          
          If aCtrl.GetType.ToString = "System.Windows.Forms.CheckBox" Then
    
            If aCtrl.Checked Then
    
              ' I do a little stuff here ..
              MessageBox.Show(aCtrl.GetType.ToString)
    
            End If
          End If
    
        Next

    If the above is not your situation, please post back with the details, such as controls in your panel and a quote of any error message received.

    • Edited by pvdg42 Friday, February 11, 2011 2:55 PM add code
    • Proposed as answer by Aspen VJ Monday, February 14, 2011 6:09 AM
    • Marked as answer by Aspen VJ Monday, February 21, 2011 3:52 AM
    Friday, February 11, 2011 2:40 PM
  • For Each ctrl As Control In myPanel.Controls
       If ctrl.GetType = (New CheckBox).GetType Then     
         If chk.Checked Then
            MessageBox.Show(ctrl.Name)
         End If
       End If
    Next
    
    Friday, February 11, 2011 2:42 PM
  • in VB check for null, i am not sure for VB syntax

    There is no key word 'as' or 'is' in VB.Net for safe casting. From his original post... this looks like VB.Net. There is TryCast

     For Each ctrl As Control In myPanel.Controls
     Dim chk As CheckBox = TryCast(ctrl, CheckBox)
     If Not IsNothing(chk) Then
     If chk.Checked Then
      MessageBox.Show(ctrl.Name)
     End If
     End If
     Next
    Friday, February 11, 2011 2:49 PM