how to reset text properties of a runtime created label

hello,
is there a way to reset text properties of a runtime created label in a panel
without using the "sender" object.
private void button1_Click(object sender, EventArgs e)
        {
            TextBox cel = new TextBox();
            cel.Name = "naam";
            cel.Top = 20;
            cel.Left = 20;
            cel.Width = 55;
            cel.Height = 15;
            cel.Text = "0";
            cel.TextChanged += Text_Changed;
            panel1.Controls.Add(cel);
 
 
            Label label = new Label();
            //cel() = new TextBox();
            label.Name = "naam";
            label.Top = 50;
            label.Left = 20;
            label.Width = 55;
            label.Height = 15;
            label.Text = "0";
            label.TextChanged += Text_Changed;
            panel1.Controls.Add(label);
        }
 
        private void Text_Changed(object sender, EventArgs e)
        {
            TextBox cel = sender as TextBox;
            cel.Text = "25";
            cel.BackColor = SystemColors.ControlDark;
//here i want to change the text property of the label
        }
    }

Open in new window

GeertDeJaegherAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SwissKnifeCommented:
It looks like you have a button1 on a form to start your action. But the action is not very usful.

Every time the button1 is clicked, you add a textBox in the form exactly at the same position.
And also a Label. And now you wat to alter the label if the user enters some text in the top most TextBox.

It is not useful to add a chenged event handler to a label, because the user can not change a label.

If you want to change the text property of the label if the textBox test is changed, you have to store the reference of the created label in a class member.
Now you have acces to the label in the changed event handler of the TextBox
class Example {
 
Label label;
 
private void button1_Click(object sender, EventArgs e)
        {
            TextBox cel = new TextBox();
            cel.Name = "naam";
            cel.Top = 20;
            cel.Left = 20;
            cel.Width = 55;
            cel.Height = 15;
            cel.Text = "0";
            cel.TextChanged += Text_Changed;
            panel1.Controls.Add(cel);
 
 
            label = new Label();
            //cel() = new TextBox();
            label.Name = "naam";
            label.Top = 50;
            label.Left = 20;
            label.Width = 55;
            label.Height = 15;
            label.Text = "0";
            label.TextChanged += Text_Changed;
            panel1.Controls.Add(label);
        }
 
        private void Text_Changed(object sender, EventArgs e)
        {
            TextBox cel = sender as TextBox;
            cel.Text = "25";
            cel.BackColor = SystemColors.ControlDark;
            
//here i want to change the text property of the label
            label.Text = "Hello"
        }
    }

Open in new window

0
GeertDeJaegherAuthor Commented:
the intention is to create  number of
textboxes with a label under them. in the label i have to display
total amount of the above column,also near the rows same thing
looks like former visual basic datagrid.
        private void btnflex_Click(object sender, EventArgs e)
        {
            
            paneel.Controls.Clear();
            int akoppen, arijen;
   
                txtcol.Focus();
                return;
            }
            Label[] koppen = new Label[akoppen];
            Label[] totalen = new Label[akoppen];
            TextBox[,] cellen = new TextBox[arijen, akoppen];
            Label[]RijTotalen = new Label[arijen];
 
            int links = 10;
            int bovenkant = 5;
            int Kolomnr = 1;
            int Rijnummer = 1;
            int Celnummer = 1;
            int Kolomnummer = 1;
            for (int k = 0; k < akoppen; k++)
            {
                koppen[k] = new Label();
                koppen[k].Left = links;
                koppen[k].Top = bovenkant;
                koppen[k].Width = 55;
                koppen[k].Height = 15;
                koppen[k].BorderStyle = BorderStyle.FixedSingle;
                koppen[k].Text = "Kolom " + Kolomnr.ToString();
                koppen[k].TextAlign = ContentAlignment.MiddleCenter;
                paneel.Controls.Add(koppen[k]);
                bovenkant += 20;
                Kolomnr += 1;
                for (int r = 0; r < arijen; r++)
                {
                    
                    cellen[r, k] = new TextBox();
                    cellen[r, k].Name = Celnummer.ToString();
                    cellen[r, k].Top = bovenkant;
                    cellen[r, k].Left = links;
                    cellen[r, k].Width = 55;
                    cellen[r, k].Height = 15;
                    cellen[r, k].Text = "0";
                    cellen[r, k].TextAlign = HorizontalAlignment.Right;
                    cellen[r, k].BorderStyle = BorderStyle.FixedSingle;
                    cellen[r, k].KeyPress += new KeyPressEventHandler(druktoets);
                    cellen[r, k].TextChanged += Text_Changed;
                    Celnummer += 1;
                    paneel.Controls.Add(cellen[r, k]);
                    bovenkant += 20;
                }
                links += 60;
                bovenkant = 5;
            }
            bovenkant = (arijen *20) +30;
            links = 10;
            for (int k =0;k<akoppen;k++)
            {
                totalen[k] = new Label();
                totalen[k].Name = "k"+Kolomnummer;
                totalen[k].Left = links;
                totalen[k].Top = bovenkant;
                totalen[k].Width = 55;
                totalen[k].Height = 15;
                totalen[k].BorderStyle = BorderStyle.FixedSingle;
                totalen[k].Text = "0";
                totalen[k].TextAlign = ContentAlignment.MiddleRight;
                paneel.Controls.Add(totalen[k]);
                links += 60;
                Kolomnummer += 1;
                
            }
            links = (akoppen * 60) + 12;
            bovenkant = 27;
            for (int r = 0; r < arijen; r++)
            {
                RijTotalen[r] = new Label();
                RijTotalen[r].Name = "r" + Rijnummer;
                RijTotalen[r].Left = links;
                RijTotalen[r].Top = bovenkant;
                RijTotalen[r].Width = 55;
                RijTotalen[r].Height = 15;
                RijTotalen[r].BorderStyle = BorderStyle.FixedSingle;
                RijTotalen[r].Text = "0";
                RijTotalen[r].TextAlign = ContentAlignment.MiddleRight;
                paneel.Controls.Add(RijTotalen[r]);
                bovenkant += 20;
                Rijnummer += 1;
            }
               
                  private void Text_Changed(object sender, EventArgs e)
        {
          int k, r,c;
          int kolom, rij;
              k = int.Parse(txtcol.Text);
              r = int.Parse(txtrow.Text);
              TextBox box = sender as TextBox;
              c = int.Parse(box.Name);
              kolom = (c / r) + 1;
              if (c % r == 0)
                  kolom -= 1;
              rij = c % r;
              if (c % r == 0)
                  rij = r;
            
              //   box.BackColor = SystemColors.ControlDark;
  
           }
 
  
 
        }

Open in new window

example.JPG
0
AUmidhCommented:
>>is there a way to reset text properties of a runtime created label in a panel
>>without using the "sender" object.

this.panel.Controls("control Name").Text="New Text"
0
GeertDeJaegherAuthor Commented:
i have solved the problem myself,
had to initiate the arrays "public" like
 public Label[] koppen ;
 public Label[] totalen ;
 public TextBox[,] cellen ;
 public Label[] RijTotalen ;
now i have access to the object properties
thanks for the reactions
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vee_ModCommented:
Closed, 250 points refunded.
Vee_Mod
Community Support Moderator
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.