Center a control in a group box dynamically

T

Tom

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
 
C

Cor Ligthert

Hi Tom,

I think you would have a look at the anchor before you start doing difficult
things wherefore is a special property

Cor
 
T

Tom

Cor: I am not exactly sure what you are talking about... Is there anything
that says what the groupbox border width is? Both the top and the sides?

Tom
 
C

Cor Ligthert

Hi Tom,

Try this than you see it.

Make a project

Set a groupbox in a form and set the dock properties to Fill (clicking the
middle)
Set in that groupbox a textbox and set the anchor properties from that to
all sides (clicking all arrows)

Run it and resize

(This all can done by the designer)

I hope this makes it clear otherwise ask than I tell it in another way.

Cor
 
T

Tom

Cor: OK, think you misunderstood me. I have no problem with doing it at
design time; however, I am creating my text box at RUN-TIME
(dynamically).... so the question is: Once I create my textbox, I want to
place it so that it is centered within a previously created and sized group
box. I'll calculate my text box width/height before I place it. So if the
group box width is 100, and my text box is 50 in width, then I would place
it at left=25. However, the top and bottom of the group box are different
sizes, because the top border has room for the title in it. So how do I
calculate where the TOP of my textbox should be? Is there any way to get the
groupbox border widths?

Tom
 
C

Cor Ligthert

Hi Tom,

What is the difference from design and runtime for creating a control (I
assume that you are talking about a window forms, otherwise we get another
situation.).

Just do as I said, open the designer part at the + and look at the code.

Than you see everything you did ask in this question. (Also not direct
related to the anchor and the dock, however when you onces have seen that,
you do not want to use any old stuff by measuring anymore) (Even the
screensize is not important)

Cor
 
H

Herfried K. Wagner [MVP]

* "Tom said:
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)

You can move controls over the caption, so it's part of the control's
"client" area. I don't see an easy-to-use way to accomplish that.
 
C

Cor Ligthert

Hi Herfried,
I don't see an easy-to-use way to accomplish that.

See my solution, you can just use the designer.

And it works I tried it before I wrote it.

Cor
 
H

Herfried K. Wagner [MVP]

* "Cor Ligthert said:
See my solution, you can just use the designer.

And it works I tried it before I wrote it.

VS.NET will hardcode the position, but it will update it when an other
font size is used. Nevertheless, this doesn't solve the problem on how
to determine the height of the groupbox's caption. There must be a way
because the .NET framework knows the height.
 
J

Jay B. Harlow [MVP - Outlook]

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
 
C

Cor Ligthert

Hi Herfried and Tom,

Now I see what you both mean, I agree that this meens calculating from the
hight using the font size.

Thanks for pointing me on this.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top