Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable and enable buttons in C#

Tags:

c#

I am working on something fairly simple, well I thought it would be. What I want is when button1 is clicked I want it to disable button1 and enable button2. I get the error below: Error 1 Only assignment, call, increment, decrement, and new object expressions can be used as a statement.

private readonly Process proc = new Process();
    public Form1()
    {
        InitializeComponent();
        button2.Enabled = false;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        proc.StartInfo = new ProcessStartInfo {
            FileName = Environment.GetFolderPath(Environment.SpecialFolder.Windows) + "/explorer.exe",
            Arguments = @"D:\",
            UseShellExecute = false
        };
        proc.Start();
        button1.Enabled = false;
        button2.Enabled = true;
    }


    private void button2_Click(object sender, EventArgs e)
    {
        proc.Kill();
        button1.Enabled = true;
        button2.Enabled = false;
    }
like image 774
user770022 Avatar asked Nov 05 '10 16:11

user770022


People also ask

How do I disable and enable a button?

To disable a button using only JavaScript you need to set its disabled property to false . For example: element. disabled = true . And to enable a button we would do the opposite by setting the disabled JavaScript property to false .

What is enable and disable button based on condition?

By default a button's state is enabled in HTML so by setting disabled = true, we have disabled the button for the user. 3. Then we add an event handler (addEventListener) to the input field with the event property change which monitors the interaction with elements.

How do I disable a clicking button?

The disabled attribute is a boolean attribute. When present, it specifies that the button should be disabled. A disabled button is unusable and un-clickable. The disabled attribute can be set to keep a user from clicking on the button until some other condition has been met (like selecting a checkbox, etc.).

How do you enable or disable a button from another form in C#?

But how to do this? You could make a method like public void SetButton1Enabled(bool enabled) { button1. Enabled = enabled; } inside Form1 and call that inside the button2_Click method of Form2 ( Form2 needs a reference to the Form1 object it was called from). Or work with events/delegates.


2 Answers

In your button1_click function you are using '==' for button2.Enabled == true;

This should be button2.Enabled = true;

like image 190
thattolleyguy Avatar answered Sep 23 '22 18:09

thattolleyguy


Update 2019

This is now IsEnabled

 takePicturebutton.IsEnabled = false; // true
like image 28
Java.Her Avatar answered Sep 20 '22 18:09

Java.Her