1

I have a hopefully simple question, for which I could not google up any solution: I would like to add labels buttons textboxes at runtime, which i can in the constructor of my Form, but I cannot access them outside of the constructor.

Something like this:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        Label changeMe = new Label();
        changeMe.AutoSize = true;
        changeMe.Left = 50; changeMe.Top = 50;
        changeMe.Text = "Change this text from other function";
        changeMe.IsAccessible = true;

        //changeMe.Name = "changeMe";

        this.Controls.Add(changeMe);
    }

    private void btn_changeLabelText_Click(object sender, EventArgs e)
    {
        //Would like to achieve this:
        //changeMe.Text = "You changed me !! ";

        //But I found only this solution:
        //Label l;   l = (Label)this.Controls.Find("changeMe", true)[0];   l.Text = "You changed Me";

    }
}

The solution I commented out is the only one I found, but I can't believe there isn't any better one then that. Is there a way to make my controls public for example? Whats a good way of solving this problem?

(The number of controls vary on every time I call my Dialogbox I am trying to design)

Thanks

EDIT --------------------------

After accepting Adil's answer, I stayed with the following solution, which I only find better as the originally outcommented this.Control.Find way, because I also want to have "n" Textboxes, and in thie way I can easily loop through them and read inputs.

    public partial class Form1 : Form
{
    public struct labels { public Label lbl; public int id; }
    List<labels> lbls = new List<labels>();

    public Form1()
    {
        InitializeComponent();
        Label changeMe = new Label();
        changeMe.AutoSize = true;
        changeMe.Left = 50; changeMe.Top = 50;
        changeMe.Text = "Change this text from other function";
        changeMe.IsAccessible = true;


        this.Controls.Add(changeMe);
        labels newlabel = new labels();
        newlabel.id = 137; newlabel.lbl = changeMe;
        lbls.Add(newlabel);

    }

    private void btn_changeLabelText_Click(object sender, EventArgs e)
    {
        lbls.Find(i => i.id == 137).lbl.Text = "You changed me";
    }
}
Onsightfree
  • 307
  • 3
  • 11

1 Answers1

4

You declared the label inside constructor that makes it accessible only in constructor, ceclare label out side constructor at class scope as class member.

Label changeMe = new Label();

public Form1()
{
    InitializeComponent();
    Label changeMe = new Label();
    changeMe.AutoSize = true;
    changeMe.Left = 50; changeMe.Top = 50;
    changeMe.Text = "Change this text from other function";
    changeMe.IsAccessible = true;

    //changeMe.Name = "changeMe";

    this.Controls.Add(changeMe);
}
Adil
  • 146,340
  • 25
  • 209
  • 204
  • What if I have to make n labels? If I am not mistaken I can only give n as a parameter to the constructor. – Onsightfree Dec 02 '12 at 16:13
  • You can declare array of labels and initialize them using for loop, this could be helpful, http://stackoverflow.com/questions/962449/array-of-labels – Adil Dec 02 '12 at 16:17
  • This post states how to use loop to create labels, http://stackoverflow.com/questions/7170673/how-can-i-create-labels-inside-a-for-loop – Adil Dec 02 '12 at 16:22
  • I have a code now, that is running, based on your answer regarding the scope. However I am not sure, setting up lists and arrays for this does not seem more elegant to me, then the this.control.find version which I commented out, where I can also define an ID, as control.name. Is there really no other way? If you say so, I am accepting your answer – Onsightfree Dec 02 '12 at 16:31
  • You can use loop variable to make unique names for all labels like changeMe.Name = "changeMe" + i.ToString(); and it will be unique identifier for each label. – Adil Dec 02 '12 at 16:40
  • Thanks for your help, accepted as solution, and updated my post with the way I implemented it. – Onsightfree Dec 02 '12 at 16:45