c# - How can I add to a listbox from another class? -


i've seen posted few times cannot seem apply me.

i want add items listbox in home class servercontroller class.

not entirely sure on how go 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 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 see if service disabled                              home.additem("attempting start " + sctemp.servicename);                             sctemp.start();                             sctemp.waitforstatus(servicecontrollerstatus.running);                         }                         if (sctemp.status == servicecontrollerstatus.running)                         {                             home.additem(sctemp.servicename + " running");                         }                      }                     else if (sctemp.status == servicecontrollerstatus.running)                     {                         home.additem(sctemp.servicename + " running");                     }                 }                 serverstatus();             }             catch (exception ex)             {                 throw ex;             }          } 

on service class want use home.additem

am write in thinking need make public listbox in home class , link 1 in design?

what want achieve this:

i want check x amount of services see status of it. if stopped, check if disabled , report - if disabled attempt set automatic, else attempt start it. want write log this.

hope gives bit more clarification.

thanks!!!

i have written sample code demonstrate suggested. have match actual types (classes e.g.) you're using. added code comments, i'm not going write here again. if want play code, here's fiddle: https://dotnetfiddle.net/t0mynk

public class program // consider home form {     public void main()     {         var servicecontroller = new servicecontroller();         servicecontroller.servicestatechange += servicecontroller_servicestatechanged;         servicecontroller.startservices();     }      private void servicecontroller_servicestatechanged(object sender, servicecontrollereventargs e)     {         // add listbox or whatever want. printing.         console.writeline(e.message);     } }  public class servicecontroller {     public event eventhandler<servicecontrollereventargs> servicestatechange;      public void startservices()     {         service[] services = new service[] // sample services data         {              new service { name = "msmq", status = "stopped" },             new service { name = "w3svc", status = "running" }         };          string message = null;          foreach(service s in services)          {             if(s.status == "stopped")              {                 s.start();                 // assuming starts immediately. if not, follow same pattern                  // service class event raised once service "actually" started.                 if(s.status == "running") {                     message = string.format("service {0} {1}", s.name, s.status);                 }             }             else if(s.status == "running") {                 message = string.format("service {0} {1}", s.name, s.status);             }              // tell subscriber (home form) this.             onservicestatechange(message);         }     }      private void onservicestatechange(string message)     {         var servicestatechangehandler = servicestatechange;         if(servicestatechangehandler != null)         {             servicestatechangehandler(this, new servicecontrollereventargs { message = message });         }     } }  // have custome delegate type event return string message. // imo cleaner, , per msft guidelines. public class servicecontrollereventargs : eventargs {     // can return service instance     // including message      public string message { get; set; } }  // demo purpose, represents individual service. public class service {     public string name { get; set; }     public string status { get; set; }      public void start() {         status = "running";     } } 

output:

service msmq running    service w3svc running 

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -