I've seen this posted a few times but I cannot seem to get it to apply for me.
I want to add items to a listbox which is in my home class from my ServerController class.
Not entirely sure on how to go about this so any help would be grateful.
Home Class:
public partial class frmHome : Form
{
serviceController sc = new serviceController();
public ListBox lb = new ListBox();
public frmHome()
{
lb = new ListBox();
InitializeComponent();
serviceController sc = new serviceController();
}
//ListBox Add
public void addItem(string item)
{
lb_msg.Items.Add(item);
lb_msg.Items.Add(item);
lb_msg.Items.Add("");
}
}
Service Class:
public class serviceController
{
ServiceController[] scServices;
//Start the service
public void startServices()
{
scServices = ServiceController.GetServices();
try
{
foreach (ServiceController scTemp in scServices)
{
if (scTemp.ServiceName == "MSSQL$SQLSERVER" || scTemp.ServiceName == "SQLBrowser")
{
if (scTemp.Status == ServiceControllerStatus.Stopped)
{
//Check to see if service is disabled
home.addItem("Attempting to start " + scTemp.ServiceName);
scTemp.Start();
scTemp.WaitForStatus(ServiceControllerStatus.Running);
}
if (scTemp.Status == ServiceControllerStatus.Running)
{
home.addItem(scTemp.ServiceName + " is running");
}
}
else if (scTemp.Status == ServiceControllerStatus.Running)
{
home.addItem(scTemp.ServiceName + " is already running");
}
}
serverStatus();
}
catch (Exception ex)
{
throw ex;
}
}
On the Service Class I want to use home.addItem
Am I write in thinking I need to make a public listbox in my home class and link it with the one in my design?
What I want to achieve is this:
I want it to check x amount of services to see the status of it. If it is stopped, then check if it is disabled and report back - if it is disabled attempt to set as automatic, else attempt to start it. I want to write a log as it does this.
Hope this gives a bit more clarification.
Thanks!!!
public ListBox lbbut uselb_msgto actually add something?