CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: Newbie question

  1. #1
    Join Date
    Mar 2001
    Posts
    7

    Newbie question

    Hi,
    I have a combo box that has to application names as choices. I want the user to click a button and when that happens, it reads what is currently displayed in the combo box and launches that application. The path would be something like c:\bsc\bsc.exe to launch the selected application.

    So how do i read in the selected values of a combo box and then launch an application (based on the selected value) with a button click?

    Any examples would be great...I'm just starting.

    Thanks,
    Steve


  2. #2
    Join Date
    Mar 2001
    Location
    Canada
    Posts
    13

    Re: Newbie question

    Try something like this...


    private Sub cmdRun_Click()
    If cmbAppl.ListIndex >= 0 then
    Shell cmbAppl.List(cmbAppl.ListIndex)
    else
    MsgBox "Please select the application"
    End If
    End Sub





  3. #3
    Join Date
    Dec 2000
    Location
    Canada
    Posts
    59

    Re: Newbie question

    Here is the exact code.
    I used it before so it works.
    Say the combo box is named cboApp

    dim AppName as string
    AppName = cboApp.text



    Here is the functionJust send the path and filename to the function)

    Function ShellAndWait(FileName as string)
    Dim objScript
    Dim ShellApp
    on error GoTo ERR_OpenForEdit
    set objScript = CreateObject("WScript.Shell")
    ' true for the Wait.
    ShellApp = objScript.Run(FileName, 1, true)
    ShellAndWait = true
    EXIT_OpenForEdit:
    Exit Function
    ERR_OpenForEdit:
    MsgBox Err.Description
    GoTo EXIT_OpenForEdit
    End Function




    If this function doesn't work replace the
    ShellApp = objScript.Run(FileName, 1, True)
    line with SHELL(filename)

    The shell basically create a DOS session and gives it the command you send it.

    If you have any question e-mail me.
    Raistlin
    Please vote if this is any help



  4. #4
    Join Date
    Mar 2001
    Posts
    7

    Re: Newbie question

    Wow, you guys are quick. I guess i'm starting to get it, you create a shell and feed it the .exe name.
    What is cmbAppl? Is that the name of the combobox? Sorry, but i really am starting out.

    also, how do i specify two differnet application paths? Remember, if they choose one item in the combobox then it goes to one application, if they choose the second then it goes to a different app.

    Thanks,

    Steve


  5. #5
    Join Date
    Dec 2000
    Location
    Canada
    Posts
    59

    Re: Newbie question

    The cmbApp1 is a combo box.
    And what I would do is have hidden variable

    Dim strApp as string 'declare as global(at the top)
    public Sub cmbApp1_change() 'The change event for the combo box
    Select Case cmbApp1.text
    Case "APPLICATIONCHOICE1":
    strApp = "C:\pathtoapp\appname1.exe"
    Case "APPLICATIONCHOICE2":
    strApp = "C:\pathtoapp\appname2.exe"
    End Select
    End Sub





    This should do it. Now when they select the App in the combobox the change event will set the
    global variable strApp to the correct path and you send that to the shell function.


    Raisltin





  6. #6
    Join Date
    Mar 2001
    Posts
    7

    Re: Newbie question

    awesome! now i guess the last piece is how do i make it work from a button click?

    Steve


  7. #7
    Join Date
    Jun 2000
    Location
    Canada
    Posts
    37

    Re: Newbie question

    Just create a button and on the click event
    execute the shell function


    public Sub cmdbutton_click
    Call Shell_Function(strAPP) 'That is it!!
    End Sub




    Just pick a shell function and cut and paste it into your project and create a button next to the combo box and uses the above code.




  8. #8
    Join Date
    Mar 2001
    Posts
    7

    Re: Newbie question

    I keep getting a label undefined error on the first line of the function. Did I forget something?

    Code:

    Dim strApp As String

    Public Sub cmbApp_change() 'The change event for the combo box
    Select Case cmbApp.Text
    Case "SD Development": strApp = "C:\Winnt\notepad.exe"

    Case "SD Production": strApp = "C:\Winnt\notepad.exe"

    End Select
    End Sub



    Private Sub Command1_Click()
    Call ShellAndWait(strApp)
    End Sub

    Private Sub Form_Load()

    cmbApp.AddItem "SD Development"
    cmbApp.AddItem "SD Production"

    End Sub



    Function ShellAndWait(FileName As String)

    Dim objScript
    Dim ShellApp

    On Error GoTo ERR_OpenForEdit

    Set objScript = CreateObject("WScript.Shell")
    ' true for the Wait.
    ShellApp = objScript.Run(FileName, 1, True)
    ShellAndWait = trueEXIT_OpenForEdit:
    Exit Function
    ERR_OpenForEdit: MsgBox Err.Description
    GoTo EXIT_OpenForEdit
    End Function




  9. #9
    Join Date
    Dec 2000
    Location
    Canada
    Posts
    59

    Re: Newbie question

    Try this function:


    Function ShellAndWait(FileName as string)

    Dim objScript
    Dim ShellApp

    on error GoTo ERR_OpenForEdit

    set objScript = CreateObject("WScript.Shell")
    ' true for the Wait.
    ShellApp = objScript.Run(FileName, 1, true)
    ShellAndWait = true
    Exit Function
    ERR_OpenForEdit:
    MsgBox Err.Description
    End Function



    Raistlin






  10. #10
    Join Date
    Mar 2001
    Posts
    7

    Re: Newbie question

    say's invalid procedure call.

    here is all my code.
    ----------------------------------
    Dim strApp As String

    Public Sub cmbApp_change() 'The change event for the combo box
    Select Case cmbApp.Text
    Case "SD Development": strApp = "C:\Winnt\notepad.exe"

    Case "SD Production": strApp = "C:\Winnt\notepad.exe"

    End Select
    End Sub



    Private Sub Command1_Click()
    Call ShellAndWait(strApp)
    End Sub

    Private Sub Form_Load()

    cmbApp.AddItem "SD Development"
    cmbApp.AddItem "SD Production"

    End Sub



    Function ShellAndWait(FileName As String)

    Dim objScript
    Dim ShellApp

    On Error GoTo ERR_OpenForEdit

    Set objScript = CreateObject("WScript.Shell")
    ' true for the Wait.
    ShellApp = objScript.Run(FileName, 1, True)
    ShellAndWait = True

    Exit Function
    ERR_OpenForEdit: MsgBox Err.Description

    End Function
    ----------------------------------
    thanks,

    steve



  11. #11
    Join Date
    Dec 2000
    Location
    Canada
    Posts
    59

    Re: Newbie question

    I have got it.
    Here is the code Just Copy and Paste it into your project. The reason why it wasn't working is
    because the change event did not fire properly. Since you have a command button just do this:




    Dim strApp as string




    private Sub Command1_Click()
    Select Case cmbapp.Text
    Case "SD Development": strApp = "C:\Winnt\notepad.exe"

    Case "SD Production": strApp = "C:\Winnt\notepad.exe"

    End Select
    Call ShellAndWait(strApp)
    End Sub

    private Sub Form_Load()

    cmbapp.AddItem "SD Development"
    cmbapp.AddItem "SD Production"

    End Sub



    Function ShellAndWait(FileName as string)

    Dim objScript
    Dim ShellApp



    set objScript = CreateObject("WScript.Shell")
    ' true for the Wait.
    ShellApp = objScript.Run(FileName, 1, true)
    ShellAndWait = true




    End Function





    Don't forget to vote.
    Thank You
    If you have any more question feel free to ask.
    Raisltin


  12. #12
    Join Date
    Mar 2001
    Posts
    7

    Re: Newbie question

    You done real good! I'm impressed.
    Thanks so much for you help.

    One problem. It does not seem to run an application if the path is to another directory like c:\Program Files\Myapp\myapp.exe.

    any thoughts?

    Steve


  13. #13
    Join Date
    Dec 2000
    Location
    Canada
    Posts
    59

    Re: Newbie question

    That I am confused on.
    It should.
    I tried the app on my machine with a different program and it worked.
    make sure the path is correct by opening a DOS session and type the exact path and filename that you have in your code. If it works then there should be no reason that the app shouldn't work

    Good luck and thanks for your votes.
    Raistlin


  14. #14
    Join Date
    Mar 2001
    Posts
    7

    Re: Newbie question

    Got it. I have to use the DOS equivilant name for Program Files (PROGRA~1) for it to work. It's workin great. Many thanks to all who helped me.

    Steve


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

Featured