unable to update label that was dinamcly created...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yoavkarmon
    New Member
    • Nov 2010
    • 1

    #1

    unable to update label that was dinamcly created...

    Code:
    private void domainUpDown1_SelectedItemChanged(object sender, EventArgs e)
            {
                Label[] labeli = new Label[2];
                for (int i = 0; i < 2; i++)
                {
    
                    labeli[i] = new Label();
                    labeli[i].Parent = this;
                    
    
                }
                labeli[0].Size = new Size(94, 17);
                labeli[0].Location = new Point(145, 114);
                labeli[0].Parent = this;
    
                labeli[1].Size = new Size(94, 17);
                labeli[1].Location = new Point(145, 114);
    
                labeli[0].Text = "clk Freq";
                labeli[1].Text = "inport name";
    
                labeli[0].Hide();
                labeli[1].Hide();
                this.Controls.AddRange(labeli);
    
                if (domainUpDown1.SelectedIndex==0)
                {
                    labeli[0].Show();
                    labeli[0].Visible = true;
                    labeli[0].Update();
                    labeli[0].Refresh();
                    
                }
                else if (domainUpDown1.SelectedIndex == 1)
                {
                    labeli[0].Hide();
                    labeli[0].Visible = false;
                    labeli[0].Update();
                    labeli[0].Refresh();
                }
                Application.DoEvents();
                this.Update();
                this.Refresh();
            }
    *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.
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    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.

    Comment

    Working...