Is it possible to change the button name? Like I have:

private void Button1_Click(object sender, EventArgs e)
{
this.Button1.Name = "Button2"
}
private void Button2_Click(object sender, EventArgs e)
{
this.Button2.Text = "Button2"
}

But when I do that and click the button again it doesn't do anything, am I doing something wrong?

Recommended Answers

All 9 Replies

are you trying to rename the button variable or rename the button text (AS shown to user). to rename button text change

{
this.Button1.Name = "Button2"
}

to
{
this.Button1.Text = "Button2"
}
now I have not checked this to see if it works so let me know how u get on. hope this helps

go to the design view, select the button

and go to the properties - then change the name of the button..

ah the beauty of visual studio! makes life easier and its comprehensive!

Sorry, I forgot to mention I wanted to change the name during run time. I know you can change the button name that way but I want to know if you can change it during run time or if there is a way to click it again and have it do something else.

what else would you like it to do ?

I don't want to change the text, I know how to do that. I want to change the name the button is given. Like

private void Button1_Click(object sender, EventArgs e)
{
label1.Text = "Old Text";
this.Button1.Name = "Button2";
this.Button1.Text = "Click Me";
}
private void Button2_Click(object sender, EventArgs e)
{
this.Button2.Text = "Click Again";
this.Button2.Name = "Button3";
label1.Text = "New Text";
}

Can I just ask out of curiosity why would you want to do that ?

Just trying to make a program for fun. I am trying to make it like press a button(button1) and put text in an area then press another button(button2) and make it change the text on button1 and change what button1 does.

ah,

you don't want to change the button name. You are getting mixed up by the way Visual Studio creates methods for you.

When a button is clicked it fires an event. Each button stores a list of event handlers which get told about the event that just happened.

So when you double click on a button to get to the code you create a method and then visual studio adds the method to the buttons list of event handlers. You can see it happening in Form1.designer.resx.

Or if you can't arsed looking then create a button, double click to get the code. Then delete the whole method. It won't compile and double clicking the error will show you where the method (which you've just deleted) is added the the handler.

Changing the name just changes the name. The property that tells it what to do is still the same.

I think, and I'm getting far too used to Intellitype to be sure. That you can do something like. I'm not on my usual PC now though so I can't check.

button1.EventHandlers.clear()
button1.EventHandlers += new eventhandler(button2_click);

although if you are going to do this you might as well call the method something useful. Instead of button2_click which could be confusing

Bear with me if this makes no sense i'm quite hungover

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.