James
James

Reputation: 27

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

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 installed!");
    }
}

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

Upvotes: 0

Views: 4601

Answers (1)

Dave Nay
Dave Nay

Reputation: 559

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.

Upvotes: 4

Related Questions