[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 646
  • Last Modified:

.NET Radiobutton Array

I am converting a VB6 application to VB.NET 2013.
this will be more difficult to explain than it is to watch the outcome of the code.

I created a groupbox and put four (4) radio buttons in it. (Radiobutton2 through Radiobutton5)
I have internal variables that I use through out the application,
the operator can change the choices based on the radiobutton they select.

The opportunity is that when I put the radio buttons into an array form, they are always in reverse order.
so my variable bFacility(0) gets assigned to the status of the fourth (4th) radio button (Radiobutton5)

I don't know how to make them the same sequence. I am sure that I will be adding more radiobutton options as the project continues, so I am trying to reduce my upgrade modifications down the road.
  • 6
  • 5
3 Solutions
Éric MoreauSenior .Net ConsultantCommented:
as you know, control array are things of the past in .Net.

Your scenario looks like you could use the Tag property of each radio button to store the index of your related bFacility variable to easily link them together
Adam_930Author Commented:
two (2) points,
first - I am not understanding how to use the tag property, can you provide an example?

second - this may work but any thoughts on why my code did a reverse order as I added radiobuttons?

Éric MoreauSenior .Net ConsultantCommented:
you said:
>>so my variable bFacility(0) gets assigned to the status of the fourth (4th) radio button (Radiobutton5)

when you create this button, you can set its Tag property through the designer or through code:
Radiobutton5.Tag = 0

Then later in your code, if you need to find out to which variable it relates, you can do something like:

>>second - this may work but any thoughts on why my code did a reverse order as I added radiobuttons?
I just don't understand what you mean
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Adam_930Author Commented:
how is bFacility(RadioButton5.Tag) setting bFacility(0) to ether T or F???

If you execute the code as supplied, the four (4) RB in GB2 will populate the bFacility(xLoop) variable.
bFacility(0) is always assigned to RadioButton5.checked
bFacility(1) is always assigned to RadioButton4.checked
bFacility(2) is always assigned to RadioButton3.checked
bFacility(0) is always assigned to RadioButton2.checked
Even though the buttons were added in sequential order (2-5)
Even if I change the cosmetic layout of the RB, it does not change the outcome.
(swap the physical location of RB2 and RB5)

I was expecting that my code would have assigned bFacility(0) to the T/F of status of RadioButton2.checked and so-forth the way they were installed in the GB. I'm just trying to understand what is happening in the loop.
Éric MoreauSenior .Net ConsultantCommented:
I am still not sure. There is no automatic relation between your controls and your variables. You need to write code to bind them together. And the order in which controls is appearing on the screen is absolutely not related to variables. This is why a mechanism like storing the index of the related variable could be useful.
Adam_930Author Commented:
This code seems to work the way I need it too.
This will also allow me to add/remove RB and the code will automatically compensate for the change.

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Me.Label2.Text = ""
        Dim xLoop, iRadioCnt As Integer
        iRadioCnt = 0
        For xLoop = 0 To Me.GroupBox2.Controls.Count - 1
            If TypeOf (Me.GroupBox2.Controls.Item(xLoop)) Is RadioButton Then
                iRadioCnt += 1
            End If
        iRadioCnt -= 1

        For xLoop = 0 To iRadioCnt
            bFacility(iRadioCnt - xLoop) = CType(GroupBox2.Controls.Item(xLoop), RadioButton).Checked
        For xLoop = 0 To iRadioCnt
            Me.Label2.Text = Me.Label2.Text & "bFacility(" & xLoop & ") = " & bFacility(xLoop) & Chr(13) & Chr(10)

    End Sub

I realized that I am still very new to .NET, but I don't understand your comment of
>> as you know, control array are things of the past in .Net
What is considered the proper replacement for what we use to do in VB6???
I am still not understanding how using the TAG allows me to set the value of my variables to either T or F,
unless I use a number of IF - ELSEIF statements. But then future modifications require allot more coding
Éric MoreauSenior .Net ConsultantCommented:
Control arrays are dead in .Net. You have to find workarounds if your logic is based on it.

Check http://msdn.microsoft.com/en-us/library/aa289500(v=vs.71).aspx which btw, uses the .Tag property
Adam_930Author Commented:
Thank you for your patients, I will award you the points for the solution.
for my future info/projects, what would be the proper way doing this in the .NET way if starting from a scratch?

I'm not trying to be argumentative, I am trying to understand the new way.
If I have to change, then so be it, but It gives me the appearance that in the .NET world, arrays are still used/needed, they are just not as convenient as they use to be.
Éric MoreauSenior .Net ConsultantCommented:
Again, when you need to link a control to an array, use the Tag property to store the index in the array
Adam_930Author Commented:
Can you provide a simple sample code???
What I am missing is if I use the tag for the index, what sets the T/F status??
Éric MoreauSenior .Net ConsultantCommented:
the link I gave you provided a sample of stocking an index id in the tag property.

you can store just about anything in the tag property since it is declared as an object but be very careful when you read it

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now