[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 218
  • Last Modified:

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);
                OccasionForm.ShowDialog();
                if (OccasionForm.DialogResult == DialogResult.OK)
                {
                    OccasionReceivedEventArgs args = new OccasionReceivedEventArgs(_instanceId, Standards.at_Occasion);
                    args.WaitForIdle = true;
                    try
                    {
                        _orderService.OnGetOccasion(args);
                    }
                    catch (Exception exception)
                    {
                        HandleException(exception);
                    }
                }
            };
            //execute the anonymous delegate on the UI thread
            this.Invoke(theDelegate);
        }

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)
        InitializeComponent()
        Refresh_Form_Fonts(Me)
        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,
JJW
0
jackwebb22002
Asked:
jackwebb22002
  • 3
  • 2
  • 2
1 Solution
 
BIOGENIUSCommented:
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();

then

locate it like

OccasionForm .Size=pnlDialog.Size;

OccasionForm .Location= pnlDialog.Location;

then show it like

OccasionForm.ShowDialog();
 
0
 
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.
0
 
joriszwaenepoelCommented:
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
        Panel1.Controls.Add(f)        
        f.WindowState = FormWindowState.Maximized
        f.Show()

However, in that case I would recommend creating a UserControl.
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
BIOGENIUSCommented:
ok swap these lines
OccasionForm .Size=pnlDialog.Size;

OccasionForm .Location= pnlDialog.Location;

with this line

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

OccasionForm.ShowDialog();

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#?
0
 
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.,
OccasionForm.ShowDialog();
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?
0
 
joriszwaenepoelCommented:
The TopeLevel property is only available in code, not in the propertygrid.

I actually wrote that code and tested it (Visual Studio 2008).
0
 
jackwebb22002Author Commented:
Thanks very much.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now