3

How do I position a Windows Form to the bottom-right of the screen when it gets open, instead of top-left?

Situation: I have a Form1 which doesn't actually do anything as a form, I just used it for its context menu (my app works from the tray only). So, most of the main running code goes into the Form1 class. When a context menu is clicked, it will do some processing and in the end it will show Form2. So Form2 gets opened/called by a context menu item of Form1. How do I change Form2's position in this case?

Form1.cs (part where Form2 gets triggered)

private void menu_upload_file_Click(object sender, EventArgs e)
{
    DialogResult dialogOpened = openFileDialog1.ShowDialog();
    if (dialogOpened == DialogResult.OK)
    {
        string filename = openFileDialog1.FileName;

        using (var client = new WebClient())
        {
            var response = client.UploadFile("http://localhost/imgitv3/upload.php?submit=true&action=upload&request=app", "POST", filename);
            // string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + Path.DirectorySeparatorChar + "response.txt";

            if (response != null)
            {
                string responseContent = System.Text.Encoding.ASCII.GetString(response);
                Form2 linkWindow = new Form2();

                if (isURL(responseContent))
                {
                    linkWindow.toTextBox(responseContent);
                    linkWindow.Show();
                }
            }
        }
    }
}

Form2.Designer.cs

// 
            // Form2
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.CausesValidation = false;
            this.ClientSize = new System.Drawing.Size(419, 163);
            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MaximizeBox = false;
            this.MaximumSize = new System.Drawing.Size(435, 202);
            this.MinimizeBox = false;
            this.MinimumSize = new System.Drawing.Size(435, 202);
            this.Name = "Form2";
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
            this.Text = "IMGit Image Uploader";
            this.TopMost = true;
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            this.Load += new System.EventHandler(this.Form2_Load);
            this.ResumeLayout(false);
            this.PerformLayout();
aborted
  • 4,481
  • 14
  • 69
  • 132
  • "bottom-right of the page". What page? This code doesn't run in a web browser. Subscribe the Load event so you know the actual size of the window, set the Location property. – Hans Passant Feb 07 '13 at 17:44
  • Sorry about saying **page** I am just used to writing PHP and I've been working with C# for 2 weeks only. How do I achieve what you just described? I need an example. – aborted Feb 07 '13 at 17:45

4 Answers4

5

There are two things you need to know. First is the working area of the screen on which you are going to display the form. The working area is the size of the screen minus the task bars displayed on that screen. You can use the Screen.WorkingArea property for that.

Second is the actual size of the window. Which is not in general the design size of the form, your user might have altered the size of the text in the window titlebar or may be running the video adapter in a different DPI setting from yours. You have to wait until the form's Load event fires before you know that size.

So make your code look like this, assuming that you want to display the form on the primary monitor:

        var frm = new Form2();
        frm.Load += (s, ea) => {
            var wa = Screen.PrimaryScreen.WorkingArea;
            frm.Location = new Point(wa.Right - frm.Width, wa.Bottom - frm.Height);
        };
        frm.Show();

Which relocates the window just before it becomes visible. The form's StartPosition property doesn't matter.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
2

You can set the form property StartPosition=Manual and than set form.left and form.top properties to the desired values.

You should set them before dialog is shown.

Form2 linkWindow = new Form2();
linkWindow.StartPosition = FormStartPosition.Manual;
linkWindow.Left = 200;
linkWindow.Top = 200;

if (isURL(responseContent))
{
  linkWindow.toTextBox(responseContent);
  linkWindow.Show();
}

Play with Left and Top values

VladL
  • 12,769
  • 10
  • 63
  • 83
  • Would be good if you showed me some code. This is why I asked here, because I had no idea how to write it. – aborted Feb 07 '13 at 17:42
  • Thanks for putting up the example. But would hard-coding the XY coordinates differ in different screen resolutions? I mean, would 200 and 200 be different for a 1440x900 resolution and 1024x768? I'd need a solution that works in general. – aborted Feb 07 '13 at 17:51
  • @Aborted you'll need to use some type of reference object then to insure consistant placement. If your running from the system tray, you can use your tray icon as a refence point for example. – Lee Harrison Feb 07 '13 at 17:58
2

Hook the FormLoad event from your Form2:

Form2 linkWindow = new Form2();
linkWindow.FormLoad += Form2_Load;

Then add this method somewhere:

    private void Form2_Load(object sender, EventArgs e)
    {
        this.StartPosition = FormStartPosition.Manual;
        this.Location = new Point(400, 400);  //set x,y to where you want it to appear
    }

Change the X,y values to whatever you want to position your window.

Lee Harrison
  • 2,306
  • 21
  • 32
2

Further to the answers regarding this.StartPosition = FormStartPosition.Manual and location etc. To calculate where to position the Form, one can use the Screen class and its WorkingArea property. http://msdn.microsoft.com/en-us/library/system.windows.forms.screen.aspx

alanjmcf
  • 3,430
  • 1
  • 17
  • 14