Moving Windows Form (Location)

How do I place the Windows position in the lower right corner of the screen when it opens instead of the top left?

Situation: I have Form1 which doesn't actually do anything like a form, I just used it for my context menu (my app only works from the tray). Thus, most of the main code runs in the Form1 class. When the context menu is clicked it will do some processing and at the end it will show Form2 . Therefore, Form2 is opened / called by the Form1 context menu item . How can I change Form2 in this case?

Form1.cs (the part where Form2 starts)

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();

      

+3


source to share


4 answers


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

The second is the actual size of the window. This is usually not the size of the form design, your user might change the size of the text in the title bar, or might work with the video adapter at a different DPI from yours. You need to wait until the Load Event form appears before you know this size.

So, make your code look like this, assuming you want to display the form on the main 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();

      

Move the window just before it becomes visible. The StartPosition property is irrelevant.

+5


source


You can set a property of the form StartPosition=Manual

and set the property values form.left

and form.top

to the values ​​you want.

You must install them before displaying the dialog.



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

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

      

Playback with left and top values

+2


source


Grab the FormLoad event from 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 in the window.

+2


source


In addition to the answers to this .StartPosition = FormStartPosition.Manual and location, etc. You can use the Screen class and its WorkingArea property to figure out where to place the form. http://msdn.microsoft.com/en-us/library/system.windows.forms.screen.aspx

+2


source







All Articles