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 6725657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:50:02+00:00 2026-05-26T09:50:02+00:00

This may seem totally unreasonable to ask, but I have been designing a multi-panel,

  • 0

This may seem totally unreasonable to ask, but I have been designing a multi-panel, real device simulator, that has many different screens and my current approach is to add all the screen objects from the code only and dispose them when I switch to another screen.

I have some fixed objects, that are the real device buttons that are already defined and in place. The thing is, I am separating each panel construction in methods, for example: buildLogin(), buildMainScreen(), etc, and I need to edit some of the screen objects from those methods, like changing the color of an enabled function label to green if enabled or white if disabled.

My question is: would it be possible to declare an object from a method that would be accessible in the whole class, like if it were defined in the variable declaration section? It would be something like the GLOBAL in PHP.

I can’t declare it on top of everything like they would always be because when I dispose the objects, I can’t “re-create” them, because of parenting, or re-using a disposed object or something…

[EDIT] Sample code:

public partial class frmMain : Form
{
    //I could as well do this:
    //Button button1 = new Button();

    public frmMain()
    {
         buildLogin();
    }

    private void buildLogin()
    {
        Panel panel1 = new Panel();
        Controls.Add(panel1);

        //But then, there is no way to do this:
        // if (button1.IsDisposed == true) //because of the panel, or smthing
        Button button1 = new Button();
        panel1.Controls.Add(button1);

        button1.Click += (s, f) => { panel1.Dispose(); buildMainMenu(); };
    }

    private void buildMainMenu()
    {
        Panel panel2 = new Panel();
        Controls.Add(panel2);

        Button button2 = new Button();
        panel2.Controls.Add(button2);
    }

    //This was created from the Designer and is class-scoped
    private void btn_Frame_TSK1_Click(object sender, EventArgs e)
    {
        //Here, I have no access to the objets I've created programatically.
        //button1.Text = "Text changed!";
    }
}
  • 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-05-26T09:50:03+00:00Added an answer on May 26, 2026 at 9:50 am

    If you want to make sure things are always completely dynamic and always done in the code behind, you may want to look at searching for the controls you’ve created in the Controls collection.

    For example, make sure to give button1 an ID value (button1.ID=”textUpdatedButton”) that will identify it uniquely from other controls you create. Then use FindControl or search on the Controls collection to find the control with the ID you want to locate in your event handler.

    //This was created from the Designer and is class-scoped
    private void btn_Frame_TSK1_Click(object sender, EventArgs e)
    {
        Control control = this.FindControl("textUpdatedButton");
        if(control != null && control is Button){
            Button button1 = (Button)control;
            button1.Text = "Text changed!";
        }
    }
    

    Alternatively, to make things look more like a variable, you can use a Property to hide the control finding (as mentioned previously):

    private Button Button1 {
        get { return (Button)this.FindControl("textUpdatedButton"); }
    }
    
    //This was created from the Designer and is class-scoped
    private void btn_Frame_TSK1_Click(object sender, EventArgs e)
    {
        if(this.Button1 != null){
            this.Button1.Text = "Text changed!";
        }
    }
    

    The actual implementation will vary with how you build up your controls, but essentially this approach can let you build everything dynamically in your code behind if you need to do it that way. Just remember to use identifiers to let you find things later.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may seem like a stupid question, but I've been looking everywhere to fix
This may seem like a dumb question but I have IDLE and I'm trying
This may seem silly but I seem to have forgotten the order of replacement
This may seem simple, but somehow it isn't. I have a table of historical
This may seem a rather silly question, but I have a header file for
This may seem a silly question, but having been pondering it for a few
this may seem like a stupid question but I have a query string which
This may seem like a very basic question, but its been in my head
This may seem like an obvious problem, but I have looked on the facebook
This may seem like a simple questiom, but I have the following html <div

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.