Using Variable Control Name

  • Thread starter Thread starter B-Dog
  • Start date Start date
B

B-Dog

I'm capturing the checked radio button to XML file using the name of the
radio button. I want to read my xml file to find which button was checked
on close and the check the appropriate button when for loads. How do I use
a variable control name. This is what I'm trying to do below but of course
it doesn't work, I'm rookie.

Thanks


Dim rbchecked as String
'rbchecked is name of radio button checked on exit from XML

rbchecked.checked = true
 
Hi,

This will check every control on the form. If the control is a
radiobutton it will write its name and if it is checked to the output
window.

RadioButtonChecked(Me.Controls)

Private Sub RadioButtonChecked(ByVal sender As
Control.ControlCollection)
For Each ctrl As Control In sender
If TypeOf ctrl Is RadioButton Then
Debug.WriteLine(String.Format("{0} Checked {1}",
ctrl.Name, DirectCast(ctrl, RadioButton).Checked))
End If
RadioButtonChecked(ctrl.Controls)
Next
End Sub

Ken
-----------------------
 
* "B-Dog said:
I'm capturing the checked radio button to XML file using the name of the
radio button. I want to read my xml file to find which button was checked
on close and the check the appropriate button when for loads. How do I use
a variable control name.

My FAQ:

\\\
Private Function FindControl( _
ByVal ControlName As String, _
ByVal CurrentControl As Control _
) As Control
Dim ctr As Control
For Each ctr In CurrentControl.Controls
If ctr.Name = ControlName Then
Return ctr
Else
ctr = FindControl(ControlName, ctr)
If Not ctr Is Nothing Then
Return ctr
End If
End If
Next ctr
End Function
///

Usage:

\\\
DirectCast(FindControl("btnBla", Me), Button).Enabled = False
///

Notice that the procedure listed above is "slow", if you have to access a
lot of controls by name very often, you should store references to them in a
'Hashtable' object. You can use the name of the control as key:

\\\
Private m_Controls As New Hashtable()
///

Adding a control:

\\\
Dim DynamicPictureBox As New PictureBox()
DynamicPictureBox.Name = "PictureBox1"
m_Controls.Add(DynamicPictureBox.Name, DynamicPictureBox)
///

Looking for a control:

\\\
Dim p As PictureBox = DirectCast(m_Controls.Item("PictureBox1"), PictureBox)
///

Removing a control:

\\\
m_Controls.Remove("PictureBox1")
///

Sometimes it's even better to add the control to an array. This will allow
fast and easy index-based access to the control references:

\\\
Dim MyLabels() As Label = {Label1, Label2, ..., Label10}
///

Access by 'MyLabels(0)' to 'MyLabels(9)'.

Control arrays:

Control arrays, as known from VB6, are not included in VB.NET 2002/2003.

Creating Control Arrays in Visual Basic .NET and Visual C# .NET:
<URL:http://msdn.microsoft.com/library/e...ngControlArraysInVisualBasicNETVisualCNET.asp>

WinForms Controls--Creating Control Arrays in VB.NET
<URL:http://www.devx.com/vb2themax/Article/19907/>
 
Wow, thanks for the reply, looks more complicated than I thought it would
be, to get around it before I had used and Case, If string = "" then
radio.checked = true etc. which was only a few lines. May stick with that
since I only have a few radio buttons. Thanks!
 
Back
Top