(Easy???) Looping thru Form Objects with a counter???


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: (Easy???) Looping thru Form Objects with a counter???

  1. #1
    Lars Netzel Guest

    (Easy???) Looping thru Form Objects with a counter???

    I run VB.NET and I am trying to create a kinda simple program where i just
    check a bunch of values in CheckBoxes and Radiobuttons and then Write the
    values to a textfile

    I have a form1 with 16 GroupBoxes, in each GroupBox I have 3 Radiobuttons.
    The RadioButtons are Named rad16th1, rad16th2, rad16th3, rad16th4.. etc...

    I want to loop thru the Radiobuttons and get the values for them but I can't
    make it...

    Last thing I tried I did this.

    Dim ctl as Control
    For each ctl in Me.Controls
    If Mid$(ctl.Name, 1, 7) = "rad16th" then MsgBox ctl.Name & " " &
    ctl.checked
    Next

    But that did'nt even find the Radiobutton.. and I gues sit's because they
    are located within the Groupboxes?

    Am I totally wrong in my thinking?
    Please help!
    _____________________________________________________________________
    Lars Netzel, SystemDeveloper , Sigma E-handel Inc, Sweden




  2. #2
    Michael Culley Guest

    Re: (Easy???) Looping thru Form Objects with a counter???

    Lars,

    Someone posted some code about a week ago that solved this problem, making
    it possible to do something like this:

    For each control in New AllControlsEnumeration (Me)

    I'm sure it was either here or in vbdotnet.discussions.

    BTW, you are probably better doing a "If TypeOf ctl is RadioButton"

    --
    Michael Culley
    www.vbdotcom.com



    "Lars Netzel" <lars.netzel@ehandel.sigma.se> wrote in message
    news:3cc53f24@10.1.10.29...
    > I run VB.NET and I am trying to create a kinda simple program where i just
    > check a bunch of values in CheckBoxes and Radiobuttons and then Write the
    > values to a textfile
    >
    > I have a form1 with 16 GroupBoxes, in each GroupBox I have 3 Radiobuttons.
    > The RadioButtons are Named rad16th1, rad16th2, rad16th3, rad16th4.. etc...
    >
    > I want to loop thru the Radiobuttons and get the values for them but I

    can't
    > make it...
    >
    > Last thing I tried I did this.
    >
    > Dim ctl as Control
    > For each ctl in Me.Controls
    > If Mid$(ctl.Name, 1, 7) = "rad16th" then MsgBox ctl.Name & " " &
    > ctl.checked
    > Next
    >
    > But that did'nt even find the Radiobutton.. and I gues sit's because they
    > are located within the Groupboxes?
    >
    > Am I totally wrong in my thinking?
    > Please help!
    > _____________________________________________________________________
    > Lars Netzel, SystemDeveloper , Sigma E-handel Inc, Sweden
    >
    >
    >




  3. #3
    Cecilio Thomas Guest

    Re: (Easy???) Looping thru Form Objects with a counter???

    Lars,

    I think the reason that your code did not work is because of your use of the
    "ctl.Name" property. The "Name" property actually returns the name of the
    type of Conrol that is actually being used. For instance if the control was
    a button, the "Name" property would return "button". In your case the
    "Name" property is actually returning "radio button" or something along
    those lines. In order for your code to work as is, you would have to
    substring the "Text" property of the control that will give you the name
    that you have assigned it in the IDE.


    Cecilio (Tony) Thomas


    "Lars Netzel" <lars.netzel@ehandel.sigma.se> wrote in message
    news:3cc53f24@10.1.10.29...
    > I run VB.NET and I am trying to create a kinda simple program where i just
    > check a bunch of values in CheckBoxes and Radiobuttons and then Write the
    > values to a textfile
    >
    > I have a form1 with 16 GroupBoxes, in each GroupBox I have 3 Radiobuttons.
    > The RadioButtons are Named rad16th1, rad16th2, rad16th3, rad16th4.. etc...
    >
    > I want to loop thru the Radiobuttons and get the values for them but I

    can't
    > make it...
    >
    > Last thing I tried I did this.
    >
    > Dim ctl as Control
    > For each ctl in Me.Controls
    > If Mid$(ctl.Name, 1, 7) = "rad16th" then MsgBox ctl.Name & " " &
    > ctl.checked
    > Next
    >
    > But that did'nt even find the Radiobutton.. and I gues sit's because they
    > are located within the Groupboxes?
    >
    > Am I totally wrong in my thinking?
    > Please help!
    > _____________________________________________________________________
    > Lars Netzel, SystemDeveloper , Sigma E-handel Inc, Sweden
    >
    >
    >




  4. #4
    Kathleen Dollard Guest

    Re: (Easy???) Looping thru Form Objects with a counter???

    Tony,

    > I think the reason that your code did not work is because of your use of

    the
    > "ctl.Name" property. The "Name" property actually returns the name of the
    > type of Conrol that is actually being used. For instance if the control

    was
    > a button, the "Name" property would return "button". In your case the
    > "Name" property is actually returning "radio button" or something along
    > those lines. In order for your code to work as is, you would have to
    > substring the "Text" property of the control that will give you the name
    > that you have assigned it in the IDE.


    The way I read what you are saying, it is incorrect. If you believe it to be
    correct can you post code illustrating what you mean?

    Kathleen



  5. #5
    Paul Mc Guest

    Re: (Easy???) Looping thru Form Objects with a counter???


    G'day.

    >> "ctl.Name" property. The "Name" property actually returns the name of
    >> the type of Conrol that is actually being used. For instance if the
    >> control was a button, the "Name" property would return "button".


    >The way I read what you are saying, it is incorrect.


    It is incorrect the way I read it, too. The following code works as expected
    (ie prints the name of the control, not the typename):

    Private Sub EnumControls(ByRef Parent As Windows.Forms.Control, Optional
    ByVal ControlType As System.Type = Nothing)

    Dim ctl As Control
    For Each ctl In Parent.Controls

    If Not ControlType Is Nothing Then
    If ControlType.IsInstanceOfType(ctl) Then
    Me.TextBox1.Text = Me.TextBox1.Text & ctl.Name
    End If
    Else
    Me.TextBox1.Text = Me.TextBox1.Text & ctl.Name
    End If
    If ctl.Controls.Count > 0 Then
    EnumControls(ctl, ControlType)
    End If
    Next

    End Sub


  6. #6
    Cecilio Thomas Guest

    Re: (Easy???) Looping thru Form Objects with a counter???

    I apologize you are all absolutely correct. Let me explain what my thinking
    was. First off, I read more into the code than I should have. I assumed
    that the code displayed would probably be triggered by some event, in which
    case the parameters we would actually be getting would be the sender whom
    initiated the event, and our EventArgs (recommended parameters for an event
    handler). In this case before actually getting the name of the actual
    object that initiated the event, we first have to cast it to an appropriate
    type at which time we can then use the Name property to properly give us the
    controls name. As far as indicating that the "Text" property of the control
    actually returns the name of the control, I'm not sure what I was thinking
    when I wrote that. An example of what I'm talking about is:

    sender.GetType().Name: Returns the type of the object (Button, Form, etc..)

    Ctype(sender,Control).Name: The name that that user has assigned the
    control

    I appreciate you all not letting improper information go uncheck. Let me
    know if my thinking is now correct.

    Thanks,
    Cecilio (Tony) Thomas



    "Paul Mc" <paulmc@nospam.thehub.com.au> wrote in message
    news:3cd8e7f7$1@10.1.10.29...
    >
    > G'day.
    >
    > >> "ctl.Name" property. The "Name" property actually returns the name of
    > >> the type of Conrol that is actually being used. For instance if the
    > >> control was a button, the "Name" property would return "button".

    >
    > >The way I read what you are saying, it is incorrect.

    >
    > It is incorrect the way I read it, too. The following code works as

    expected
    > (ie prints the name of the control, not the typename):
    >
    > Private Sub EnumControls(ByRef Parent As Windows.Forms.Control, Optional
    > ByVal ControlType As System.Type = Nothing)
    >
    > Dim ctl As Control
    > For Each ctl In Parent.Controls
    >
    > If Not ControlType Is Nothing Then
    > If ControlType.IsInstanceOfType(ctl) Then
    > Me.TextBox1.Text = Me.TextBox1.Text & ctl.Name
    > End If
    > Else
    > Me.TextBox1.Text = Me.TextBox1.Text & ctl.Name
    > End If
    > If ctl.Controls.Count > 0 Then
    > EnumControls(ctl, ControlType)
    > End If
    > Next
    >
    > End Sub
    >




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center