Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Sub Routines in VB.net

Posted on 2013-05-14
2
Medium Priority
?
512 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 1000 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 1000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…

564 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