Results 1 to 18 of 18

Thread: Use Run Task in Background

Hybrid View

  1. #1
    Addicted Member
    Join Date
    Jan 2013
    Posts
    144

    Re: Use Run Task in Background

    It's almost the same, just put the code I gave you in the SelectedIndexChanged of the ListBox and you're fine. Something like this: (change the listbox items names to your actual names)

    Code:
    Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
            Dim UserCmd As String = ListBox1.Text
            Select Case UserCmd
                Case "Command Prompt"
                    Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.System) & "\cmd.exe")
                Case "System Configuration"
                    Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.System) & "\msconfig.exe")
                Case "System Information"
                    Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.System) & "\msinfo32.exe")
                Case "Documents"
                    Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments))
                Case "Drive C:\"
                    Process.Start("C:\")
                Case "Management Console"
                    Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.System) & "\wmimgmt.msc")
            End Select
        End Sub
    Now your user just need to click "System Configuration" item and msconfig will run.

    Hope that helps

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Use Run Task in Background

    Quote Originally Posted by cPubis View Post
    It's almost the same, just put the code I gave you in the SelectedIndexChanged of the ListBox and you're fine.
    That's really not a good idea. I know that it addresses the OP's request but the request is bad. What if the user has already selected the first item in the list and they want to selected the last item using the keyboard? A developer should never assume that the mouse will always be used for something that the keyboard can be used for too. In that case, your code would execute every single command when the user only wants the last one.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Addicted Member
    Join Date
    Jan 2013
    Posts
    144

    Re: Use Run Task in Background

    Quote Originally Posted by jmcilhinney View Post
    A developer should never assume that the mouse will always be used for something that the keyboard can be used for too.
    I agree, but as you said it was the OP's request.

    Now kshadow22, in case you didn't figure out how to avoid this, here you go:

    Code:
    Public Class Form1
    
        Private Sub ListBox1_DoubleClick(sender As Object, e As System.EventArgs) Handles ListBox1.DoubleClick
            RunTool()
        End Sub
    
        Private Sub ListBox1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles ListBox1.KeyDown
            If e.KeyCode = Keys.Enter Then
                RunTool()
            End If
        End Sub
    
        Private Sub RunTool()
            Dim UserCmd As String = ListBox1.Text
            Select Case UserCmd
                Case "Command Prompt"
                    Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.System) & "\cmd.exe")
                Case "System Configration"
                    Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.System) & "\msconfig.exe")
                Case "System Information"
                    Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.System) & "\msinfo32.exe")
                Case "Documents"
                    Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments))
                Case "Drive C:\"
                    Process.Start("C:\")
                Case "Managment Console"
                    Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.System) & "\wmimgmt.msc")
            End Select
        End Sub
    End Class
    Now you can select the item and press Enter or just double click the item.

    Hope that helps.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Use Run Task in Background

    Quote Originally Posted by cPubis View Post
    I agree, but as you said it was the OP's request.
    I would suggest we should point out to the OP the shortcomings of their request even if we provide a solution. It would be far from ideal if they just accepted that solution, implemented it, deployed it and then had users complaining that it didn't work.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Addicted Member
    Join Date
    Jan 2013
    Posts
    144

    Re: Use Run Task in Background

    Quote Originally Posted by jmcilhinney View Post
    I would suggest we should point out to the OP the shortcomings of their request even if we provide a solution. It would be far from ideal if they just accepted that solution, implemented it, deployed it and then had users complaining that it didn't work.
    I unreservedly agree with that.

  6. #6
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: Use Run Task in Background

    Quote Originally Posted by cPubis View Post
    I unreservedly agree with that.
    That said, you should be teaching the OP how to correctly build paths.
    My Github - 1d3nt

  7. #7
    Addicted Member
    Join Date
    Jan 2013
    Posts
    144

    Re: Use Run Task in Background

    Quote Originally Posted by ident View Post
    That said, you should be teaching the OP how to correctly build paths.
    In this very thread, jmc preceded me to that, and he did it professionally as usual. Repeating what he said was unnecessary. If you looked at my first reply to the OP you will see that it's just a practical example of what jmc said in his first reply to the OP.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width