Solved

Sub Routines in VB.net

Posted on 2013-05-14
2
503 Views
Last Modified: 2013-05-20
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..
0
Comment
Question by:AnthonySmithMCP
2 Comments
 
LVL 12

Accepted Solution

by:
funwithdotnet earned 250 total points
ID: 39166949
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
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 250 total points
ID: 39167486
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

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question