Skip to content

Parent to Dll form problems

Abraham van Wyk

Asked Mar 4, 2009, 4:00 AM EST2 answers
Share
LinkedInX
Hi there.

I have a problem with the parenting in C#. It seems as though I'm missing something. (Searching the web doesn't help).

I want to open multiple forms in a panel on my main form by clicking on a button, but I seem to be doing something wrong. This is the code I'm using:
        public IntPtr Handel1;
        public Control pannelControl;

private void button1_Click(object sender, EventArgs e)
        {
            pannelControl = panel1;
            Handel1 = pannelControl.Handle;
            FORM2 form2 = new FORM2();
            form2.TopMost = false;
            panel1.Controls.Add(form2);
            form2.Show();

        }

public partial class FORM2 : Form
    {
        public void FORM2_Load(object sender, EventArgs e)
        {
           
        }

    }

First of all, The form is empty. This is the exception I'm getting at the line "panel1.Controls.Add(form2);"

ArgumentException was unhandled
Value does not fall within the expected range.

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Wheres the constructors for the form? And furthermore, wheres your InitializeComponent() method call as required to initialize the form? lol
    0
  • 0
    Thanx Brandon.

    That is not the problem, I've added a new form to the project itself and did the same thing in the button_click event. Here is the new code
    The button1_click stays the same.

    private void button1_Click(object sender, EventArgs e)
            {
                pannelControl = panel1;
                Handel1 = pannelControl.Handle;
                Form2 form2 = new Form2();
                form2.TopMost = false;
                panel1.Controls.Add(form2);
                form2.Show();

            }

    This is the new form added to the project "DeviceApplication100"

    namespace DeviceApplication100
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }

            private void Form2_Load(object sender, EventArgs e)
            {

            }
        }
    }

    The exception is still the same, Am I missing a reference somewhere ? Please tell me what I'm doing wrong? LOL

    Thanx.
    0