Hello,

This is the first time I've attempted creating controls at runtime, and I'm not sure why the following code isn't working;

private void Popup_Load(object sender, EventArgs e)
{
            Panel panel2 = new Panel();
            panel2.Size = new Size(1280, 720);
            panel2.Location = new Point(20, 20);
            panel2.BackColor = Color.Red;
            this.Size = new Size(1280, 720);
            this.Controls.Add(panel2);
}

I'm also trying to draw a string to the screen, but figured that wasn't working for what's probably the same reason as why the above code isn't working.

Any help is appreciated,
Thanks.

This is working with me:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private Panel panel2; //NOTICE OUTSIDE of Form1_Load

        public Form1()
        {
            InitializeComponent();            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.panel2 = new Panel();
            panel2.Size = new Size(1280, 720);
            panel2.Location = new Point(20, 20);
            panel2.BackColor = Color.Red;
            this.Size = new Size(1280, 720);
            this.Controls.Add(this.panel2);
            this.panel2.Paint += new System.Windows.Forms.PaintEventHandler(this.panel2_Paint);                    
        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {
            //Example +/- from MSDN
            // Create string to draw.
            String drawString = "Sample Text";

            // Create font and brush.
            Font drawFont = new Font("Arial", 16);
            SolidBrush drawBrush = new SolidBrush(Color.Black);

            // Create point for upper-left corner of drawing.
            PointF drawPoint = new PointF(10.0F, 10.0F);

            // Draw string to screen.
            e.Graphics.DrawString(drawString, drawFont, drawBrush, drawPoint);
        }
    }
}

Your example doesn't work for me either. After trying yours, I tried moving my panel2 declaration outside of my functions and adding your paint function but it still doesn't work. Just to be sure, if I wanted to create a new instance of the form and show it with the panel2 Panel, how would I do it. I currently have;
Popup popped = new Popup(); popped.Show();

What exactly is Popup? A Form?

It's a form, yes.

Could you be a little more elaborate on that?
Is it created as another form at runtime? what exactly is it supposed to do etc.

I have Form1 and Popup forms in my project explorer tab. Then on run time, Form1 is supposed to open Popup with the relevant information (once a trigger is activated).

So;
Form1 opens and waits for a certain event.
On the event, it's supposed to open Popup (form) with the event information.

Did you install a Loadeventhandler, like I did for the Painteventhandler for DrawString?

n my main form (Form1) I have a button. The Click event simulates your events.
It goes like this:

private void PopupBtn_Click(object sender, EventArgs e)
        {
            Popup popped = new Popup();
            popped.Show();
        }   

The code of the Popu form now is like this:

namespace WindowsFormsApplication1
{
    public partial class Popup : Form
    {
        private Panel panel2; //NOTICE OUTSIDE of Form1_Load

        public Popup()
        {
            InitializeComponent();
        }

        private void Popup_Load(object sender, EventArgs e)
        {
            this.panel2 = new Panel();
            panel2.Size = new Size(1280, 720);
            panel2.Location = new Point(20, 20);
            panel2.BackColor = Color.Red;
            this.Size = new Size(1280, 720);
            this.Controls.Add(panel2);
            this.panel2.Paint += new System.Windows.Forms.PaintEventHandler(this.panel2_Paint); 
        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {
            //Example +/- from MSDN
            // Create string to draw.
            String drawString = "Sample Text";

            // Create font and brush.
            Font drawFont = new Font("Arial", 16);
            SolidBrush drawBrush = new SolidBrush(Color.Black);

            // Create point for upper-left corner of drawing.
            PointF drawPoint = new PointF(10.0F, 10.0F);

            // Draw string to screen.
            e.Graphics.DrawString(drawString, drawFont, drawBrush, drawPoint);
        }
    }

It is working perfectly.

Is this:

private void Popup_Load(object sender, EventArgs e)
{
    Panel panel2 = new Panel();
    panel2.Size = new Size(1280, 720);
    panel2.Location = new Point(20, 20);
    panel2.BackColor = Color.Red;
    this.Size = new Size(1280, 720);
    this.Controls.Add(panel2);
}

declared as an event in the Form which is calling the pop-up, or in the pop-up class?
i.e.:
1.

// form stuff
void Your_Event(object sender, EventArgs e)
{
    var popup = new Popup();
    popup.LoadPop += Popup_Load;
}

// popup function declared in the form class
void Popup_Load(object sender, EventArgs e)
{
    // stuff
}

or 2.

// form stuff
void Your_Event(object sender, EventArgs e)
{
    var popup = new Popup();
}

// popup class

public Popup(string text, string mail)
{
    InitializeComponent();
    popup.LoadPop += Popup_Load;
}

void Popup_Load(object sender, EventArgs e)
{
    // stuff
}

Also, try this version of your popup load event:

private void Popup_Load(object sender, EventArgs e)
{
    var popup = sender as Popup;        // get the sender of the event, which is your popup
    popup.Size = new Size(1280, 720);
    Panel panel2 = new Panel();
    panel2.Size = new Size(1280, 720);
    panel2.Location = new Point(20, 20);
    panel2.BackColor = Color.Red;


    popup.Controls.Add(panel2);         // add the panel to your popup
    panel2.BringToFront();              // in case something is on top of your panel
}

Sorry for the delay. I decided to give up on the project and start from scratch, in which I used a custom paint event to draw text to the screen, and added the panel runtime code to the form load. Both of which worked perfectly, must of been something else conflicting.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.