0

Background

I create a set of linklabel and label controls using a loop that uses data from a database as there content (Text).

Question

How do I then remove or change there visibility?

What I would Like to Happen?

On a button click event, I would like all of the link's and linklabel's text properties to be set to either null, or their visibility properties to be set as false.

Code

 private void getInfoStationID()
            {


    //SQL Connection Stuff

             for (int i = 0; i <= rowCount - 1; i++)
                            {

                                LinkLabel Linklabel = new LinkLabel();
                                Linklabel.Text = ds.Tables[0].Rows[i]                 ["code"].ToString();
                                Linklabel.Height = 15;
                                Linklabel.Width = 50;
                                Linklabel.AutoSize = true;
                                Linklabel.Location = new Point(10, (i + 1) * 30);
                                tabControl1.TabPages[0].Controls.Add(Linklabel);
                                // Add an event handler to do something when the links are clicked. 
                                Linklabel.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);

                                Label label1 = new Label();
                                label1.Text = ds.Tables[0].Rows[i]["name"].ToString();
                                label1.Height = 15;
                                label1.Width = 70;
                                label1.Location = new Point(100, (i + 1) * 30);
                                tabControl1.TabPages[0].Controls.Add(label1);

                                Label label3 = new Label();
                                label3.Text = ds.Tables[0].Rows[i]["toc"].ToString();
                                label3.Height = 15;
                                label3.Width = 50;
                                label3.Location = new Point(240, (i + 1) * 30);
                                tabControl1.TabPages[0].Controls.Add(label3);
                            }

                  }

 private void clearAllBtn_Click(object sender, EventArgs e)
        {
           //Would like this to clear all previously drawn labels and linklabels

        }
Dan Cundy
  • 2,649
  • 2
  • 38
  • 65
  • what is the method or event that the code posted belongs too..? please show full method signature are you familiar with the `Controls Class` depending on your logic you could use / implement a `foreach(Controls ctrl in Controls){}` type of construct and do what you need to do there checking to make sure of course that the type of control is of LinkLabel `Stackoverflow` has tons of examples on how to use the Controls class in a foreach loop..this can also be done using a lambda statement as well – MethodMan Jan 26 '15 at 21:49

2 Answers2

1

Simply add the dynamic controls to a List so you have a quick reference to them:

        // out at CLASS/FORM level:
        private List<Control> MyControls = new List<Control>();

        // ... some method ...
            for (int i = 0; i <= rowCount - 1; i++)
            {
                LinkLabel Linklabel = new LinkLabel();
                MyControls.Add(Linklabel);
                // ... rest of your code ...

                Label label1 = new Label();
                MyControls.Add(label1);
                // ... rest of your code ...

                Label label3 = new Label();
                MyControls.Add(label3);
                // ... rest of your code ...
            }

Now you can use that List from somewhere else:

    private void clearAllBtn_Click(object sender, EventArgs e)
    {
        foreach(Control ctl in MyControls)
        {
            ctl.Visible = false; // or something else
        }
    }

*Don't forget to dispose of those controls and empty the list if you decide to create a new set of dynamic controls. If you want to completely get rid of them:

    private void clearAllBtn_Click(object sender, EventArgs e)
    {
        foreach(Control ctl in MyControls)
        {
            ctl.Dispose();
        }
        MyControls.Clear();
    }
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
1

You can loop through all controls on a certain tabpage. You could use a open generic function to make the code nice and clean. Like this:

private void HideControls<TControl>(Control parentControl)
    where TControl : Control
{
    var controls = parentControl.Controls.OfType<TControl>();

    foreach (var control in controls)
    {
        control.Visible = false;
    }
}

And use it like this:

private void button1_Click(object sender, EventArgs e)
{
    this.HideControls<Label>(tabControl1.TabPages[0]);
    this.HideControls<LinkLabel>(tabControl1.TabPages[0]);
}

You could even refactor this to a nice extension method:

public static class ControlExtensions
{
    public static void HideControlsOfType<TControl>(this Control parentControl)
        where TControl : Control
    {
        var controls = parentControl.Controls.OfType<TControl>();

        foreach (var control in controls)
        {
            control.Visible = false;
        }
    }
}

and use like:

 this.tabControl1.TabPages[0].HideControlsOfType<Label>();
Ric .Net
  • 5,540
  • 1
  • 20
  • 39