4

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?

user774411
  • 1,749
  • 6
  • 28
  • 47

8 Answers8

15

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
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
3

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.

Code Name Jack
  • 2,856
  • 24
  • 40
  • 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
2

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.

Wouter de Kort
  • 39,090
  • 12
  • 84
  • 103
1

Add the line of code in frm.Designer.cs file

this.Location = new Point(0,0);

Note: Check if the location is already set in frm.resX file you can change it there. Or remove from .resX file and add the above line in frm.Designer.cs

Any way it will work.

fuma
  • 5,067
  • 4
  • 35
  • 39
Jimut
  • 26
  • 2
1

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
Adam
  • 147
  • 1
  • 13
0

Just Add This to your OnLoad Event

    Me.Location = New Point(1, 1)  
Boutamen
  • 109
  • 4
0

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);
 }
Aniruddh Parihar
  • 3,072
  • 3
  • 21
  • 39
-1

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);
        }
Sonu Singh
  • 191
  • 8