Align Contents of a Panel in the Center

iPoor 0 Tallied Votes 4K Views Share

This is the code to align the content of a Panel in the Center, Hope this Helps :)

'You Will need 1 Panel Control and 1 PictureBox Control inside the Panel Control
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Panel1.AutoScroll = True
        'Tell the Panel Control 
        'to show ScrollBars when the
        'controls Are out of the Panel's Visible Boundaries
    End Sub

    Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        Panel1.Width = Me.Width - 50
        Panel1.Height = Me.Height - 70
'This is the Main PART!!!
        PictureBox1.Top = Panel1.Height / 2 - PictureBox1.Height / 2
        PictureBox1.Left = Panel1.Width / 2 - PictureBox1.Width / 2
    End Sub
End Class
Santanu.Das 125 Santanu Das

PictureBox1.Top = Panel1.Height / 2 - PictureBox1.Height / 2
PictureBox1.Left = Panel1.Width / 2 - PictureBox1.Width / 2

you can do it as

PictureBox1.Top = (Panel1.Height  - PictureBox1.Height) / 2
PictureBox1.Left = (Panel1.Width  - PictureBox1.Width) / 2
iPoor 0 Newbie Poster

Yes, Simpler Code

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.