|
-
Jan 24th, 2011, 03:18 PM
#1
Thread Starter
Hyperactive Member
Accessibg the controls of a form
Lately I am having a problem accessing the controls of a form . I was used to use the following code back in VB6 :
For Each GenericControl In Me.Controls
If TypeOf GenericControl Is System.Windows.Forms.Button And Mid(GenericControl.Name, 14, 11) = "COEFICIENT_" Then
GenericControl.Visible = False
End If
Next GenericControl
in which GenericControl has been defined in a Module as :
Public GenericControl As Object
However I found out that the above code , now in .NET , misses some controls . For example if the buttons I am looking for are located in a group box , then the above code won't catch them . I used the following code :
For Each GenericControl In Me.GroupBox1
and it worked fine , but the problem is that on the form there are plenty of group boxes . Moreover , the same thing happens in many forms , therefore I wonder if there is any possibility to catch all the controls on a form , no matter if they are within a group box or not .
In case there isn't , then is it possible to reduce the lines of code by making a search within all group boxes of the form ?
-
Jan 24th, 2011, 03:50 PM
#2
Re: Accessibg the controls of a form
Check the attached project out which has three GroupBoxes, Checkboxes and TextBoxes in GroupBoxes and on the main form canvas. The extension methods GetAllCheckBoxes and GetAllCheckBoxes are demo'd. Others included,
GetAllComboBoxes
GetAllRadioButtons
So if this is of help you could use the above to create more of the specific methods or use the core functions included also.
Code:
Private Sub Form1_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim ListOfAllCheckBoxes As New List(Of CheckBox)
' Me is implied below as in Me.GetAllCheckBoxes()
ListOfAllCheckBoxes = GetAllCheckBoxes()
For Each cb In ListOfAllCheckBoxes
Console.WriteLine("name {0} checked {1} | Parent {2}", _
cb.Name, cb.Checked, cb.Parent.Name)
Next
Console.WriteLine()
Console.WriteLine("Textboxes")
Dim ListOfAllTextBoxes = Me.GetAllTextBoxes
For Each tb In ListOfAllTextBoxes
Console.WriteLine("{2}.{0}.Text = {1}", _
tb.Name, tb.Text, tb.Parent.Name)
Next
End Sub
-
Jan 24th, 2011, 03:51 PM
#3
Thread Starter
Hyperactive Member
Re: Accessibg the controls of a form
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|