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

  • Home
  • SEARCH
  • 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 8479523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:03:03+00:00 2026-06-10T19:03:03+00:00

I have added my main class, from which I call buttons class, and that’s

  • 0

I have added my main class, from which I call buttons class, and that’s where it’s supposed to print out the button. The code is compiling ok, but I can’t see the actual button. Seems to me it didnt inherit the controls properties. Thank you!

  using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Windows.Forms;
  using System.Windows;

  //main class
  namespace Test
  {
    public partial class Form1 : Form
    {
     buttons button1;

     public Form1()
    {
        InitializeComponent();
        print_button();
    }

    private void print_button()
    {
        button1 = new buttons();
        button1.print();
    }


   }//form
  }//test

//---------------------------------------------------------------//

//buttons class
namespace Test
{
  public class buttons : System.Windows.Forms.Control

  class buttons
  {
     private Button button1;

     public buttons()
     {

     }

      public void print()
      {
          button1 = new Button();
          button1.Location = new System.Drawing.Point(82, 44);
          button1.Size = new System.Drawing.Size(977, 54);
          button1.Text = "next";
          Controls.Add(button1);
     }
  }//class
}//test
  • 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-10T19:03:04+00:00Added an answer on June 10, 2026 at 7:03 pm

    I have no idea what you are trying to accomplish with the code you have provided. In any event the reason this method doesn’t work:

    public void print()
    {
       button1 = new Button();
    
        button1.Location = new System.Drawing.Point(82, 44);
        button1.Size = new System.Drawing.Size(977, 54);
        button1.Text = "next";
        Controls.Add(button1);
    }
    

    is because the class ‘buttons’ does not define a Controls collection nor does it inherit from a class that exposes a controls collection. For example if you were to do this:

    public class buttons : System.Windows.Form
    {
        ...
    }
    

    Your code would then at least compile since the Form class exposes a Controls collection. It isn’t entirely apparent what you are trying to accomplish though so this solution may not suit your needs. Post more information as appropriate and I will do what I can to help you.

    —–Edit

    It seems like you are trying to refactor your Form1 class from one of your comments above.

    You could do something like this:

    public class Form1 : Form
    {
        public void foo()
        {
           Controls.Add(buttons.print());
        }
    }
    

    and modify your buttons class as:

    public class buttons
    {
       public static Button print()
       {
         Button btn = new Button();
         btn.Location = new System.Drawing.Point(82, 44);
         btn.Size = new System.Drawing.Size(977, 54);
         btn.Text = "next";
         return btn;
       }
    }
    

    This will then add a button to your main form’s control collection once it is called. As wrote though this method is useless since it can only produce the same button over and over. I would modify the method so that it looks like this

       public static Button print(Point buttonLocation, Size buttonSize, string buttonText)
       {
         Button btn = new Button();
         btn.Location = buttonLocation;
         btn.Size = buttonSize;
         btn.Text = buttonText;
         return btn;
       }
    

    And then the main form looks like this:

        public void foo()
        {
           Controls.Add(buttons.print(new System.Drawing.Point(82, 44), new System.Drawing.Size(977, 54), "button text"));
        }
    

    Does this help?

    — Edit 2 —

    I am supplying this edit as a strictly academic example of how you could keep your main class (Form) ‘cleaner’ per the OP’s request in the comments. I don’t suggest using this method in production and would instead recommend using a pattern such as MVC, MVP, or MVVP. I am excluding examples of those patterns here because I think they exceed the skill level of the OP at this point in time and would only lead to more confusion.

    Consider the following:

     public class buttons
     {
          private Form _form = null;
          public buttons(Form form)
          {
             _form = form;
          }
    
          public void print(Point buttonLocation, Size buttonSize, string buttonText)
          {
             Button btn = new Button();
             btn.Location = buttonLocation;
             btn.Size = buttonSize;
             btn.Text = buttonText;
             _form.Controls.Add(btn);
          }     
     }
    
     public class Form1 : Form
     {
         private buttons _buttons = null;
         public Form1()
         {
           _buttons = new buttons(this);
         }
    
         public void foo()
         {
            buttons.print(new System.Drawing.Point(82, 44), new System.Drawing.Size(977, 54), "button text");
         }
     }
    

    Now here is what is happening: when Form1 is instantiated it creates a new instance of the buttons class and passes a reference of itself into the buttons constructor (_buttons = new buttons(this)) internally the buttons class sets this reference to the local variable _form so, therefore, anything you do to the variable _form well be like if you were doing it directly to Form1. As you can see that is what happens in the print() method where a button is created and then added to the Controls collection of _form which is the same as calling Controls.Add from within Form1.

    Does this make sense to you?

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

Sidebar

Related Questions

I have a main view to which I have added a subview using the
I have one main project and another project which is added as a subprojects.
In my main Actionscript file i have a instance of a body class that
Disclaimer: I apologize that this question is so long. I have added code as
I have written a variant class, which will be used as the main type
I have a problem that, I have a serializable class which sets and gets
I have some code in a thread. The code's main function is to call
I have added a checker to ensure that all fields in a form have
I have a listview that contains values from webservice.Each page contains only 10 listitems
My question comes from a problem which I have right now. I have MainWindow,

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.