I am trying to make a code that loops through 9 text boxes labelled A1 - A9, and places the text of the textboxes to the varibles N1 - N9. This is the code I have got so far but it doesn't seem to work.
For i as integer = 1 to 9
nnumber = "N"+ i.tostring
Solution.Controls("A"+i.tostring).text = nnumber
next
Can someone help me

Looping varibles
Akd02
did you try this:
a1.text = n1
a2.text = n2
a3.text = n3
a4.text = n4
and so on
AnandAsir
Boba_Phatt
This code should give you the basis of what you want.
Put a button and some textboxes called textbox1 .... textbox4 on the form.
Public Class Form1
Private Sub PopulateControls(ByVal StartTextBox As Integer, ByVal MaxTextBox As Integer)
Dim i As Integer = 0
For i = StartTextBox To MaxTextBox
SetTextboxValue("textbox", i, "textvalue" & CType(i, String))
Next i
End Sub
Private Function SplitName(ByVal Name As String) As Integer
'//Splits the Index from the Name
Dim iIndex As Integer
Dim StrIndex As String = ""
For iIndex = 0 To Name.Length - 1
If IsNumeric(Name.Substring(iIndex, 1)) Then
StrIndex = StrIndex & Name.Substring(iIndex, 1)
End If
Next
Return CType(StrIndex, Integer)
End Function
Private Sub SetTextboxValue(ByVal prefixname As String, ByVal Index As Integer, ByVal Value As String)
'//Find the correct TextBox Control with the index in the name and set its value
Dim IIndex As Integer = 0
For Each xControl As Control In Me.Controls
If TypeOf (xControl) Is TextBox And xControl.Name.ToLower.Substring(0, 7) = prefixname Then
IIndex = SplitName(xControl.Name)
If Index = IIndex Then
xControl.Text = Value
End If
End If
Next
End Sub
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'//This will loop through the textbox controls named textbox1.... textbox4
Call PopulateControls(1, 4)
End Sub
End Class
dm14011
Hi,
To simplify this, you need to use the Controls collection and specify the name of your control to be able to have a reference to your control via string.
For i = 1 to 4
Dim txt As TextBox = DirectCast(Controls["txt" + i], TextBox)
txt.Text = i
Next
cheers,
Paul June A. Domag