Sunday, August 2, 2009

I have a for loop in C# to add controls to a new form. How can I name the new controls?

So, basically I have a for loop that goes through the array of selected items from a list box. I want to then open a new form and create a new label control for each selected item. How can I create these inside the loop?

I have a for loop in C# to add controls to a new form. How can I name the new controls?
foreach(object o in listBox1.SelectedItems)


{


Label newLabel=new Label();


newLabel.Text=o.ToString();


//coordinates here





this.Controls.Add(newLabel);


}
Reply:foreach( ------ ) {


Label lbl = new Label();


lbl.Text = ""; // set Text for your label


lbl.length = .. // set other properties for your label here





//add the label to the Form:


this.Controls.Add(lbl);


}





for more info you can check:


http://support.microsoft.com/kb/319266





Hope this helps.
Reply:For my program, I used XML serialization, and had a property for the name...that seemed like the easiest way. You'll have to learn how to do this from a book or a website if you don't know how to already...kinda long to explain here.


No comments:

Post a Comment