none
LOCATION ANCHOR RRS feed

Answers

  • Add the following code module to your project.

    Public Module Positioning
        <Runtime.CompilerServices.Extension>
        Public Sub Center(ByVal control As Control)
            control.CenterHorizontally()
            control.CenterVertically()
        End Sub
        <Runtime.CompilerServices.Extension>
        Public Sub CenterHorizontally(ByVal control As Control)
            Dim parentRect As Rectangle = control.Parent.ClientRectangle
            control.Left = (parentRect.Width - control.Width) \ 2
        End Sub
        <Runtime.CompilerServices.Extension>
        Public Sub CenterVertically(ByVal control As Control)
            Dim parentRect As Rectangle = control.Parent.ClientRectangle
            control.Top = (parentRect.Height - control.Height) \ 2
        End Sub
    End Module

    Usage

    Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize
        CheckBox1.Center
    End Sub


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by ahmeddc Friday, August 24, 2018 3:08 PM
    Friday, August 24, 2018 2:17 PM
    Moderator
  • Hi

    One simple way to get this.

    Instead of a Panel, create a new SplitContainer.

    Place the SC top left of Form, drag to right margin.

    Set Splitter Horizontal, fixed. Drag Splitter to center horizontally.

    Set SC Anchor to Top/Left/Right

    Adjust SC height as needed.

    *

    Add CheckBox A to left Panel of SC. Set CB Anchor to Top/Right.

    *

    Add CheckBox B to right Panel of SC. Set CB Anchor to Top/Left.

    *

    The above will keep both CB centered horizontally.


    Regards Les, Livingston, Scotland

    • Marked as answer by ahmeddc Friday, August 24, 2018 3:08 PM
    Friday, August 24, 2018 2:55 PM

All replies

  • You could do this with code, but you may find it easier to employ a TableLayoutPanel, or a combination of Panels inside 'cells' of your TLP


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Friday, August 24, 2018 2:07 PM
  • Add the following code module to your project.

    Public Module Positioning
        <Runtime.CompilerServices.Extension>
        Public Sub Center(ByVal control As Control)
            control.CenterHorizontally()
            control.CenterVertically()
        End Sub
        <Runtime.CompilerServices.Extension>
        Public Sub CenterHorizontally(ByVal control As Control)
            Dim parentRect As Rectangle = control.Parent.ClientRectangle
            control.Left = (parentRect.Width - control.Width) \ 2
        End Sub
        <Runtime.CompilerServices.Extension>
        Public Sub CenterVertically(ByVal control As Control)
            Dim parentRect As Rectangle = control.Parent.ClientRectangle
            control.Top = (parentRect.Height - control.Height) \ 2
        End Sub
    End Module

    Usage

    Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize
        CheckBox1.Center
    End Sub


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by ahmeddc Friday, August 24, 2018 3:08 PM
    Friday, August 24, 2018 2:17 PM
    Moderator
  • Hi

    One simple way to get this.

    Instead of a Panel, create a new SplitContainer.

    Place the SC top left of Form, drag to right margin.

    Set Splitter Horizontal, fixed. Drag Splitter to center horizontally.

    Set SC Anchor to Top/Left/Right

    Adjust SC height as needed.

    *

    Add CheckBox A to left Panel of SC. Set CB Anchor to Top/Right.

    *

    Add CheckBox B to right Panel of SC. Set CB Anchor to Top/Left.

    *

    The above will keep both CB centered horizontally.


    Regards Les, Livingston, Scotland

    • Marked as answer by ahmeddc Friday, August 24, 2018 3:08 PM
    Friday, August 24, 2018 2:55 PM