Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8919499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:02:33+00:00 2026-06-15T06:02:33+00:00

I have a hopefully simple question, for which I could not google up any

  • 0

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";
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T06:02:35+00:00Added an answer on June 15, 2026 at 6:02 am

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is hopefully a simple question: I have an OpenGL texture and would like
I have a (hopefully) a relatively simple question. How do I tell Android which
I have a simple question hopefully - how does one free memory which was
I have a hopefully simple MySQL query question which is eluding me at late
I have a simple question, which should hopefully have a quick answer. The code
I have a (hopefully) simple question. Is it possible to use SSHD tunneling to
I'm just getting started with Require.JS and I have a (hopefully) simple question. I'm
Hopefully a simple question although one I have found impossible to answer myself using
hopefully a pretty simple question this time. I have a Select method in a
I'm new to Xcode development and have a (hopefully simple) question: I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.