0

I have a code like this:

MyUserControl[] myControl = new MyUserControl[10];

when Form load:

for( int i = 0; i < 10; i++ )
{
    myControl[i] = new MyUserControl();
    myControl[i].label1.Text = "Title";

    this.Controls.Add ( myControl[i] );
}

now it is show normally.

after then press button like below:

private void Button1_Click(object sender, EventArgs e)
{
    myControl[0].label1.Text = "Other Title";
}

When i see the debug mode,value was added normally, but lable1 text not show "Other Title".

So, I try to below method, but not work anything.

myControl[0].label1.Update();
myControl[0].label1.Invalidate();
myControl[0].label1.Refresh();

Please, kindly advise.

Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
  • 1
    Maybe you ought to consider setting the Location property so they are not all on top of each other and you can only see the one on top. Hard guess, such a problem should be pretty evident. – Hans Passant Sep 02 '16 at 09:32
  • Works perfectly well when I try it. Is this your only initialization code? Because it seems they overlay each other when you don't assign a proper location (x,y). – stefankmitph Sep 02 '16 at 09:32
  • Works fine! See [this](http://i.stack.imgur.com/vN4gS.gif) – Raktim Biswas Sep 02 '16 at 09:34
  • Location code already exist. That code is simple sample to show. Not work currently. I had test just one usercontrol but it is same. I assumed there is other reason. – Brooklyn Smith Sep 02 '16 at 09:56

2 Answers2

0

I don't understand your concept Hope this example work for you

private void Button1_Click(object sender, EventArgs e)
{
    //myControl[0].label1.Text = "Other Title";

    MyUserControl ctrl = null;
    foreach (var c in this.Controls)
    {
        if (c.GetType().Name == "MyUserControl")
        {
            var myctrl = (MyUserControl)c;
            if (myctrl.Text == "Title")
            {
                ctrl = myctrl;
                break;
            }
        }
    }
    if (ctrl != null)
    {
        ctrl.Text = "Other Title";
    }
}

This is a very simple example that code will work and you have to enhance as you like

Nitesh Shaw
  • 216
  • 2
  • 17
0

Thank you for everyone. I got a root cause in this matter.

myControl[0].label1.Text = "Other Title";
myControl[0].BringToFront(); // <- Solved problem with this code.

I don't know why z-order was twisted.