user3349095
user3349095

Reputation: 59

Issue adding form controls to form from another class c#

Hello I am creating a windows form application, I am trying to create and add a new Control in this case it is a Panel from a different class, however when I have created the Panel and added it to the form it doesn't appear? [NOTE: I can change the text of the form with F.text("Some text"); but F.Controls.Add(panel1); doesn't work].

Here is my code:

public static void Create(Form1 F)
    {
         //this works:
         F.Text = "DEFAULT TEXT";
         Panel test = new Panel();
         test.Dock = DockStyle.Fill;
         test.BackColor = System.Drawing.Color.Black;
         test.Show();
         //this does not:
         F.Controls.Add(test);
    }

Upvotes: 0

Views: 129

Answers (2)

HackerMan
HackerMan

Reputation: 914

This works fine for me

    public Form1(){
 InitializeComponent();
            Create(this);

        }

        public static void Create(Form1 F)
        {
            //this works:
            F.Text = "DEFAULT TEXT";
            Panel test = new Panel();
            test.Dock = DockStyle.Fill;
            test.BackColor = Color.Blue;

            //test.Show(); this is irrelevant
            //this does not:
            F.Controls.Add(test);

Upvotes: 0

Mohamed
Mohamed

Reputation: 520

If you are adding the control from other class, Locate the control using Control c = fr.Controls["controlname"];

and then add it to the form that you want. F.controls.add(c);

Upvotes: 1

Related Questions