How do I set where a form opens up at?

I have an application with a form.  In the form, i have a panel named pnlDialog.  I want to open a second form inside the panel, pnlDialog.  When I run it, the called form displays at the wrong place on the screen, and with a slightly wrong size.

Here is the calling form snippet:
        /// <summary>
        /// Handle the request from the Workflow to get the Occasion
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _orderService_RequestOccasion(object sender, MessageReceivedEventArgs e)
            _instanceId = e.InstanceId;
            UpdateDelegate theDelegate = delegate()
                Form OccasionForm = new frmOccasion(pnlDialog.Size, pnlDialog.Location);
                if (OccasionForm.DialogResult == DialogResult.OK)
                    OccasionReceivedEventArgs args = new OccasionReceivedEventArgs(_instanceId, Standards.at_Occasion);
                    args.WaitForIdle = true;
                    catch (Exception exception)
            //execute the anonymous delegate on the UI thread

and here is the called form's New Procedure:

Option Strict On
Public Class frmOccasion

    Public Sub New(ByVal inSize As System.Drawing.Size, ByVal inLocation As System.Drawing.Point)
        Dim mySize As New System.Drawing.Size
        mySize.Width = inSize.Width
        mySize.Height = inSize.Height
        Dim myLocation As New System.Drawing.Point
        myLocation.X = inLocation.X
        myLocation.Y = inLocation.Y

        Me.WindowState = FormWindowState.Normal
        Me.StartPosition = FormStartPosition.Manual
        Me.Size = mySize
        Me.Location = myLocation
    End Sub

Has anyone done this before, and if so, can you direct me as to how to correct my problem?

Thanks much in advance,
Who is Participating?
joriszwaenepoelConnect With a Mentor Commented:
The TopeLevel property is only available in code, not in the propertygrid.

I actually wrote that code and tested it (Visual Studio 2008).
I'm doing this before and i always wonder why the coordinates that i assign to the form or to any control are not activated on it.

and after many works i expert that i should instantiate the form like

Form OccasionForm = new frmOccasion();


locate it like

OccasionForm .Size=pnlDialog.Size;

OccasionForm .Location= pnlDialog.Location;

then show it like

jackwebb22002Author Commented:
No, didn't work.  OccasionForm still showed up in the wrong place.  It displayed at coordinate X = 0, relative to the physical display screen, and coordinate Y higher than pnlDialog.
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Even if this would work, you popup form would not be "inside the panel", but just on top of the panel.  The use would be able to move your popup around.  Is that the expected behaviour, or do you want to popup-form to be fixed inside the the panel?

In that case, you could use code like this:

       Dim f As New Form2
        f.TopLevel = False
        f.FormBorderStyle = Windows.Forms.FormBorderStyle.None
        f.WindowState = FormWindowState.Maximized

However, in that case I would recommend creating a UserControl.
ok swap these lines
OccasionForm .Size=pnlDialog.Size;

OccasionForm .Location= pnlDialog.Location;

with this line

so it will be
Form OccasionForm = new frmOccasion();


OccasionForm .Size=pnlDialog.Size;

OccasionForm .Location= pnlDialog.Location;

but why you are creating the form using VB.NET you can complete it all in C#?
jackwebb22002Author Commented:
Dear Bio,
The controls on OccasionForm are not static.  They are dynamic, and derived from the User's Database.  Hence the desire to use an existing form that already gathers the user's data and build the required controls on OccasionForm.  And as it happens, the form was originally written in VB.  Also, swapping files does not look like it makes sense, i.e.,
opens the form modally, prior to setting it's size and location.

And to joriszwaenepoel:
I'm not using a control for the reasons stated above.  As far as your proposed solution, it seems plausible, but in practice,
1) There is no property of "TopLevel", so I assumed you meant "TopMost", and
2) OccasionForm never shows up at all.

in fact, during Debug, i had a look at the Immediate Window and saw:
A first chance exception of type 'System.ArgumentException' occurred in System.Windows.Forms.dll
Step into: Stepping over non-user code 'System.Windows.Forms.Control.InvokeMarshaledCallbackHelper'
Step into: Stepping over non-user code 'System.Windows.Forms.Control.InvokeMarshaledCallbacks'
A first chance exception of type 'System.ArgumentException' occurred in System.Windows.Forms.dll

Any thoughts on why this happened?
jackwebb22002Author Commented:
Thanks very much.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.