Looping through selective controls

akash.add

Member
Joined
Nov 21, 2012
Messages
5
Programming Experience
Beginner
Hello All,

I am a beginner in VB.NET, so please excuse me if you find my question stupid.:drunk:

There are 4 sets of textboxs in the my Windows form. Their names are like below:
Set 1: HBD_NDT1, HBD_NDT2, HBD_NDT3, HBD_NDT4, HBD_NDT5
Set 2: HBD_NDP1, HBD_NDP2, HBD_NDP3, HBD_NDP4, HBD_NDP5
Set 3: HBD_UDT1, HBD_UDT2, HBD_UDT3, HBD_UDT4, HBD_UDT5
Set 4: HBD_UDP1, HBD_UDP2, HBD_UDP3, HBD_UDP4, HBD_UDP5

In the form I have four check boxes for each set of textboxs defined above. e.g. if user clicks on first check box than all the 5 textboxs of first set shall be enabled & so on...

Earlier, I made this application in MS Excel 2010 VBA editor. At that time i used following codes:

Private Sub Checkbox2_Change() ' this type of sub was defined for all the four checkboxs
Call Set_Design_Box("HBD_UDP", Me.Checkbox2.Value)
End Sub


Public Sub Set_Design_Box(ByVal Box_Name As String, ByVal Status As Boolean)
Dim c As Integer

If Status = True Then
For c = 1 To 5
Me.Controls(Box_Name & c).Enabled = True
Next
Else
For c = 1 To 5
Me.Controls(Box_Name & c).Enabled = False
Next
End If

End Sub

Now, I am currently writing this codes in VBA 2010. Does anyone has idea how i can loop through selective textboxs using loops.

Please do help me.

Thanks in advance.

== Akash
 
If these sets belong to each their own container (a panel/groupbox etc) you can loop through that Controls collection, like:
For Each ctrl In PanelA.Controls ...

Else you can use a variable to group the controls and loop through that, for example
Dim Set1 = {HBD_NDT1, HBD_NDT2, HBD_NDT3, HBD_NDT4, HBD_NDT5}
For Each ctrl In Set1 ...
 
Hi John,

First of all thank you very much for your quick response.

I appreciate your suggestion and that is very useful, however, the solution I am looking for is little different. If I make Set1, Set2,.. than I need to make 5 sets listing Controls of all the set.
But what I am exactly looking for is two variables that togather make name of every control of every set.

e.g. Variable Box_Name shall have values of "HBD_NDT", "HBD_NDP", "HBD_UDT", "HBD_UDP"...
and variable C shall have values of 1,2,3,4,5...
So by calling like (Box_Name & C) in For loops i can cover all the controls...

So by using these two variables, I need not to define 5 sets and listing names of all the text boxs... This is very useful while handling large number of textboxs...

Please suggest me some syntax to implement such kind of logic..

Thanks,
Akash.
 
Well, you then use an array with your strings and loop through that, and a nested loop with the numbers, looking up the control by name as in your first post; Controls(string & number)
 
Please note that the syntax indicated in the first post :Me.Controls(Box_Name & c).Enabled = True was written in VBA editor of Excel 2010.
However, the same thing doesn't work in MS VB 2010 Express. I think there is some change in the syntax..

Please suggest me if there is any other way to use / make the Control names with variables..
 
Hi,

The only thing I can see that you have missed is that your syntax does not take into account the fact that your strings are contained within arrays. Have a look at how this example works using the contents of arrays. I have also used GetType and DirectCast for completeness when interpreting different controls in for loops.

VB.NET:
Dim BoxName() As String = {"TextBox", "TextBox", "TextBox", "TextBox"}
Dim BoxNo() As String = {"1", "2", "3", "4"}
 
For BoxCount As Integer = 0 To BoxName.Count - 1
  For BoxNoCount As Integer = 0 To BoxNo.Count - 1
    If Me.Controls(BoxName(BoxCount) & BoxNo(BoxNoCount)).GetType = GetType(TextBox) Then
      Dim myTextBox As TextBox = DirectCast(Me.Controls(BoxName(BoxCount) & BoxNo(BoxNoCount)), TextBox)
      'If myTextBox.Name = "CheckMyConditionsHere" Then
      myTextBox.Enabled = False
      'End If
    End If
  Next
Next
Hope that helps.

Cheers,

Ian
 
Hi Ian,

Thanks for sharing your knowledge..

I used your codes in my application. I used {"HBD_NDT", "HBD_NDP", "HBD_UDT", "HBD_UDP"} instead of {"TextBox", "TextBox", "TextBox", "TextBox"} as former is my textbox name prefix...
However, during run time I am getting an error "NullReferenceException was unhandled" , "Object reference not set to an instance of an object." on following line:
If Me.Controls(BoxName(BoxCount) & BoxNo(BoxNoCount)).GetType = GetType(TextBox) Then
Please throw some light on this.

I am very sorry that I am bothering you a lot... :-(

= Akash
 
Hi,

The loops are obviously creating the name of an object which does not exist in the controls collection that you are working with. Check that the BoxNo array only has the strings 1 to 5. If that is OK then give this a quick try to see which object is incorrect:-

Add this line MsgBox(BoxName(BoxCount) & BoxNo(BoxNoCount)) just before the IF statement. It will tell you the name of each object which is about to be referenced. You should be able to tell from here what has gone wrong. You can then check your textbox field names to make sure you have not misspelled them. Don't forget to remove this line when you are done.

Cheers,

Ian
 
Please note that the syntax indicated in the first post :Me.Controls(Box_Name & c).Enabled = True was written in VBA editor of Excel 2010.
However, the same thing doesn't work in MS VB 2010 Express. I think there is some change in the syntax..
That is perfectly valid in VB.Net also, if there is a control by that name in that Controls collection the returned object is of type Control, which has the Enabled property.
There is no need to check the type of the object and cast to a more specific type as in IanRyders code.
 
You can also use LINQ. Example for the first set:

VB.NET:
For Each C As TextBox In Me.Controls.OfType(Of TextBox).Where(Function(ctr) ctr.Name.Contains("BD_NDT"))
    C.Enabled = True
Next

This assumes that these textbox controls in that set are a child control within the main form itself as the parent container.
 
Back
Top