1

I'm trying to adapt @HansPasant's code from vb.net to c#. Also I want to adapt it so the winForms starts centered in terms of top-to-bottom but to the far left of the screen in terms of left-to-right:

vb.net from here How to set winform start position at top right? :

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

My current (bad) attempt:

private void scriptSurfer_Load(object sender,EventArgs e)
{
    var scr = Screen.FromPoint(this.Location);
    this.Location = New Point(scr.WorkingArea.Left - this.Width, scr.WorkingArea.Top);
    this.OnLoad(e);
}
Community
  • 1
  • 1
whytheq
  • 34,466
  • 65
  • 172
  • 267

1 Answers1

3

If I have understood your question you need

The LeftMost position of the Screen = 0
Vertical Centering = (Screen.Height - form.Height) / 2
this.Location = new Point(0, (scr.WorkingArea.Height - this.Height) / 2);

and do not forget

Form.StartPosition = FormStartPosition.Manual

As noted below in comments, the best point in which execute this code is inside the override of the OnLoad method albeit a Load event should work just fine in 99% of the situations. Also using the Screen.WorkingArea.Left property to position the form on the left side of the screen could be better instead of a fixed leftmost position. This could avoid edge cases where the leftmost available position is not at zero coordinates.

protected override void OnLoad(EventArgs e)
{
    var scr = Screen.PrimaryScreen;
    this.Location = new Point(scr.WorkingArea.Left, 
                             (scr.WorkingArea.Height - this.Height) / 2);
    base.OnLoad(e);
}
Steve
  • 213,761
  • 22
  • 232
  • 286
  • thanks steve...what about the signature?: this is closer than in my question: `protected override void OnLoad(object sender,EventArgs e)` but still not quite there – whytheq Jul 25 '15 at 22:13
  • I think you coud go with the Form_Load event without problems – Steve Jul 25 '15 at 22:16
  • ok - good - Form_Load event seems fine - this line of code is not required: `this.OnLoad(e);` – whytheq Jul 25 '15 at 22:19
  • `scr.WorkingArea.Left` is correct. It won't be `0` if anything is docked to the **left** side of the screen... – Idle_Mind Jul 25 '15 at 22:22
  • Form_Load will work fine but OnLoad is preferred is many situations. Form_Load is still there for legacy reasons. If you're interested in more, check these out: https://stackoverflow.com/questions/3670806/form-load-event-or-override-onload https://stackoverflow.com/questions/2521322/what-setup-code-should-go-in-form-constructors-versus-form-load-event – Saragis Jul 25 '15 at 23:31
  • @Saragis of course what Hans Passant says is completely true. Overriding OnLoad is better but I have no context to understand if this is an issue here. – Steve Jul 26 '15 at 10:52
  • Your vertical centering also needs a slight tweak to handle those pesky "edge" cases. If anything is docked to the top (TaskBar and/or third-party AppBars), then as with the .Left value, the .Top value will not be zero. With that in mind, instead of `(scr.WorkingArea.Height - this.Height) / 2`, you'd use `scr.WorkingArea.Top + (scr.WorkingArea.Height - this.Height) / 2`. – Idle_Mind Jul 26 '15 at 23:13