0

I am trying to create a custom panel which will contains many controls. It is the first time I create a custom control like this. This example contains a panel with a button inside that panel.

I am not sure why the panel is not added to the Form1 when I click the button.
Am i missing something in my code?

        private void button2_Click(object sender, EventArgs e)
        {
            streamingPanel sp = new streamingPanel();
            sp.Size = new Size(319, 240);
            Point point = new Point(50, 50);
            sp.Location = point;
            this.Controls.Add(sp);
        }
        public class streamingPanel : Panel
        {
            public System.Windows.Forms.Panel panel1;
            public System.Windows.Forms.Button button1;

            public streamingPanel()
            {
                InitializeComponent();
            }

            private void InitializeComponent()
            {
                this.panel1 = new System.Windows.Forms.Panel();
                this.button1 = new System.Windows.Forms.Button();
                this.panel1.SuspendLayout();
                // 
                // panel1
                // 
                this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                this.panel1.Controls.Add(this.button1);
                this.panel1.Location = new System.Drawing.Point(483, 91);
                this.panel1.Name = "panel1";
                this.panel1.Size = new System.Drawing.Size(319, 240);
                this.panel1.TabIndex = 0;
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(100, 72);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(75, 23);
                this.button1.TabIndex = 0;
                this.button1.Text = "button1";
                this.button1.UseVisualStyleBackColor = true;
            }
        }
Andreas
  • 1,121
  • 4
  • 17
  • 34
  • 1
    Remove the `panel1` your custom control IS a `Panel`, and do everything you did for the removed `panel1` to `this`. and add the button to `this.Controls.Add(button1);` –  Nov 19 '19 at 00:47
  • Yes that was the solution. Now I understood. This IS a Panel ofcourse. I just blindly copied everything how it was layed out in Visual Studio. Thanks for your help! – Andreas Nov 19 '19 at 00:54
  • What are you targetting: Winforms, WPF, ASP..? YOU should __always__ TAG your questions correctly so one can see it on the questions page! – TaW Nov 19 '19 at 09:08
  • 1
    The control to use for this is not Panel but UserControl. – TaW Nov 19 '19 at 09:08

1 Answers1

1

Observe what Visual Studio editor does when you add a control to a form. The code is in method InitializeComponent called from form's constructor. You forgot for instance to set Location and Size of the new control.

Dialecticus
  • 16,400
  • 7
  • 43
  • 103
  • Yes, I am trying to follow what Visual Studio editor does. I changed my code in my original post. Where I added `InitializeComponent` and I set `Location` and `Size` but still I cant see the panel? – Andreas Nov 19 '19 at 00:40