I'm having a bit of an issue in开发者_如何学Go doing this. I need to create windows forms programmatically from my startup class.
I have a foreach loop like this:
int count = 1;
foreach (User user in User.AllUsers)
{
Form myForm = new Form();
myForm.Text = "User: " + count;
myForm.Show();
count ++;
}
The problem is that I need the "myForm" name to be variable so each created form will have a different name that I can later refer to, like; myForm1, myForm2, myForm3 and so on. I tried a variety of things but you can't assign a variable to that name.
How do you do this?
If you want to hold references to multiple objects, you store them as elements in a collection, not in multiple separate variables.
int count = 1;
List<Form> formInstances = new List<Form>();
foreach (User user in User.AllUsers)
{
Form myForm = new Form();
myForm.Text = "User: " + count;
myForm.Show();
formInstances.Add(myForm);
count ++;
}
However, as Oded has already commented, you can't use WinForms in ASP.NET, so you need to think again about exactly what you're trying to achieve, then get a good book and learn about .NET programming.
精彩评论