Click to See Complete Forum and Search --> : C# Forms - Create Label on Runtime


bixel
April 21st, 2009, 07:36 PM
I created a menu - with a Add New tab and a function.

When Add New is used I want it to open a editable Text box. You enter the Text and a new Label is created. Its for creating an inventory list like this

Apples
Oranges
Grapes
Flour
Oat Grain

etc....

So when Add New is selected from the Menu you can add another item. But I dont know the correct code that fires the Menu > Add New -- is it Menu.somethign or MenuItem.someting???

All I have is this at the moment (srry)

#region //Add New
private void AddNew(object sender, EventArgs e)
{

}
#endregion

pgrammer
April 22nd, 2009, 12:08 AM
I think you have pasted the wrong code example because I don't see what your example has to do with creating a Label at runtime. You have created an event handler. You need to map that to something or it will just be ignored.

As for the thread question, to create a label at run time you simply do:

Label lblName = new Label();
lblName.Text = "something";
// set Location here...
this.Controls.Add(lblName)


You can use this same method to create any text box or tab at runtime.

cilu
April 22nd, 2009, 07:13 AM
Why would you create labels for the items? How many can you create? Why don't you display the items in a list box or something? If you'll have 100 items how will your form look like?

bixel
April 23rd, 2009, 08:53 PM
Hey thats true- maybe list box is better