I decided to try and expand my project, I now wish to have the (current, I'm adding more as I find out which aren't required) two processes here's a brief on what stage the additions are at.
I have added 4 check boxes named;
SpoolCheck1
SpoolCheck2
MDMCheck1
MDMCheck2
(more will be added as I find out more that can be stopped)
and two command buttons named;
StopCheckedButton
StartCheckedButton
I would like to 'link' the check boxes to the command buttons, but the checkboxes job (when checked, then clicking the start/stop command buttons) is to start/stop the selected program/process, in this case spool and mdm.
I have these codes for starting and stopping the processes and return a message if there is a problem.
Start Code
Try
StartSpool.ServiceName = "Spooler"
StartSpool.Start()
Catch EX As InvalidCastException
MessageBox.Show("Invalid Cast Exception")
Catch Ex As Exception
MessageBox.Show("SpoolSV could not be started, are you sure it's not already running ")
End Try
Stop Code
Try
StopSpool.ServiceName = "Spooler"
StopSpool.Stop()
Catch EX As InvalidCastException
MessageBox.Show("Invalid Cast Exception")
Catch Ex As Exception
MessageBox.Show("SpoolSV could not be stopped, are you sure it's running ")
End Try
These codes work, is there some way of integrating them, or adapting them into the checkbox system I'm after I would like to retain the message return, as this solves a crashing issue I can't fix.
If the above doesn't make sense, then please ask me to explain further as its just gone 00:00 and I've been toying with this for a few hours while and adding/editing existing stuff.
Many Thanks

