Tom,
I normally use the Layout event of the control that I want to center in,
combined with the DisplayRectangle of the same control.
Something like:
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
MyBase.OnLoad(e)
GroupBox1.PerformLayout()
End Sub
Private Sub GroupBox1_Layout(ByVal sender As Object, ByVal e As
System.Windows.Forms.LayoutEventArgs) Handles GroupBox1.Layout
Dim pt As Point = GroupBox1.DisplayRectangle.Location
pt.X += (GroupBox1.DisplayRectangle.Width - TextBox1.Width) \ 2
pt.Y += (GroupBox1.DisplayRectangle.Height - TextBox1.Height) \ 2
TextBox1.Location = pt
End Sub
Alternatively if you center the control at design time, then set
Control.Anchor & Control.Dock both to None the control will stay centered at
runtime.
If I were dynamically creating the control, as it sounds like you I would do
both of the above, when I created it.
Public Function CreateTextBox() As TextBox
Dim TextBox1 As New TextBox
TextBox1.Anchor = None
TextBox1.Dock = None ' this is the default
Dim pt As Point = GroupBox1.DisplayRectangle.Location
pt.X += (GroupBox1.DisplayRectangle.Width - TextBox1.Width) \ 2
pt.Y += (GroupBox1.DisplayRectangle.Height - TextBox1.Height) \ 2
TextBox1.Location = pt
Return TextBox1
End Function
As you may have guessed, the DisplayRectangle is the client area of a
control, less any adornments, such as the frame on the GroupBox.
Hope this helps
Jay
"Tom" <(E-Mail Removed)> wrote in message
news:OTR$(E-Mail Removed)...
> I want to be able (at run time) to center a control (like a text box)
inside
> a group box. Whats the best way to calculate this? Is there something in
the
> framework that tells you the area INSIDE a group box? And how does one
> account for the wierd groupbox margins? (i.e. the top line, where the name
> is, seems bigger than the rest of the groupbox border)
>
> Tom
>
>
|