Referencing a Control

xiecsuk

Member
Joined
Apr 24, 2005
Messages
23
Location
Coventry, Warwickshire, UK
Programming Experience
10+
I have an array of controls on a form named txtPlayer11, txtPlayer12, ... I want to set properties of each control in a For ... Next loop using the loop index to create the control name. However, it appears that the Controls collection only takes an integer index, thus Me.Controls(12), rather than Me.Controls("Player1" & intI). Is there any way that I can achieve what I want.
 
You can just use something like:
VB.NET:
For i As Integer = 0 To myControlArray.Length - 1 Step 1
	CType(myControlArray(i), TextBox).Text = <desired text>
Next
If you need to use the control's name, you can access each control on a form via the form's Controls property, like:
VB.NET:
For i As Integer = 1 To 9
	For Each control In Me.Controls
		If control.Name = "txtPlayer" & i Then
			'Set desired property.

			Exit For
		End If
	Next
Next
 
Last edited:
I have done it the way you describe in your second code sample, but it is very cumbersome. I have something like 35 controls on which to change properties. The number varies depending on the number of players in a team.

I will look at your first example, but if I read it right, it is changing all textbox controls, something I don't want to do.

It is possible to do what I want in MS Access.
 
This is the code I am currently using

VB.NET:
 For Each ctrl In Me.Controls 
	If TypeOf ctrl Is TextBox Then
		For intI = 5 To intPlayersPerTeam + 1 Step -1
			For intJ = 1 To 2
				If ctrl.Name = "txtPlayerName" & intJ.ToString & intI.ToString OrElse _
				 ctrl.Name = "txtSex" & intJ.ToString & intI.ToString OrElse _
				 ctrl.Name = "txtIndicator" & intJ.ToString & intI.ToString OrElse _
				 ctrl.Name = "txtGame" & intJ.ToString & intI.ToString & "1" OrElse _
				 ctrl.Name = "txtGame" & intJ.ToString & intI.ToString & "2" OrElse _
				 ctrl.Name = "txtGame" & intJ.ToString & intI.ToString & "3" Then
 
					ctrl.Enabled = False
					ctrl.BackColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(255, Byte), CType(255, Byte))
 
				ElseIf ctrl.Name = "txtHcap" & intJ.ToString & intI.ToString OrElse _
						ctrl.Name = "txtSeriesTotal" & intJ.ToString & intI.ToString OrElse _
						ctrl.Name = "txtPoints" & intJ.ToString & intI.ToString & "1" OrElse _
						ctrl.Name = "txtPoints" & intJ.ToString & intI.ToString & "2" OrElse _
						ctrl.Name = "txtPoints" & intJ.ToString & intI.ToString & "3" Then
 
					ctrl.Enabled = False
 
				End If
			Next intJ
		Next intI
	End If
Next

In the code above, I am changing the background colour of the control. I would like to make them invisible and move the controls below them up a bit using code something like this.

VB.NET:
 For intI = 1 To 2 
	.Controls("txtScratch" & intI.ToString & "1").Location = New Point(1, 2)
	.Controls("txtScratch" & intI.ToString & "2").Location = New Point(1, 2)
	.Controls("txtScratch" & intI.ToString & "3").Location = New Point(1, 2)
	.Controls("txtScratchTotal" & intI.ToString).Location = New Point(1, 2)
	.Controls("txtHandicap" & intI.ToString & "1").Location = New Point(1, 2)
	.Controls("txtHandicap" & intI.ToString & "2").Location = New Point(1, 2)
	.Controls("txtHandicap" & intI.ToString & "3").Location = New Point(1, 2)
	.Controls("txtHandicapTotal" & intI.ToString).Location = New Point(1, 2)
	.Controls("txtGameTotal" & intI.ToString & "1").Location = New Point(1, 2)
	.Controls("txtGameTotal" & intI.ToString & "2").Location = New Point(1, 2)
	.Controls("txtGameTotal" & intI.ToString & "3").Location = New Point(1, 2)
	.Controls("txtSeriesTotal" & intI.ToString).Location = New Point(1, 2)
	.Controls("txtMatchPoints" & intI.ToString & "1").Location = New Point(1, 2)
	.Controls("txtMatchPoints" & intI.ToString & "2").Location = New Point(1, 2)
	.Controls("txtMatchPoints" & intI.ToString & "3").Location = New Point(1, 2)
	.Controls("txtMatchPoints" & intI.ToString & "4").Location = New Point(1, 2)
	.Controls("txtTotalPoints" & intI.ToString).Location = New Point(1, 2)
Next intI

However, I can't reference a control in this way, and using the method above would be very long winded.
 
You should create a GetControlByName() function.
VB.NET:
Private Function GetControlByName(ByVal controlName As String) As Control
	For Each control As Control In Me.Controls
		If control.Name = controlName Then
			Return control
		End If
	Next

	Return Nothing
End Function
or
VB.NET:
Public Function GetControlByName(ByVal form As Form, ByVal controlName As String) As Control
	For Each control As Control In form.Controls
		If control.Name = controlName Then
			Return control
		End If
	Next

	Return Nothing
End Function
 
Last edited:
Without actually trying it, your suggestion looks a brilliant solution.

I just don't understand why the only way to reference a control is with it's index; a number I didn't assign and a number I can't find the value of. Even the IndexOf() method is almost impossible to use. Why haven't they overloaded the Controls() collection to take the name of the control as well as the index. After all, you always know the name of the control you're interested in.
 
Back
Top