By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,275 Members | 920 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,275 IT Pros & Developers. It's quick & easy.

unable to update label that was dinamcly created...

P: 1
Expand|Select|Wrap|Line Numbers
  1. private void domainUpDown1_SelectedItemChanged(object sender, EventArgs e)
  2.         {
  3.             Label[] labeli = new Label[2];
  4.             for (int i = 0; i < 2; i++)
  5.             {
  6.  
  7.                 labeli[i] = new Label();
  8.                 labeli[i].Parent = this;
  9.  
  10.  
  11.             }
  12.             labeli[0].Size = new Size(94, 17);
  13.             labeli[0].Location = new Point(145, 114);
  14.             labeli[0].Parent = this;
  15.  
  16.             labeli[1].Size = new Size(94, 17);
  17.             labeli[1].Location = new Point(145, 114);
  18.  
  19.             labeli[0].Text = "clk Freq";
  20.             labeli[1].Text = "inport name";
  21.  
  22.             labeli[0].Hide();
  23.             labeli[1].Hide();
  24.             this.Controls.AddRange(labeli);
  25.  
  26.             if (domainUpDown1.SelectedIndex==0)
  27.             {
  28.                 labeli[0].Show();
  29.                 labeli[0].Visible = true;
  30.                 labeli[0].Update();
  31.                 labeli[0].Refresh();
  32.  
  33.             }
  34.             else if (domainUpDown1.SelectedIndex == 1)
  35.             {
  36.                 labeli[0].Hide();
  37.                 labeli[0].Visible = false;
  38.                 labeli[0].Update();
  39.                 labeli[0].Refresh();
  40.             }
  41.             Application.DoEvents();
  42.             this.Update();
  43.             this.Refresh();
  44.         }
  45.  
*I maked sure, usung brake points, that i am entering the if statments.
*I used labeli[0].Visible = false; / labeli[0].Hide(); Separately - didnt work.
Nov 27 '10 #1
Share this Question
Share on Google+
1 Reply


GaryTexmo
Expert 100+
P: 1,501
Can you be more clear on the problem? What exactly is going wrong? I can infer from the code, but it's easier if you just tell us what the problem is... "didn't work" doesn't cut it ;)

Do these labels exist on your form already before this method is run? You're creating new labels in this method every time, so every time the selected item changes on the control that event is tied to, the old labels will still be there and just get overlapped with new ones. If the one before was visible, but the new one is invisible, it will still appear to the old version of the control. You'll also be filling up your form's control list with objects that are no longer useful.

You should make that array a class member and initialize it in the constructor or form load event, adding to the form at that time. Then your selected item changed event can toggle the visibility from there.
Nov 29 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.