How to set winform start position at top right? I mean when user click (start) my winform application the winform will appear at the top right of the screen?
- 1,725
- 6
- 27
- 47
8 Answers
Use the Load event to change the position, the earliest you'll know the actual size of the window after user preferences and automatic scaling are applied:
Public Class Form1
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
Dim scr = Screen.FromPoint(Me.Location)
Me.Location = New Point(scr.WorkingArea.Right - Me.Width, scr.WorkingArea.Top)
MyBase.OnLoad(e)
End Sub
End Class
- 897,808
- 140
- 1,634
- 2,455
In form load even send the windows position to y=0 and x= Screen width - form width.
e.g.
private void Form1_Load(object sender, EventArgs e)
{
this.Location = new Point( Screen.PrimaryScreen.Bounds.Right - this.Width,0);
}
You can alternatively use "Screen.GetBounds(this).Right". This will give you the coordinates of screen which contain your form.
- 2,385
- 21
- 36
-
I couldn't comment on a related answer. Because I didn't have enough reputation. sorry for that. "I'm struggling with Screen.GetBounds function. This function ask a pt point type parameter. What value should I put in? " The point parameter is for different scenario. It is used in case you have multiple screens and you want to find the appropriate screen to be used based on a point coordinate. Primary screen parameter should be just enoguh for you. – Code Name Jack Jun 11 '16 at 20:23
You can use Form.Location to set the location to a Point that represents the top left corner of the form.
So if you set this to 'Screenwidth - Formwidth' you can position the Form in the top right. To get the screen width you can use the Screen.Bounds property.
- 37,915
- 11
- 80
- 100
-
I'm struggling with Screen.GetBounds function. This function ask a pt point type parameter. What value should I put in? – user774411 Oct 25 '11 at 16:13
-
to show on the primary monitor if you have multi monitor useful for multi monitor setup
Start on Upper Right
Public Class Form1
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
Dim scr = Screen.AllScreens(0)
Me.Location = New Point(scr.WorkingArea.Right - Me.Width, scr.WorkingArea.Top)
MyBase.OnLoad(e)
End Sub
End Class
Start on Upper Left
Public Class Form1
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
Dim scr = Screen.AllScreens(0)
Me.Location = New Point(scr.WorkingArea.Right - Me.Width - scr.WorkingArea.Right + Me.Width, scr.WorkingArea.Top)
MyBase.OnLoad(e)
End Sub
End Class
- 87
- 1
- 10
you can use this in OnLoad Event of your Form
private void dlgTTMSContract_Load(object sender, EventArgs e) {
int screenWidth = Screen.PrimaryScreen.Bounds.Size.Width;
int formWidth = this.Width;
this.Location = new Point(screenWidth - formWidth, 0);
}
- 2,674
- 2
- 19
- 34
- 11
- 3
It works fine for you:
private void Form1_Load(object sender, EventArgs e)
{
this.Location = new Point(Screen.FromPoint(this.Location).WorkingArea.Right - this.Width, 0);
}
- 181
- 7