Solved

Process start app from listview VB.net 2003

Posted on 2011-02-20
28
755 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:RobertoFreemano
  • 14
  • 13
28 Comments
 
LVL 17

Expert Comment

by:nepaluz
ID: 34937010
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
 

Author Comment

by:RobertoFreemano
ID: 34937022
I don't know anythink about datagridview's... :(
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 34937050
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
 

Author Comment

by:RobertoFreemano
ID: 34937136
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
 
LVL 17

Expert Comment

by:nepaluz
ID: 34937438
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
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 34937660
<<   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
 
LVL 17

Expert Comment

by:nepaluz
ID: 34937661
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
 

Author Comment

by:RobertoFreemano
ID: 34938307
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
 
LVL 17

Expert Comment

by:nepaluz
ID: 34938358
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
 
LVL 17

Expert Comment

by:nepaluz
ID: 34938364
Also, change the second handler to the last code I suggested.
0
 

Author Comment

by:RobertoFreemano
ID: 34938568
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
 
LVL 17

Expert Comment

by:nepaluz
ID: 34938679
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
 

Author Comment

by:RobertoFreemano
ID: 34938700
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
 
LVL 17

Expert Comment

by:nepaluz
ID: 34938825
I am NOT using VB2003, however, the events SHOULD be there too!
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:RobertoFreemano
ID: 34943930
Hi Nepaluz,

 Thanks for being sooooo patient with me... below is what I got.
window3.jpg
window4.jpg
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 34944005
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
 

Author Comment

by:RobertoFreemano
ID: 34944166
nepaluz,

Let's start from a blank canvas...


FORM1 (blank)
Do I need to declare a ContextMenu called 'ContextMenuStrip1'?
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 34944276
NO.
just add your listview and contextmenu in the designer.
0
 

Author Comment

by:RobertoFreemano
ID: 34944314
Ok,

Form1 [with] default names:
Listview1 & ContectMenu1
0
 

Author Comment

by:RobertoFreemano
ID: 34944320
ContextMenu1
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 34944365
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
 

Author Comment

by:RobertoFreemano
ID: 34944448
Ok,

Selected events ContextMenu1 (top left corner - dropdown)

In Declarations (drop-down), there are on 2 options:-
Disposed
Popup
0
 

Author Comment

by:RobertoFreemano
ID: 34944713
Could you send me a screenshot of what you have please?
0
 
LVL 17

Accepted Solution

by:
nepaluz earned 110 total points
ID: 34944859
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
 

Author Comment

by:RobertoFreemano
ID: 34945257
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
 
LVL 17

Expert Comment

by:nepaluz
ID: 34945293
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
 

Author Comment

by:RobertoFreemano
ID: 34945375
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
 

Author Closing Comment

by:RobertoFreemano
ID: 34945380
Thanks nepaluz ;)
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

746 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

12 Experts available now in Live!

Get 1:1 Help Now