Solved

Sub Routines in VB.net

Posted on 2013-05-14
2
506 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access Schema 6 45
Developing a front end to SPLUNK 1 65
TFS 2015 Access denied. 1 30
VB.net/VSTO Excel Add-in 2 19
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…

730 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