我已经看过几次,但似乎无法让它申请我。
我想将项目添加到我home
班级ServerController
班级的列表框中。
不完全确定如何解决这个问题所以任何帮助都会感激不尽。
家庭班级:
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("");
}
}
服务类:
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;
}
}
在Service Class
我要使用home.addItem
我是否认为我需要在家庭班级中创建公共列表框并将其与我设计中的列表框链接?
我想要实现的目标是:
我希望它检查x
数量的服务以查看其状态。如果它已停止,则检查是否已禁用并报告 - 如果已禁用,则尝试设置为自动,否则尝试启动它。我想写一个日志,就像这样做。
希望这会给出更多澄清。
感谢!!!
答案 0 :(得分:3)
您可以通过构造函数
传递它public class serviceController
{
private ListBox home;
public serviceController(ListBox lb)
{
home = lb;
}
public void foo()
{
home.Items.Add("")
}
}
现在,当您创建serviceController的新对象时,您需要传递一个列表框。
serviceController sv = new serviceController(lb_msg);
然后,当你在serviceController中时,你可以像在主类中一样使用变量lb
访问其他表单控件的另一种方法是
frmHome form = new frmHome();
ListBox lb = (ListBox)form.Controls["nameOfControl"];
lb.Items.Add("");
另外:serviceController应以大写的ServiceController
开头答案 1 :(得分:1)
我写了一个示例代码来演示我的建议。您必须将其与您正在使用的实际类型(例如类)相匹配。添加了代码注释,所以我不会再写这么多了。如果你想玩这个代码,这里有一个小提琴:https://dotnetfiddle.net/t0MyNk
InnoDB
输出:
public class Program // Consider this your home form
{
public void Main()
{
var serviceController = new ServiceController();
serviceController.ServiceStateChange += ServiceController_ServiceStateChanged;
serviceController.StartServices();
}
private void ServiceController_ServiceStateChanged(object sender, ServiceControllerEventArgs e)
{
// Add to listbox or do whatever you want. I am just 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 it starts almost immediately. If not, you could follow same pattern for
// Service class where an event will be raised once service is "actually" started.
if(s.Status == "Running") {
message = string.Format("Service {0} is {1}", s.Name, s.Status);
}
}
else if(s.Status == "Running") {
message = string.Format("Service {0} is already {1}", s.Name, s.Status);
}
// Now tell subscriber (home form) about this.
OnServiceStateChange(message);
}
}
private void OnServiceStateChange(string message)
{
var serviceStateChangeHandler = ServiceStateChange;
if(serviceStateChangeHandler != null)
{
serviceStateChangeHandler(this, new ServiceControllerEventArgs { Message = message });
}
}
}
// You could have custome delegate type for event to return string message.
// But IMO this is much cleaner, and as per MSFT guidelines.
public class ServiceControllerEventArgs : EventArgs
{
// You can also return Service instance
// Just including Message for now
public string Message { get; set; }
}
// for demo purpose, represents individual service.
public class Service
{
public string Name { get; set; }
public string Status { get; set; }
public void Start() {
Status = "Running";
}
}
答案 2 :(得分:0)
我强烈建议您采用绑定而不是直接操作表单上的控件,目的是保持数据和UI分离。例如,您可以在类中包含一个列表(它必须是BindingList&lt;&gt;,如果内存服务),并且列表控件将绑定到它;这样,列表的内容就会反映在用户界面中,您可以从其他地方自由更新列表,只需 data ,而无需考虑数据呈现方式的复杂性。
最后你可以想出类似MVVM(model-view-viewmodel)或MVC的东西,在我看来,这是手动操作控件的优秀解决方案。