none
Writing an array of objects RRS feed

  • Question

  • I have forms with many objects of same type (checkboxes).  I have to write                                                                          unit(0)=checkbox1, unit(1)=checkbox2, .....    unit(n)=checkboxn to manage them.                                                                Is there a shorter way to fill the array unit()?

    Tuesday, February 21, 2017 10:39 PM

Answers

  • Enzo,

    Why do you need an array of controls?

    Me.Controls (in a WinForm form) is a collection of all the controls on that form.


    "One who has no vices also has no virtues..."

    Tuesday, February 21, 2017 11:27 PM
  • Pseudo code:

    For i = 0 to n
       unit(i) = Controls.Item("Checkbox" & (i + 1).ToString).Checked
    Next
    This assumes that the code is running on the form with the controls.  If the controls are in a panel or other container, reference that object before the "Controls" property in the code above.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Tuesday, February 21, 2017 11:47 PM
    Moderator
  • Hello,

    Button1 gets all CheckBox controls on the form but not in containers such as Panels while Button2 gets them all. Button3 gets all of them that are checked.

    Public Class Form3
        Private checkBoxArray As CheckBox()
        ''' <summary>
        ''' get all check box controls on the form canvas
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            checkBoxArray = (From ctr In Controls.OfType(Of CheckBox) Select ctr).ToArray
        End Sub
        ''' <summary>
        ''' get all check box controls any where on the form e.g. in group boxes
        ''' and panels for instance.
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            checkBoxArray = GetAll(Me, GetType(CheckBox)).OfType(Of CheckBox).ToArray
        End Sub
        Public Function GetAll(ByVal control As Control, ByVal type As Type) As IEnumerable(Of Control)
            Dim controls = control.Controls.Cast(Of Control)()
            Return controls.SelectMany(Function(ctrl) GetAll(ctrl, type)) _
                .Concat(controls).Where(Function(c) c.GetType() Is type)
        End Function
        ''' <summary>
        ''' Get all checked only
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            checkBoxArray = GetAll(Me, GetType(CheckBox)).OfType(Of CheckBox).Where(Function(cb) cb.Checked).ToArray
        End Sub
    End Class

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Wednesday, February 22, 2017 12:03 AM
    Moderator

All replies

  • Enzo,

    Why do you need an array of controls?

    Me.Controls (in a WinForm form) is a collection of all the controls on that form.


    "One who has no vices also has no virtues..."

    Tuesday, February 21, 2017 11:27 PM
  • Pseudo code:

    For i = 0 to n
       unit(i) = Controls.Item("Checkbox" & (i + 1).ToString).Checked
    Next
    This assumes that the code is running on the form with the controls.  If the controls are in a panel or other container, reference that object before the "Controls" property in the code above.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Tuesday, February 21, 2017 11:47 PM
    Moderator
  • Hello,

    Button1 gets all CheckBox controls on the form but not in containers such as Panels while Button2 gets them all. Button3 gets all of them that are checked.

    Public Class Form3
        Private checkBoxArray As CheckBox()
        ''' <summary>
        ''' get all check box controls on the form canvas
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            checkBoxArray = (From ctr In Controls.OfType(Of CheckBox) Select ctr).ToArray
        End Sub
        ''' <summary>
        ''' get all check box controls any where on the form e.g. in group boxes
        ''' and panels for instance.
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            checkBoxArray = GetAll(Me, GetType(CheckBox)).OfType(Of CheckBox).ToArray
        End Sub
        Public Function GetAll(ByVal control As Control, ByVal type As Type) As IEnumerable(Of Control)
            Dim controls = control.Controls.Cast(Of Control)()
            Return controls.SelectMany(Function(ctrl) GetAll(ctrl, type)) _
                .Concat(controls).Where(Function(c) c.GetType() Is type)
        End Function
        ''' <summary>
        ''' Get all checked only
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            checkBoxArray = GetAll(Me, GetType(CheckBox)).OfType(Of CheckBox).Where(Function(cb) cb.Checked).ToArray
        End Sub
    End Class

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Wednesday, February 22, 2017 12:03 AM
    Moderator