简体   繁体   中英

How can I change a button name in windows form c#

Im starting to learn c# and Im trying to make a simple app that when I press a button the button's name changes to "Clicked" but I cant make it change the name. This is what I did:

    private void Button1_Click(object sender, EventArgs e)
    {
        button1.Text= "Clicked";
    }

Button1_Click event is not assigned to button1 control, select button then navigate to properties then select events then select Button1_Click at click event then button1 text will change on click.

private void Form1_Load(object sender, EventArgs e)
{
    button1.Text= "Clicked";
}

It works that way

The Text property is used to set the text caption displayed in the Button control. It seems you don't get the event handler triggered at all. Just see a breakpoint and see how it is working under the debugger.

But if you need to change the name of object pointer (button name) you can't really do that after declaring it. You have to set a new object reference instead:

Button newButtonName = button1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM