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
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:
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:
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:
and modify your buttons class as:
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
And then the main form looks like this:
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:
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?