0

i don't want to waste your time with introductions, am just going to ask the question straight up.
am trying to make a dynamic form, where i can change what is inside of it through the parameters passed when the form is called, my problem occurs when i want to do something with the stuff i created outside of the creation method,
here is an extremely simplified example to hopefully better communicate the problem:

 public partial class MyForm : Form
{
    public MyForm()
    {
        InitializeComponent();
        CreateLabels();
        CreateButtons();
    }
    // creates and locates labels in the form
    private void CreateLabels()
    {
        Label Lbl = new Label();
        Lbl.Text = "help please"
        Lbl.Name = "MainLabel";
        Lbl.Location = new Point(this.Width / 2, 30);
        this.Controls.Add(Lbl);
    }
    // creates and locates buttons in the form
    private void CreateButtons()
    {
        Button Btn = new Button();
        Btn.Name = "Mainbuttom";
        Btn.Location = new Point(this.Width / 2, 100);
        Btn.BackColor = Color.Red;
        this.Controls.Add(Btn);
        Btn.Click += new EventHandler(button_click);
    }
    // does something when the user presses the button
    private void button_click(object sender, EventArgs e)
    {
        // do something with the labels created, e.g : save the text inside, change the text, etc.
    }
}

ya, so i want to access the labels created somehow.
i wasted so much time on this i hope its not as simple as changing a parameter,
anyways, thanks for your time.

ammdado
  • 1
  • 1
  • Create fields to hold your controls. E.g. `private Label lbl; ... private void CreateLabels() { lbl = new Label(); lbl.Text = .... }`. You can then access those from within methods like `button_click` – canton7 May 18 '21 at 12:57
  • thx for the fast reasons, i'm calling the "createLabels" method more than once in the real form, with this approach am going to need to add an extra field for each additional label i want, is there a way to set a name for each label so i can access them individually – ammdado May 18 '21 at 13:02
  • Build a UserControl that contains a Button and a Label. Or use a TableLayoutPanel with two Columns, so the Label is always in the Cell next to the related Button. Or make `CreateLabels()` return the Handle of the Label, then set it as the Tag of the Button (`Control.FromHandle()` does the rest). Or use a `List` that associates a Button to a Label. Or... – Jimi May 18 '21 at 13:13
  • thx for your help my friend, to my surprise, there is a way to search a form for specific objects (labels, buttons, etc) if they where named at creation, i just found out about it from the link provided with the "question removed" message, just using `this.controls.find()` – ammdado May 18 '21 at 13:29
  • Either you have to 'find' those controls or you can put them into a `Dictionary` and access them by name. – TaW May 18 '21 at 13:40
  • you see, the problem is not with knowing the name of the objects, the problem is with accessing them. even if u know the name, visual studio will just say "it does not exist", it's basically the same this with variable in methods, they are non existent in any other method unless u pass them through somehow. if u want copy the code to a new project and try simply calling the label, try something like `MainLabel.Text = "thanks for your help"` (note: try it outside of the "create Labels" method) – ammdado May 19 '21 at 11:53

0 Answers0