Sub Routines in VB.net

Im new to VB.net and programming in general. I really need some help with my sub routines.

Basicall what i have is a list of buttons. Each button takes two textboxes and does something (they all do the same thing).

What I'm trying to do is instead of writing code for each button click... I want to call a sub routine that will do the task. But i need it to someone how take the data from the textboxes (textbox1.text) along with it in order to run the task in the subroutine.

The click button click currently looks like whats below.. There are data from two text boxes that needs to go with it and They are textbox1.text and textbox2.text :
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DM1.Click

        Call MigrateUserData()

    End Sub

Open in new window


And it calls Thissub routine:

    Private Sub MigrateUserData()

        Try
            Dim p As New Process()
            p.StartInfo.FileName = "C:\pstools\psexec.exe"
            p.StartInfo.Arguments = "\\" & TextBox1a.Text & " -s cmd /c c:\userMigration\scanstate.exe c:\%user% /o /all /c /i:migapp.xml /i:miguser.xml /i:migsys.xml /nocompress /localonly"
            p.StartInfo.CreateNoWindow = True
            p.Start()
            RichTextBox1.Text = "User Data Migration Started Sucessfully."
            RichTextBox1.BackColor = Color.LightGreen
        Catch ex As Exception
            RichTextBox1.Text = "Coudn't Not Start User Data Migration. Check if the systems is online."
            RichTextBox1.BackColor = Color.OrangeRed
        End Try
    End Sub

Open in new window


It currently works just fine but only for the static (TextBox1a.Text)  textbox. I think something have to be changed in the sub routine but im not sure what or how..
LVL 1
AnthonySmithMCPAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
funwithdotnetConnect With a Mentor Commented:
You might try something like this:
    Private Sub AnyButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DM1.Click, Button2.Click ' add additional button click events as desired
        Dim myButton as Button = Ctype(sender, Button)
        MigrateUserData(myButton)

    End Sub 

Open in new window


Private Sub MigrateUserData(selectedButton as Button)

        Try

dim val1, val2 as String
Select Case selectedButton.Id 
' pick the textboxes to use (or outputs etc.) based on the button id.
Case "DM1"
val1 = textBox1.Text
val2 = textbox2.Text
Case "Button2
val1 = textBox1.Text
val2 = textbox2.Text
' add additional buttons here
End Select

            Dim p As New Process()
            p.StartInfo.FileName = "C:\pstools\psexec.exe"
            p.StartInfo.Arguments = "\\" & val1  & " -s cmd /c c:\userMigration\scanstate.exe c:\%user% /o /all /c /i:migapp.xml /i:miguser.xml /i:migsys.xml /nocompress /localonly"
' I don't see a textbox2 referenced anywhere
            p.StartInfo.CreateNoWindow = True
            p.Start()
            RichTextBox1.Text = "User Data Migration Started Sucessfully."
            RichTextBox1.BackColor = Color.LightGreen
        Catch ex As Exception
            RichTextBox1.Text = "Coudn't Not Start User Data Migration. Check if the systems is online."
            RichTextBox1.BackColor = Color.OrangeRed
        End Try
    End Sub 

Open in new window


There are many ways to do that. This is just one example.
0
 
CodeCruiserConnect With a Mentor Commented:
Here is your revised method

    Private Sub MigrateUserData(str1 As String)

        Try
            Dim p As New Process()
            p.StartInfo.FileName = "C:\pstools\psexec.exe"
            p.StartInfo.Arguments = "\\" & str1 & " -s cmd /c c:\userMigration\scanstate.exe c:\%user% /o /all /c /i:migapp.xml /i:miguser.xml /i:migsys.xml /nocompress /localonly"
            p.StartInfo.CreateNoWindow = True
            p.Start()
            RichTextBox1.Text = "User Data Migration Started Sucessfully."
            RichTextBox1.BackColor = Color.LightGreen
        Catch ex As Exception
            RichTextBox1.Text = "Coudn't Not Start User Data Migration. Check if the systems is online."
            RichTextBox1.BackColor = Color.OrangeRed
        End Try
    End Sub

Open in new window



Then you can call it by passing textbox value


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DM1.Click
       MigrateUserData(Textbox1.Text)
       MigrateUserData(Textbox2.Text)
       MigrateUserData(Textbox3.Text)
    End Sub

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.