Hi,
Simple question.How do you rename a label?
I'm trying LAbel1.Name="sk";

Please someone tell me the correct code?

label1.Text = "label name here";
this.label4.Name = "label4"; // This causes problems, you would have to change all references for it. Hopefully you're just changing the text of it using label1.Text

To find all the properties in code form, click on the FormDesigner.cs in the solution explorer.

Edited 5 Years Ago by lxXTaCoXxl: n/a

I have to change the name of the label and not the text in the label.

Hi,
Simple question.How do you rename a label?
I'm trying LAbel1.Name="sk";

Please someone tell me the correct code?

Why would you rename it? To use a Name property.
That is not only that you would like to set a new Text property for the label?
like:

label1.Text = My new text";

?
If you want to rename it, simpy do:

label1.Name = "NewName";
//check it:
string _name = label.1Name;

That's what I was saying Mitja, renaming an object on the form would be a really terrible idea. I have no idea why someone would want to do it. So I just gave both code snippets.

label1.Name = "new name";

// Test the rename.
string myLabelName = label1.Name; // Save it to a string.
MessageBox.Show(myLabelName, "Testing ReName Label"); // Print the string in a message box.

Setting the name property at runtime doesn't affect the running code. It might affect reflection of the assembly, but I don't know why you would want to do that in the first place.

Doing it in the designer just renames the control.

I have no idea why either. It seems like a very awkward approach to letting a user create their own objects on a form. Which is quite simple compared to how I thought it would be. :D

In properties it has "NAME" which has a default of label1 just change it to your desire name for the label.

Hope this can help!

label1.Name = "hi";

It has no effect on code you can only use a control by the name you set in designer. Only when you want to display the controls name it will be displayed hi.

so it is better to set the name in designer or on the form through control-properties.

I dont have access to windows form designer I have to do everything through the code.My entire code doesn't work because someone has created two labels with same name.

I dont have access to windows form designer I have to do everything through the code.My entire code doesn't work because someone has created two labels with same name.

Why not? It's a free download

Look there in your solution explorer there you will see all the files included in your project including your forms on clicking your form_name you will find 2 files one of which will be form_name.designer.cs open it and go to the region "Windows Form Designer generated code"... Locate your label there and rename it... Hope it will help you

Edited 5 Years Ago by abelLazm: n/a

This article has been dead for over six months. Start a new discussion instead.