Solved

Sub Routines in VB.net

Posted on 2013-05-14
2
481 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now