i am tring to pass a veriable to a other sub and then that sub uses that veriable as a textbox name
Public
Sub Casess(ByVal nu) Dim n1, n2, n3 As String Select Case (nu) Case 0n1 = Form1.S631.Name
n2 = Form1.S632.Name
n3 =
""Cal(n1, n2, n3)
Case 1n1 = Form1.S501.Name
n2 = Form1.S502.Name
n3 = Form1.S631.Name
Cal(n1, n2, n3)
This code set the virables as the name of the textbox
Public Sub Cal(ByVal n1, ByVal n2, ByVal n3) If Form1.n1.text >= "0" Or Form1.n2.text >= "0" ThenIf Form1.n1.Text = "" Then
Form1.n2.Text =
"0" ElseIf Form1.S92.Text = "" ThenForm1.S92.Text =
"0" End IfTHis tries to use the veriable as the name
Error 1 'n1' is not a member of 'WindowsApplication1.Form1'. \\matsdc1\matcraig\AddStream\AddStream\Module1.vb 308 12 AddStream
this is the error that i get

how do i use a veriable as a textbox name
DutchChick1234
you can use this function to find the control by using its name.
Private Function getControlFromName(ByVal controlName As String) As System.Windows.Forms.Control
For Each ctl As Control In Me.Controls
If String.Compare(ctl.Name, controlName, True) = 0 Then
Return ctl End If Next ctl Return Nothing End Functioncall this function as
Dim txt as TextBoxtxt = getControlFromName("xyz")
sticksnap
Actually i am using .net v 1.1 and and i cannot the ref to the control by using
Me.Controls(controlname)
i donot know what is in v 2.0
here in v1.1 the Me.Controls(int) takes the parameter as index as int.
so i have written the above function to find the control.
SchallerJe
If you simply passing in the name of the control. The following shows an example where I pass in a string which is the name of the control I wish to update the text property with. This example should give you what you need.
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
foo("textbox1", "contents")
End Sub
Sub foo(ByVal x As String, ByVal content As String)
Me.Controls(x).Text = content
End Sub
End Class
ctzseng
Jesús López
I am trying to it the first way and i am getting a error
Object reference not set to an instance of an object.
on the line
t1 = Form1.Controls(n2).Text
i am passing n1, n2 ,3 from the public sub casess
Public Sub Cal(ByVal n1 As String, ByVal n2 As String, ByVal n3 As String)
Dim t1, t2 As String
Dim s1, s2 As Int64 Dim tot, m1, m2, r1 As Decimalt1 = Form1.Controls(n2).Text
t2 = Form1.Controls(n2).Text
s1 = Form1.TextBox19.Text
s2 = Form1.TextBox20.Text
SValentine
If your trying to return the control for a given name why not simply do
Private Function getControlFromName(ByVal controlName As String) As System.Windows.Forms.Control
Return Me.Controls(controlName)
End Function
Ekart Laszlo
VS express edition
yes
Public
Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickAddStreams()
End Subend class
Module
Module1 Public Sub AddStreams()WeightorPercentage()
End SubPublic
Sub WeightorPercentage() boxs()End Sub
Public
Sub boxs()Casess(nu)
End SubPublic
Sub Casess(ByVal nu) Dim n1, n2, n3 As String Select Case (nu) Case 0n1 = Form1.S631.Name
n2 = Form1.S632.Name
n3 =
""Cal(n1, n2, n3)
Case 1n1 = Form1.S501.Name
n2 = Form1.S502.Name
n3 = Form1.S631.Name
Cal(n1, n2, n3)
end sub
Public Sub Cal(ByVal n1 As String, ByVal n2 As String, ByVal n3 As String) Dim t1, t2 As String Dim s1, s2 As Int64 Dim tot, m1, m2, r1 As Decimalt1 = Form1.Controls(n1).Text
t2 = Form1.Controls(n2).Text
And this is where thge error occurs
form 1 dose contain the textboxs
ejstembler
VS express edition
yes
Public
Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickAddStreams()
End Subend class
Module
Module1 Public Sub AddStreams()WeightorPercentage()
End SubPublic
Sub WeightorPercentage() boxs()End Sub
Public
Sub boxs()Casess(nu)
End SubPublic
Sub Casess(ByVal nu) Dim n1, n2, n3 As String Select Case (nu) Case 0n1 = Form1.S631.Name
n2 = Form1.S632.Name
n3 =
""Cal(n1, n2, n3)
Case 1n1 = Form1.S501.Name
n2 = Form1.S502.Name
n3 = Form1.S631.Name
Cal(n1, n2, n3)
end sub
Public Sub Cal(ByVal n1 As String, ByVal n2 As String, ByVal n3 As String) Dim t1, t2 As String Dim s1, s2 As Int64 Dim tot, m1, m2, r1 As Decimalt1 = Form1.Controls(n1).Text
t2 = Form1.Controls(n2).Text
And this is where thge error occurs
form 1 dose contain the textboxs
David S. Anderson
Which version of the product are you using
Are you sure that the that controls you are passing exist on the form.
Is Form1 the class name or an instance of the class (ie. an Object)
My though would be that Form1 is a class and you havent create and instance and are trying to refer to the class.
bxchan
I am trying to it the first way and i am getting a error
Object reference not set to an instance of an object.
on the line
t1 = Form1.Controls(n2).Text
i am passing n1, n2 ,3 from the public sub casess
Public Sub Cal(ByVal n1 As String, ByVal n2 As String, ByVal n3 As String)
Dim t1, t2 As String
Dim s1, s2 As Int64 Dim tot, m1, m2, r1 As Decimalt1 = Form1.Controls(n2).Text
t2 = Form1.Controls(n2).Text
s1 = Form1.TextBox19.Text
s2 = Form1.TextBox20.Text