开发者

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.

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;
    }


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.


Update 2019

This is now IsEnabled

 takePicturebutton.IsEnabled = false; // true


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;
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