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 code/class/script in JAVA that I want to be executed when someone
I have quite a few records in my program that I end up putting
At work it is requested that I make a program that can broadcast a
So let's say I have an MPI program with 2 processes, rank 0 and
I have a problem with a simple program im making with fork and pipes
I have two files from a C program. I guess both belong to quite
I've read that smalltalk is a joy to program with, because the toolset makes
So I have two columns on named program and one with cost values. The
I'm quite new to android, but basically I want to set up a program
I'm a new Java programmer, and I've been trying to setup a simple Swing

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.