Form Centering

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

  1. carmz

    carmz MDL Senior Member

    Nov 29, 2011
    351
    13
    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,515
    7,171
    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,034
    1,061
    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. Muerto

    Muerto MDL Debugger

    Mar 7, 2012
    1,858
    2,112
    60
    #4 Muerto, Sep 3, 2012
    Last edited: Aug 22, 2021
    ...
     
  5. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    6,222
    22,280
    210
    #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
     
  6. carmz

    carmz MDL Senior Member

    Nov 29, 2011
    351
    13
    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
    351
    13
    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
    351
    13
    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. Muerto

    Muerto MDL Debugger

    Mar 7, 2012
    1,858
    2,112
    60
    #9 Muerto, Sep 3, 2012
    Last edited: Aug 22, 2021
    ...
     
  10. Josh Cell

    Josh Cell MDL Developer

    Jan 8, 2011
    3,515
    7,171
    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...