Process start app from listview VB.net 2003

Hi Experts,

I want to create a app which will centralize all my apps in a listview on a winform. I originally started by creating a checkbox(button) on a winform... when click (checked=Y) it opens an app.  If clicked again (checked = N) it closes the app....

Simples:

But I stumbled across a problem when I wanted to launch CMD.exe from another BUTTON.... as you can see by the code snippet, the path is correct, but I think it must be sharing the process sequence - am I right?

(I've changed my design from a button to a listview!)

I wish my listview to resemble 'windows services' - see picture attached.

I will manually create column headers and populate the apps I wish for... but I'll need text change in (status) for example 'started/stopped' & of course a process app launch.

Thanks,
Roberto
Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
        If CheckBox2.Checked = True Then
            Process.Start("C:\Program Files\CA\DSM\bin\gui_rcLaunch.exe")
            Label2.Text = "Running"
        Else
            Dim pProcess() As Process = System.Diagnostics.Process.GetProcessesByName("EGC30N")

            For Each p As Process In pProcess
                p.Kill()
            Next
            Label2.Text = "Closed"
        End If
   
    End Sub

  
    Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged
        If CheckBox2.Checked = True Then
            Process1.Start("C:\WINDOWS\system32\cmd.exe")
            Label3.Text = "Running"
        Else
            Dim p1Process() As Process = System.Diagnostics.Process.GetProcessesByName("cmd.exe")

            For Each p1 As Process In p1Process
                p1.Kill()
            Next
            Label3.Text = "Closed"
        End If
    End Sub

Open in new window

vbvb-services.bmp
RobertoFreemanoAsked:
Who is Participating?
 
nepaluzConnect With a Mentor Commented:
Apologies, I thought I'd posted a reply (must have had communication issues).

Anyway, I am STUMPED! I have NOT developed in VS2003 and am really puzzled where the events have gone / reside.

PS. Does your designer have a ContextMenuStrip as oppossed to a ContextMenu? If so, add this and remove the other.

can you try pasting the code again now that you have not declared the menu twice?

Private Sub ContextMenuStrip1_ItemClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles ContextMenuStrip1.ItemClicked
        Select Case e.ClickedItem.Text
            Case "Start"
                'Process.Start("C:\Program Files\CA\DSM\bin\gui_rcLaunch.exe")
                Me.ListView1.FocusedItem.SubItems.Item(1).Text = "Running"
            Case "Stop"
                'here as above you run your kill code
                Me.ListView1.FocusedItem.SubItems.Item(1).Text = "Idle"
            Case Else
        End Select
 End Sub
Private Sub ContextMenuStrip1_Opening(ByVal sender As ContextMenuStrip, ByVal e As System.ComponentModel.CancelEventArgs) Handles ContextMenuStrip1.Opening
    ContextMenuStrip1.Items.Clear()
    If Me.ListView1.FocusedItem.SubItems.Item(1).Text = "Running" Then
        ContextMenuStrip1.Items.Add("Stop")
    Else
        ContextMenuStrip1.Items.Add("Start")
    End If
End Sub

0
 
nepaluzCommented:
Lauching the apps from the lstview would be simple, just handle the click onthe row.

Kiling the app (I would suggest) would be via a context menu. Clicking it would check whether the status is running and then kill the app, else the menu item for killing would be dis-abled.

PS> Have you thought about implementing this in a datagridview rather than a listview?
0
 
RobertoFreemanoAuthor Commented:
I don't know anythink about datagridview's... :(
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
nepaluzCommented:
OK. I suppose you'll have to do with the listview then.

Same principle, add a context menu (with the actions "Start", "Stop" etc) and assign it to the list view. On firing the context menu, dis-able the Stop if status is not Running (and vise versa dis-able the Start if status is running).

Are you conversant with the above? If not, where would you require help?
0
 
RobertoFreemanoAuthor Commented:
Hi nepaluz,

is there any code you could offer please?

I will have a crack at this shortly and see how I get on.

Thanks,
Roberto
0
 
nepaluzCommented:
Here is some scrap code.
1. You'll have to set up / add the items to the listview, add a new context menu to the form and associate it with the listview (from the listview properties).
2. handle the ItemClicked and ContextMenuOpening for the context menu (as in code below).
3. You can enable / disable the Start / Stop in the opening handle. The rest should be self explanatory, if not, let usknow.


Private Sub ContextMenuStrip1_ItemClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles ContextMenuStrip1.ItemClicked
    Select Case e.ClickedItem.Text
        Case "Start"
            'Process.Start("C:\Program Files\CA\DSM\bin\gui_rcLaunch.exe")
            Me.ListView1.FocusedItem.SubItems.Item(1).Text = "Running"
        Case "Stop"
            'here as above you run your kill code
            Me.ListView1.FocusedItem.SubItems.Item(1).Text = "Idle"
        Case Else
    End Select
End Sub

Private Sub ContextMenuStrip1_Opening(ByVal sender As ContextMenuStrip, ByVal e As System.ComponentModel.CancelEventArgs) Handles ContextMenuStrip1.Opening
    If Me.ListView1.FocusedItem.SubItems.Item(1).Text = "Running" Then
        MessageBox.Show("Running")
    Else
        MessageBox.Show("Idle")
    End If
End Sub

Open in new window

0
 
nffvrxqgrcfqvvcCommented:
<<   p1.Kill() >>

This means applications won't cleanup or save settings when the application exits. Use CloseMainWindow() for each application that has the MainWindowHandle property <> 0
0
 
nepaluzCommented:
Youcan actually change the second handler for the ContextMenuOpening to create menu items on the fly to suit the status of the item. (replace above code suggestion with this)


Private Sub ContextMenuStrip1_Opening(ByVal sender As ContextMenuStrip, ByVal e As System.ComponentModel.CancelEventArgs) Handles ContextMenuStrip1.Opening
    ContextMenuStrip1.Items.Clear()
    If Me.ListView1.FocusedItem.SubItems.Item(1).Text = "Running" Then
        ContextMenuStrip1.Items.Add("Stop")
    Else
        ContextMenuStrip1.Items.Add("Start")
    End If
End Sub

Open in new window

0
 
RobertoFreemanoAuthor Commented:
Hi nepaluz,

Sorry to be a total clutz... but... I get errors... do i need to rename the Context Menu1? (I did try it my the way)

Errors:

1. Type 'System.Windows.Forms.ToolStripItemClickedEventArgs' is not defined.

2. Handles clause requires a WithEvents variable.

3. Type 'ContextMenuStrip' is not defined.

4. Handles clause requires a WithEvents variable.

If indicated by "1" in the Code where the errors occur.
Private Sub ContextMenuStrip1_ItemClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs "1" ) Handles ContextMenuStrip1 "2".ItemClicked
        Select Case e.ClickedItem.Text
            Case "Start"
                'Process.Start("C:\Program Files\CA\DSM\bin\gui_rcLaunch.exe")
                Me.ListView1.FocusedItem.SubItems.Item(1).Text = "Running"
            Case "Stop"
                'here as above you run your kill code
                Me.ListView1.FocusedItem.SubItems.Item(1).Text = "Idle"
            Case Else
        End Select
    End Sub

    Private Sub ContextMenuStrip1_Opening(ByVal sender As ContextMenuStrip, ByVal e As System.ComponentModel "3" .CancelEventArgs) Handles ContextMenuStrip1 "4" .Opening
        If Me.ListView1.FocusedItem.SubItems.Item(1).Text = "Running" Then
            MessageBox.Show("Running")
        Else
            MessageBox.Show("Idle")
        End If
    End Sub

Open in new window

0
 
nepaluzCommented:
Add a contextmenu to the form first and errors 1 and 2 thatyou have indicated will disappear. This is from the toolbox or you can declare it as:

Public WithEvents ContextMenu1 As ContextMenu

(adding it to the form, in the background, adds the above line)
0
 
nepaluzCommented:
Also, change the second handler to the last code I suggested.
0
 
RobertoFreemanoAuthor Commented:
Sorry nepaluz,

It must be me (friend)... I'm obviously not getting it :(

Please see screenshots of my VB.NET 2003 environment.

Thanks
screenshot-1.jpg
screenshot-2.jpg
screenshot-3.jpg
0
 
nepaluzCommented:
try selecting the handlers directly from the VS interface rather than copying the entire code suggestion.

e.g in your screenshot-3.jpg, in the top left hand corner where it says Form1, click the drop down and select ContextMenuStrip1 and select the handler from the right hand side drop down (for opening and item clicked)
0
 
RobertoFreemanoAuthor Commented:
Sorry Nepaluz,

Are you testing on VB.NET 2003? I'm not sure I follow your suggestion :(

If I were to start from complete beginning maybe?
0
 
nepaluzCommented:
I am NOT using VB2003, however, the events SHOULD be there too!
0
 
RobertoFreemanoAuthor Commented:
Hi Nepaluz,

 Thanks for being sooooo patient with me... below is what I got.
window3.jpg
window4.jpg
0
 
nepaluzCommented:
There is an error in your error log (call me eagle eyes!)

remove the declaration for contextmenu1 (NOTE: The one starting in lowercase)
Since you have put a ContextMenu via the designer, there is no need to redeclare it.

The events should show for the designer added context menu.
0
 
RobertoFreemanoAuthor Commented:
nepaluz,

Let's start from a blank canvas...


FORM1 (blank)
Do I need to declare a ContextMenu called 'ContextMenuStrip1'?
0
 
nepaluzCommented:
NO.
just add your listview and contextmenu in the designer.
0
 
RobertoFreemanoAuthor Commented:
Ok,

Form1 [with] default names:
Listview1 & ContectMenu1
0
 
RobertoFreemanoAuthor Commented:
ContextMenu1
0
 
nepaluzCommented:
right, now move over to the code (view code) and choose the ContextMenu1 from the top left hand dropdown.

Next, in the right hand dropdown, you can select the events (they SHOULD be showing now)

NOTE: The contextmenu you just added will automatically be named ContextMenu1
0
 
RobertoFreemanoAuthor Commented:
Ok,

Selected events ContextMenu1 (top left corner - dropdown)

In Declarations (drop-down), there are on 2 options:-
Disposed
Popup
0
 
RobertoFreemanoAuthor Commented:
Could you send me a screenshot of what you have please?
0
 
RobertoFreemanoAuthor Commented:
Hi,

I have downloaded Visual Basic 2010 Express and installed it to test... your code works in this with ContextMenuStrip; although, I cannot kill the app (for some reason) - I used notepad as an example.

I develop on VB.NET 2003 because I bought it yonks ago and it works in my work environment.

VB 2003 doesn't use 'ContextMenuStrip' :(

Surely I can try to work around this...
0
 
nepaluzCommented:
I suppose you have to plumb the events in yourself. (talk about re-inventing the wheel)
My opinion ..... use VS Express. I am sure it will work in your work environment too. The feature set in the 2010 express should rival those in VS2003. But then again, I've never used 2003 so wouldn't know!
0
 
RobertoFreemanoAuthor Commented:
Thanks nepaluz,

This is the closest I'm going to get and it seems to work.
Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click
        Select Case ListView1.SelectedIndices(0)
            Case 0

                Process.Start("C:\WINDOWS\system32\notepad.exe")
                Me.ListView1.FocusedItem.SubItems.Item(1).Text = "Running"
            Case 1
                Process.Start("C:\Program Files\Internet Explorer\iexplore.exe")
                Me.ListView1.FocusedItem.SubItems.Item(1).Text = "Running"
        End Select
        
    End Sub

    Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
        Select Case ListView1.SelectedIndices(0)
            Case 0
                Dim pProcess() As Process = System.Diagnostics.Process.GetProcessesByName("notepad")
                For Each p As Process In pProcess
                    p.Kill()
                    Me.ListView1.FocusedItem.SubItems.Item(1).Text = "Idle"
                Next
            Case 1
                Dim pProcess() As Process = System.Diagnostics.Process.GetProcessesByName("iexplore")
                For Each p As Process In pProcess
                    p.Kill()
                    Me.ListView1.FocusedItem.SubItems.Item(1).Text = "Idle"
                Next
        End Select

Open in new window

0
 
RobertoFreemanoAuthor Commented:
Thanks nepaluz ;)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.