Setting form's location when calling Form.Show() in C#

Setting form's location when calling Form.Show() in C#

When calling Form.Show() in C#, you can set the initial location of the form by setting its StartPosition property before calling Show(). The StartPosition property specifies how the form should be positioned when it is first displayed. Here are the possible values:

  • Manual: The form is positioned based on its Location property. This is the default value.
  • CenterScreen: The form is centered on the screen.
  • CenterParent: The form is centered on its parent form, if it has one.
  • WindowsDefaultLocation: The form is positioned based on the default location for new forms on the current operating system.

To set the initial location of the form, you can set the StartPosition property to Manual, and then set the Location property to the desired location. For example:

var form = new MyForm();
form.StartPosition = FormStartPosition.Manual;
form.Location = new Point(100, 100);
form.Show();

In this example, we create a new instance of MyForm, set its StartPosition property to Manual, and set its Location property to (100, 100). We then call Show() to display the form on the screen.

Note that if the StartPosition property is set to WindowsDefaultLocation, the Location property will be ignored, and the form will be positioned based on the default location for new forms on the current operating system.

Examples

  1. "C# set initial position of form when calling Form.Show()"

    Description: Learn how to set the initial position of a form when calling Show() in C#.

    Code:

    // Assuming you have a Form named myForm
    myForm.StartPosition = FormStartPosition.Manual;
    myForm.Location = new Point(100, 100); // Set the initial location to (100, 100)
    myForm.Show();
    
  2. "C# show form at center of the screen on Form.Show()"

    Description: Explore how to display a form at the center of the screen when calling Show() in C#.

    Code:

    // Assuming you have a Form named myForm
    myForm.StartPosition = FormStartPosition.CenterScreen;
    myForm.Show();
    
  3. "Set form's location relative to another form in C#"

    Description: Find out how to position a form relative to another form when calling Show() in C#.

    Code:

    // Assuming you have two Forms named mainForm and childForm
    childForm.StartPosition = FormStartPosition.Manual;
    childForm.Location = new Point(mainForm.Location.X + 20, mainForm.Location.Y + 20); // Set position relative to mainForm
    childForm.Show();
    
  4. "C# set form's position based on screen resolution"

    Description: Learn how to set a form's position dynamically based on the screen resolution when calling Show() in C#.

    Code:

    // Assuming you have a Form named myForm
    myForm.StartPosition = FormStartPosition.Manual;
    myForm.Location = new Point((Screen.PrimaryScreen.Bounds.Width - myForm.Width) / 2, (Screen.PrimaryScreen.Bounds.Height - myForm.Height) / 2);
    myForm.Show();
    
  5. "C# open form at top-left corner on Form.Show()"

    Description: Explore how to open a form at the top-left corner of the screen when calling Show() in C#.

    Code:

    // Assuming you have a Form named myForm
    myForm.StartPosition = FormStartPosition.Manual;
    myForm.Location = new Point(0, 0); // Set the initial location to the top-left corner
    myForm.Show();
    
  6. "C# set form's location relative to mouse cursor on Form.Show()"

    Description: Find out how to position a form relative to the current mouse cursor when calling Show() in C#.

    Code:

    // Assuming you have a Form named myForm
    myForm.StartPosition = FormStartPosition.Manual;
    myForm.Location = new Point(Cursor.Position.X - myForm.Width / 2, Cursor.Position.Y - myForm.Height / 2); // Set position relative to mouse cursor
    myForm.Show();
    
  7. "C# open form at bottom-right corner of the screen on Form.Show()"

    Description: Learn how to open a form at the bottom-right corner of the screen when calling Show() in C#.

    Code:

    // Assuming you have a Form named myForm
    myForm.StartPosition = FormStartPosition.Manual;
    myForm.Location = new Point(Screen.PrimaryScreen.Bounds.Width - myForm.Width, Screen.PrimaryScreen.Bounds.Height - myForm.Height);
    myForm.Show();
    
  8. "C# set form's location based on saved user preferences"

    Description: Explore how to set the form's location based on user preferences stored in settings when calling Show() in C#.

    Code:

    // Assuming you have a Form named myForm and user preferences are saved in application settings
    myForm.StartPosition = FormStartPosition.Manual;
    myForm.Location = Properties.Settings.Default.FormLocation; // Retrieve saved location from settings
    myForm.Show();
    
  9. "C# show form at a specific monitor on Form.Show()"

    Description: Find out how to display a form on a specific monitor when calling Show() in a multi-monitor setup.

    Code:

    // Assuming you have a Form named myForm and you want to show it on the second monitor
    myForm.StartPosition = FormStartPosition.Manual;
    myForm.Location = new Point(Screen.AllScreens[1].Bounds.Left, Screen.AllScreens[1].Bounds.Top);
    myForm.Show();
    
  10. "C# set form's location dynamically based on application logic"

    Description: Learn how to set the form's location dynamically based on application logic when calling Show() in C#.

    Code:

    // Assuming you have a Form named myForm and a condition determining the location
    myForm.StartPosition = FormStartPosition.Manual;
    myForm.Location = (someCondition) ? new Point(100, 100) : new Point(200, 200); // Set location based on condition
    myForm.Show();
    

More Tags

alert array-column random typechecking gpo re2 cryptography roslyn spring-security angularjs

More C# Questions

More Weather Calculators

More Cat Calculators

More Genetics Calculators

More Dog Calculators