当前位置: 动力学知识库 > 问答 > 编程问答 >

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)

{

}

}

}

网友答案:

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;
    }
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: