how do i use a veriable as a textbox name

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 0

n1 = Form1.S631.Name

n2 = Form1.S632.Name

n3 = ""

Cal(n1, n2, n3)

Case 1

n1 = 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" Then

If Form1.n1.Text = "" Then

Form1.n2.Text = "0"

ElseIf Form1.S92.Text = "" Then

Form1.S92.Text = "0"

End If

THis 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



Answer this question

how do i use a veriable as a textbox name

  • chutachut

    VS express edition

    yes

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    AddStreams()

    End Sub

    end class

    Module Module1

    Public Sub AddStreams()

    WeightorPercentage()

    End Sub

    Public Sub WeightorPercentage()

    boxs()

    End Sub

    Public Sub boxs()

    Casess(nu)

    End Sub

    Public Sub Casess(ByVal nu)

    Dim n1, n2, n3 As String

    Select Case (nu)

    Case 0

    n1 = Form1.S631.Name

    n2 = Form1.S632.Name

    n3 = ""

    Cal(n1, n2, n3)

    Case 1

    n1 = 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 Decimal

    t1 = Form1.Controls(n1).Text

    t2 = Form1.Controls(n2).Text

    And this is where thge error occurs

    form 1 dose contain the textboxs


  • Eric66

    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


  • Stick94794

    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.


  • BVark

    VS express edition

    yes

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    AddStreams()

    End Sub

    end class

    Module Module1

    Public Sub AddStreams()

    WeightorPercentage()

    End Sub

    Public Sub WeightorPercentage()

    boxs()

    End Sub

    Public Sub boxs()

    Casess(nu)

    End Sub

    Public Sub Casess(ByVal nu)

    Dim n1, n2, n3 As String

    Select Case (nu)

    Case 0

    n1 = Form1.S631.Name

    n2 = Form1.S632.Name

    n3 = ""

    Cal(n1, n2, n3)

    Case 1

    n1 = 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 Decimal

    t1 = Form1.Controls(n1).Text

    t2 = Form1.Controls(n2).Text

    And this is where thge error occurs

    form 1 dose contain the textboxs


  • itsdzs

    Hi, in your code the function cal() is outside the from, then how should it get the reference of Form1. you have to pass the reference of the form1. to the function.

  • xochi

    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 Decimal

    t1 = Form1.Controls(n2).Text

    t2 = Form1.Controls(n2).Text

    s1 = Form1.TextBox19.Text

    s2 = Form1.TextBox20.Text


  • IvanBi

    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.



  • cdaviduik

    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


  • tanzirmusabbir

    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 Decimal

    t1 = Form1.Controls(n2).Text

    t2 = Form1.Controls(n2).Text

    s1 = Form1.TextBox19.Text

    s2 = Form1.TextBox20.Text


  • _Alex_

    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 Function

    call this function as

    Dim txt as TextBox

    txt = getControlFromName("xyz")



  • how do i use a veriable as a textbox name