1

Is it possible to add Controls via a loop to a Control? The difficulty is the name: it is the same in each circulation, so I get only one Control named hu and not 10. How can I modify the code to get 10 Borders?

for (int i = 0; i < 10; i++)
{
    Border hu = new Border();
    hu.Width = 10;
    hu.Height = 10;
    hu.Margin = new Thickness(10 * i);
    hu.Background = (Brush) typeof(Brushes).GetProperty(Felder[i]).GetValue(null, null);
    mastergrid.Children.Add(hu);
}
5
  • I take it this is WPF? Added. Commented Dec 13, 2011 at 17:52
  • What error are you getting in the code you posted? Commented Dec 13, 2011 at 17:54
  • 1
    i would not recommend this way of creating controls in WPF. Commented Dec 13, 2011 at 17:55
  • 1
    Note that the variable name "hu" in the loop is in no way related to each control's Name property. Commented Dec 13, 2011 at 17:55
  • 1
    The above should probably just work. Maybe they are overlapping? You could try a stack panel instead of a grid for mastergrid. Or try adding columns and rows. Commented Dec 13, 2011 at 17:56

2 Answers 2

3

At first, yes it is possible. What you can do is to set the Name like this

hu.Name = string.Format("Border_{0}", i);

Maybe not in the case of border, but for example a button. How can i get the data that is responsible for the control in an event, because you bind one event to several controls. Usually use the Control.Tag Property to add additional data and use them in a event. For example:

public MainWindow()
{
    InitializeComponent();

    for (int i = 0; i < 10; i++)
    {
        Border hu = new Border();
        hu.Width = 10;
        hu.Height = 10;
        hu.Margin = new Thickness(10 * i);
        hu.Tag = anyObject;
        hu.MouseDown += new MouseButtonEventHandler(hu_MouseDown);
    }
}

void hu_MouseDown(object sender, MouseButtonEventArgs e)
{
    Border b = (Border)sender;
    // b.Tag contains your "anyObject"
}

Update

Be aware that the name of the variable that holds your instance, in your case "hu", is NOT the same as the Name Property of your Control.

Sign up to request clarification or add additional context in comments.

3 Comments

ah ok, makes sense. i have used that in almost every answer in the past. from now i will leave it. Thanks!
I think it's not accurate to say that hu is the name of the instance. It's the name of the variable holding the instance. There could be several variables holding the same instance.
good point @svick i will update my answer to be more accurate.
0

In your code, you don't create several controls named "hu". You create several unnamed controls, the name of the local variable doesn't matter. And doing so should be completely fine, unless you need the name. If you do, you can just create some unique name for each control.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.