CheckBox Service control (VB, VS 05)
Minherz
Ensure you code is the same do declaring the enumeration called action. Once again this code is well tried and tested and is a core part of the language.
Double check you code against the code previously shown.
Srdjan
For Each c as Checkbox in me.controls
This has worked for me in the past.
adamhill
ok, got the code in place but i get this (see image at link below)
http://img178.imageshack.us/img178/6991/confusedyz3.jpg
now I havent changed the code itself, only the names to fall in line with some changes to the project I made today.
any ideas have I accedently changed something that shouldnt have been changed
Thanks
Daniel 'fred' Munro
Tovdb
I would assume you are using the ServiceController Control - as you dont say and there isnt any example in you supplied code but it would be as simple as the following code. What this code does is check all the checkboxes and use the checkbox text as the servicename you wish to start/stop.
Want to add more services to you list - just add the checkboxes to the panel and set the text to match the service name - no code changes. One function handling the start and stop action - just the calls that are different meaning less duplicated code. Uses and enumeration for the actions rather than any magic numbers so its clear the action is starting or stopping and removes unneccessary exception.
Public Class Form1
Enum Action
StartProcess
StopProcess
End Enum
Private Sub StartProcessButton_Clicked(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each c As CheckBox In Me.Panel1.Controls
If c.Checked Then ProcessAction(Me.ServiceController1, c.Text, Action.StartProcess)
Next
End Sub
Private Sub StopProcessButton_Clicked(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
For Each c As CheckBox In Me.Panel1.Controls
If c.Checked Then ProcessAction(Me.ServiceController1, c.Text, Action.StopProcess)
Next
End Sub
Sub ProcessAction(ByVal sc As ServiceProcess.ServiceController, ByVal ServiceName As String, ByVal Action As Action)
'ServiceName has to be passed to StartCode Sub or it won't run, you can than use the variable StartCode anywehre within this sub
Dim ActionString As String = ""
Try
sc.ServiceName = ServiceName
If Action = Action.StartProcess Then
sc.Start()
ActionString = "Started"
Else
sc.Stop()
ActionString = "Stopped"
End If
Catch Ex As Exception
Dim msg As String = ServiceName & " could not be " & ActionString
MessageBox.Show(msg)
End Try
End Sub
End Class
newbiedogg
That will work as long as there are no other checkboxes on the form except the service ones.
The form is a container control as is the panel control which means it has a controls collection of the controls that are contained within it.
I used Panels and Groupboxes and they work just fine so something like
For each c as control in Me.<ControlName>.Controls
Next
Will work just fine for all container controls. (Panel, Groupbox, TabControl etc.) Or if you are looking at iterating through all the controls on the form. Ensure that you are calling the correct controlname for the one on your form.
For each c as control in Me.Controls
Next
Javier Luna
Create Sub/Functions for the start and stop and make them so that the same method works for whichever service you are trying to start / stop.
Put the checkboxes in a panel, set the text to show the name of the service and then use something like the following which would iterate through all the checkboxes and call you method (in this case called startProcess) with the text from your checkbox.
For Each c As CheckBox In Me.Panel1.Controls
If c.Checked Then StartProcess(c.Text)
Next
You would have a method called StopProcess doing a similar thing and similar code in the Stop Button Click event.
Docpro777
Real simple
Use
Sub ProcessAction(ByVal sc As ServiceProcess.ServiceController, ByVal ServiceName As String, ByVal Action As Action)
'ServiceName has to be passed to StartCode Sub or it won't run, you can than use the variable StartCode anywehre within this sub
Dim ActionString As String = ""
Try
sc.ServiceName = ServiceName
If Action = Action.StartProcess Then
sc.Start()
ActionString = "Started"
Else
sc.Stop()
ActionString = "Stopped"
End If
Catch Ex As Exception
Dim msg As String = ServiceName & " could not be " & ActionString
MessageBox.Show(msg)
End Try
End Sub
furmangg
ive just noticed this in the error list,
'Panel1' is not a member of 'Process_Stopper_Multi.Main.Main'
the whole "Main.Main" bit could that be causing a problem
EDIT:
During a full re-build (from scratch) I have got rid of all but ONE error.
"Type 'EnumAction' is not defined."
This is the only error that has stayed even over a rebuild.
I think the problems were "relics" from me using the previous versions as a base for the subsequent versions.
I Will try to find a solution myself, but if i cannot I will post again.
Mantorok
This is a direct copy and paste from the projects code;
Public
Class MainEnum Action
StartProcess
StopProcess
End Enum
Private Sub StartProcessButton_Clicked(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckStartButton.Click
For Each c As CheckBox In Me.CheckControlGroupBox.Controls
If c.Checked Then ProcessAction(Me.ServiceController1, c.Text, Action.StartProcess)
Next
End Sub
Private Sub StopProcessButton_Clicked(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckStopButton.Click
For Each c As CheckBox In Me.CheckControlGroupBox.Controls
If c.Checked Then ProcessAction(Me.ServiceController1, c.Text, Action.StopProcess)
Next
End Sub
Sub ProcessAction(ByVal sc As ServiceProcess.ServiceController, ByVal ServiceName As String, ByVal Action As EnumAction)
'ServiceName has to be passed to StartCode Sub or it won't run, you can than use the variable StartCode anywehre within this sub
Dim ActionString As String = ""
Try
sc.ServiceName = ServiceName
If Action = Action.StartProcess Then
sc.Start()
ActionString = "Started"
Else
sc.Stop()
ActionString = "Stopped"
End If
Catch Ex As Exception
Dim msg As String = ServiceName & " could not be " & ActionString
MessageBox.Show(msg)
End Try
End Sub
As you can see the only differences are;
"Panel1" has been changed to a GroupBox and named "CheckControlGroupBox" also "Button1" and "Button2" have been renamed to "CheckStartButton" and "CheckStopButton" (so that I can easily find the relevant button) there are no issues with the renaming, I can at least figer that out.
The only thing with an error is the "EnumAction" I have compared the code twice and even tried the original names, as far as I can see the code is the same (except for names which are correctly changed) I just don't understant why this one error is persistant.
I'll keep toying, but if anyone can see the problem could you point it out.
Many thanks
Daniel 'fred' Munro
CJira
Anyways here we go
Private sub StartCode(ByVal ServiceName As String)
'ServiceName has to be passed to StartCode Sub or it won't run, you can than use the variable StartCode anywehre within this sub
StartSpool.ServiceName = ServiceName
StartSpool.Start()
Catch EX As InvalidCastException
MessageBox.Show("Invalid Cast Exception")
Catch Ex As Exception
MessageBox.Show("SpoolSV could not be started, are you sure it's not already running ")
End Sub
Private Sub StopCode(ByVal ServiceName As String)
Try
End SubStopSpool.ServiceName = ServiceName
StopSpool.Stop()
Catch EX As InvalidCastException
MessageBox.Show("Invalid Cast Exception")
Catch Ex As Exception
MessageBox.Show("SpoolSV could not be stopped, are you sure it's running ")
End Try
Private Sub StartCheckedButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartCheckedButton.Click
Dim ServiceName as String
if SpoolCheck1.checked = true then
ServiceName = "spool"
Else if SpoolCheck2.checked = true then
ServiceName = "spool"
Else if MDMCheck1.checked = true then
ServiceName = "MDM"
Else if MDMCheck2.checked = true then
ServiceName = "MDM"
End If
StartCode(ServiceName)
End Sub
Private Sub StopCheckedButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StopCheckedButton.Click
Dim ServiceName as String
if SpoolCheck1.checked = true then
ServiceName = "spool"
Else if SpoolCheck2.checked = true then
ServiceName = "spool"
Else if MDMCheck1.checked = true then
ServiceName = "MDM"
Else if MDMCheck2.checked = true then
ServiceName = "MDM"
End If
StopCode(ServiceName)
End Sub
You could also set that ugly if statement into a a function if you wanted like this
Private Function checked()
Dim ServiceName as String
if SpoolCheck1.checked = true then
ServiceName = "spool"
Else if SpoolCheck2.checked = true then
ServiceName = "spool"
Else if MDMCheck1.checked = true then
ServiceName = "MDM"
Else if MDMCheck2.checked = true then
ServiceName = "MDM"
End If
Return ServiceName
End Function
than I beleive you could put that ugly if statement in a function if you wanted instead like this, someone correct me if this is wrong.
Private Sub StopCheckedButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StopCheckedButton.Click
Dim ServiceName as checked()
StopCode(ServiceName)
End Sub
Anyways that should atleast get you started in the right direction.
Amde
ok, heres what i did,
i got rid of the two groupboxes i had and created a new Panel keeping its original name "Panel1" see image below;
http://img185.imageshack.us/img185/9572/panel1withpropertiesqu3.jpg
now here is the code (related to the panel) for the main form.
sorry for the change in host, imageshack went down while uploading.. :(
http://www.imagehosting.com/show.php/108804_Code.JPG.html
as you can see, its from what i can see the same issue, i have tried placing this new code at the very begining of the code for Main.vb but this has no effect.
Any Ideas
Dnaiel 'fred' Munro
Buddy Funny
how do I created Sub/Functions
I've toyed with it and I think I'm on the right path, How do I "Declare" a funtion as right now im getting "Name "StartProcess" is not declared."
Daniel 'fred' Munro
barkingdog
Cool, Thats got rid of that error. Thanks
Edit:
Just tried using the check boxes to stop Spool and MDM and got this;
http://img292.imageshack.us/img292/507/debuggingerrorpa7.jpg
Tried just one at a time but same thing, checked that the checkboxes are named the same as the service name, and they check out (told to do that in a previous post)
I'll dig around see if I've overlooked something again...
sujazach
Create a single Panel Control, in my case called Panel1 and ensure that you checkboxes are contained within this Panel control by dragging them onto the panel.
I would guess you dont have a panel control with that name - and you appear to have two different names for the panel controls.