Question not Resizing controls

mcapone888

Member
Joined
Jun 17, 2011
Messages
10
Programming Experience
Beginner
Hi everyone. I am a new VB.Net hobbyist. Had a quick question which I am sure has an obvious answer which is currently eluding me. I have a basic windows form. I have one command button of the default size right in the middle of the form. I also set the anchors for the command button to keep it in the center when the window form is resized.

The issue is when I maximize the the window form the command button remains in the center, but it has grown in size tremendously (almost taking up the entire form).

I want to keep the command button centered when the form is maximized, but I also want to keep it the same size. Is there a property for the command button I am overlooking which will do this?

Thanks.
 
There isn't. Use the Resize event of the form and calculate where to place the button.
Private Sub CenterControl(c As Control, area As Size)
    c.Left = (area.Width - c.Width) \ 2
    c.Top = (area.Height - c.Height) \ 2
End Sub

Private Sub Form1_Resize(sender As Object, e As System.EventArgs) Handles Me.Resize
    CenterControl(Me.Button1, Me.ClientSize)
End Sub
 
Back
Top