Disable and enable buttons in 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.

{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button3_Click(object sender, EventArgs e)
    {
        Close();

    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }
    private void RandomNumber(int min, int max)
    {
        int num = new Random().Next(min, max);
         label1.Text = num.ToString();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        RandomNumber(0,99);
        button2.Enabled == true ;
        if (textBox1.Text == label1.Text)
            MessageBox.Show("Winner");

    }

    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void button2_Click(object sender, EventArgs e)
    {

    } 

}
}

Answers


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

This should be button2.Enabled = true;


button2.Enabled == true ; must be button2.Enabled = true ;.

You have a compare == where you should have an assign =.


button2.Enabled == true ;

thats the problem - it should be:

button2.Enabled = true ;

Change this

button2.Enabled == true

to

button2.Enabled = true;

button2.Enabled == true ;

should be

button2.Enabled = true ;

It is this line button2.Enabled == true, it should be button2.Enabled = true. You are doing comparison when you should be doing assignment.


Change button2.Enabled == true to button2.Enabled = true and you will be set!


You can use this for your purpose.

In parent form:

private void addCustomerToolStripMenuItem_Click(object sender, EventArgs e)
{
    CustomerPage f = new CustomerPage();
    f.LoadType = 1;
    f.MdiParent = this;
    f.Show();            
    f.Focus();
}

In child form:

public int LoadType{get;set;}

private void CustomerPage_Load(object sender, EventArgs e)
{        
    if (LoadType == 1)
    {
        this.button1.Visible = false;
    }
}

Update 2019

This is now IsEnabled

 takePicturebutton.IsEnabled = false; // true

Change this:

button2.Enabled == true;

To:

button2.Enabled = true;

Need Your Help

Which is the easiest way to simulate keyboard and mouse on Python?

python windows keyboard mouse

I need to do some macros and I wanna know what is the most recommended way to do it.

msbuild calling powershell script under windows 7 gets signing error

powershell msbuild windows-7

We have a powershell build script that calls out to other scripts that is giving me trouble under Windows 7 64 bit. It works fine under XP. I've run set-executionpolicy to RemoteSigned (and also ...