开发者

How to grey out buttons on C# forms dependent on a particular instance

开发者 https://www.devze.com 2023-03-10 19:25 出处:网络
I have this method that checks if a service is running and a button which when clicked initaites the method. Although is there a way to \"grey out\" the button if the service is for example - not inst

I have this method that checks if a service is running and a button which when clicked initaites the method. Although is there a way to "grey out" the button if the service is for example - not installed. ?

public static void StopService(string serviceName, int timeoutMilliseconds)
{
    ServiceController service = new ServiceController(serviceName);
    try
    {
        TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);

        service.Stop();
        service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
        MessageBox.Show("The service was successfully turned on");
    }
    catch
    {
         MessageBox.Show("Service is not开发者_如何学C installed!");
    }
}

private void button14_Click_1(object sender, EventArgs e)
{
    StopService("Update Scheduler Service", 20000);
}


WinForms controls have a .Enabled property that when set to False, the control is grayed out like you wish. I assume WPF has the same functionality, but I have never used WPF so I can't say for certain.

0

精彩评论

暂无评论...
验证码 换一张
取 消