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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:26:48+00:00 2026-05-19T02:26:48+00:00

I have a program that dynamically creates controls when it starts, it works just

  • 0

I have a program that dynamically creates controls when it starts, it works just fine when the code to do this is in the class of the actual form. I tried moving the code to a separate class and found that I could not use Controls.Add(). How can I add controls to the Form from a separate class? This is what I have so far:

            TextBox txtbx = new TextBox();
            txtbx.Text = "asd" + x.ToString();
            txtbx.Name = "txtbx" + x.ToString();
            txtbx.Location = new Point(10, (20 * x));
            txtbx.Height = 20;
            txtbx.Width = 50;
            Controls.Add(txtbx);

Error 1 The name ‘Controls’ does not exist in the current context

  • 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-19T02:26:48+00:00Added an answer on May 19, 2026 at 2:26 am

    Controls is actually a property exposed by the Control class (and therefore the Form class, since it inherits from Control) , and it represents a collection of all the controls that have been added to that particular instance of the form class.

    That is why you can’t use it from another class, because you don’t have a reference to the Form object to which you’re trying to add the controls in the other class. That’s what it means by “does not exist in the current context”.

    You need to pass the instance of the form to which you want to add the controls as a parameter to the method in the class that will add the controls:

    public void AddControls(Form frm)
    {
        TextBox txtbx = new TextBox();
        txtbx.Text = "asd" + x.ToString();
        txtbx.Name = "txtbx" + x.ToString();
        txtbx.Location = new Point(10, (20 * x));
        txtbx.Height = 20;
        txtbx.Width = 50;
        frm.Controls.Add(txtbx);
    }
    

    But you should probably reconsider the design of your application if you’re forced into a position like this. You really shouldn’t be adding controls to a form from a separate class because that increases the amount of coupling between your UI and ancillary classes, which you should strive to minimize to every extent possible. In general, most of the time that you find something is particularly difficult to do, that should send up a red flag that you might be trying to do it the wrong way.

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

Sidebar

Related Questions

I have a flash program that loads movie clips dynamically and sometimes they want
I have a program that creates a Windows user account using the NetUserAdd() API
I have a program that when it starts, opens a winform (it is the
I'm thinking of having a program that dynamically creates new tables as the need
I have 4 UITextFields that I'm dynamically creating, in the viewDidLoad, which works good.
I have an ANSI C program that dynamically loads a .so file using dlopen()
I have a program that spits out both standard error and standard out, and
I have a program that uses the mt19937 random number generator from boost::random. I
I have a program that spits out an Excel workbook in Excel 2003 XML
I have a program that monitors debug messages and I have tried using a

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.