Form Centering

Discussion in 'Mixed Languages' started by carmz, Sep 2, 2012.

  1. carmz

    carmz MDL Senior Member

    Nov 29, 2011
    285
    7
    10
    Hi is it possible to center my form during run-time?and how do i get the dimension of the screen of desktop so that when it runs in different dimension my form will be centered always.I hope someone hears me.
     
  2. Josh Cell

    Josh Cell MDL Developer

    Jan 8, 2011
    3,519
    7,106
    120
    #2 Josh Cell, Sep 2, 2012
    Last edited by a moderator: Apr 20, 2017
    Code:
    this.StartPosition = FormStartPosition.CenterScreen;
    Just this will center the form programatically in all resolutions.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. user_hidden

    user_hidden MDL Expert

    Dec 18, 2007
    1,026
    978
    60
    #3 user_hidden, Sep 2, 2012
    Last edited by a moderator: Apr 20, 2017
    in VBnet you can try

    Code:
    ' This call is required by the Windows Form Designer.     
    InitializeComponent()     
    ' Add any initialization after the InitializeComponent() call.     
    Me.StartPosition = FormStartPosition.CenterScreen
    
    or you can just view "properties" of your form and change the "StartPosition" to "Center Screen"
     
  4. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,485
    1,321
    60
    #4 QuantumBug, Sep 3, 2012
    Last edited by a moderator: Apr 20, 2017
    Put this in your form load event.

    VB.NET

    Code:
    Me.Left = (Screen.PrimaryScreen.WorkingArea.Width - Me.Width) / 2
    Me.Top = (Screen.PrimaryScreen.WorkingArea.Height - Me.Height) / 2
    
    C#

    Code:
    {
    this.Left = (Screen.PrimaryScreen.WorkingArea.Width - this.Width) / 2;
    this.Top = (Screen.PrimaryScreen.WorkingArea.Height - this.Height) / 2;
    }
    Me.StartPosition is actually poop.
     
  5. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    5,886
    20,280
    180
    #5 Alphawaves, Sep 3, 2012
    Last edited by a moderator: Apr 20, 2017
    I wouldn't use primaryscreen, think this may be better:
    Code:
    Dim current = Screen.FromControl(Me).WorkingArea
    Me.Top = current.Top + CInt(current.Height / 2) - (Me.Height / 2)
    Me.Left = current.Left + CInt(current.Width / 2) - (Me.Width / 2)
    :D
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  6. carmz

    carmz MDL Senior Member

    Nov 29, 2011
    285
    7
    10
    #6 carmz, Sep 3, 2012
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Thank you all for helping me,@Alphawaves what is mean by this "Cint" is this convert to integer?
     
  7. carmz

    carmz MDL Senior Member

    Nov 29, 2011
    285
    7
    10
    #7 carmz, Sep 3, 2012
    Last edited by a moderator: Apr 20, 2017
    (OP)
    @AplhaWaves,is your code works in C#? it tried it but i get error.
     
  8. carmz

    carmz MDL Senior Member

    Nov 29, 2011
    285
    7
    10
    #8 carmz, Sep 3, 2012
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Hi Div,your C# code works for me...what is "POOP" ?
     
  9. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,485
    1,321
    60
    #9 QuantumBug, Sep 3, 2012
    Last edited by a moderator: Apr 20, 2017
    Erm, that's not important, what's important is the code worked haha.

    I converted Alphawaves code to C# for you. Just the same way, put it in your form load event.

    Code:
    {
    dynamic current = Screen.FromControl(this).WorkingArea;
    this.Top = current.Top + Convert.ToInt32(current.Height / 2) - (this.Height / 2);
    this.Left = current.Left + Convert.ToInt32(current.Width / 2) - (this.Width / 2);
    }
    Carmz you can always tell the difference between VB and C#

    VB.NET uses Me on itself and C# uses this, example below.

    VB:
    Code:
    Me.Text = "A Program " + Application.ProductVersion
    C#:
    Code:
    {
    this.Text = "A Program " + Application.ProductVersion;
    }
    More information on the programming differences here.
     
  10. Josh Cell

    Josh Cell MDL Developer

    Jan 8, 2011
    3,519
    7,106
    120
    My tip is always use the wherewithal of the programming language.

    .NET wins at many points with the productivity of other languages on the programming world.

    Isn't good back to the past, programming buttons or do the encapsulated methods manually. Use the .NET for you! ;)
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...