sohsiteadmin
sohsiteadmin

Reputation: 17

VB.Net Changing the value in a different text boxes with 1 function based on name

Ok. I have done this in Php with a function before. I just don't know how to do it in VB.

I have a character sheet creator for a game I am making. What I am wanting to do is write a single function, where in you click on a button and it will pass the text box name (so that it knows which text box to edit), as well as the value inside of the text box (to make sure that the new character stat ceiling is taken into account).

In my mind, the psuedo code behind the button would look something like

Private Sub Button_Click
     AddtoValue(Textbox1,stats)
End Sub

Public Sub AddtoValue (NoIdea,NoIdea2)
     dim ObjectName as Object, ObjectText as string, Textasnum as integer

     Objectname = frmmain.noidea
     ObjectText = objectname.text

     textasnum = convert.toint32(objecttext)
     objecttext = textasnum

     [If Statements to check value of objectname for rules validation, and then noidea2 to figure out which point pool to take from and change if passes rules validation]
End Sub

Essentially, I need to know what to put for noidea, and noidea2, like, byval or byref or what?

Upvotes: 0

Views: 3968

Answers (3)

competent_tech
competent_tech

Reputation: 44971

Based on the clarified comments, you should be able to pass and access the textboxes directly.

Here is what your AddToValue method should look like:

Public Sub AddtoValue (theTextBox As TextBox, theStatsBox As TextBox)
     dim Textasnum as integer

     If IsNumeric(theTextBox.Text) Then
         textasnum = convert.toint32(theTextBox.Text)
     Else
         textasnum = 0
     End If

     [If Statements to check value of objectname for rules validation, 
      and then noidea2 to figure out which point pool to take from and 
      change if passes rules validation]
End Sub

Upvotes: 0

John Woo
John Woo

Reputation: 263943

Create a procedure something like this:

Private Sub ProcedureName(ByVal NameOFTextBox as String, _
                          ByVal ValueToAssign as String)
     Dim xTextBox as TextBox = DirectCast(Me.Controls(NameOfTextBox), TextBox)
     xTextBox.Text = ValueToAssign
End Sub

Usage:

ProcedureName("txtBoxName", "NewValue")

UPDATE:

Private Sub ProcedureName(ByVal CtrlName As String, ByVal NewVal As String)
    Dim xCtrl() As Control = Controls.Find(CtrlName, True)
    For Each iControl As Control In xCtrl
        If iControl.Name = CtrlName Then
            Dim xTxt As TextBox = DirectCast(iControl, TextBox)
            xTxt.Text = NewVal
        End If
    Next
End Sub

Upvotes: 2

Gopal Nair
Gopal Nair

Reputation: 840

In VB.Net, it is very much possible to send objects to function calls using reference.

Hence, your sub signature will look something like this:

Public Sub AddtoValue (Byref textboxObj as TextBox, Byval stat as String)

    textboxObj.text = stat

End Sub

Upvotes: 1

Related Questions