PC Review


Reply
Thread Tools Rate Thread

Center a control in a group box dynamically

 
 
Tom
Guest
Posts: n/a
 
      13th May 2004
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


 
Reply With Quote
 
 
 
 
Cor Ligthert
Guest
Posts: n/a
 
      13th May 2004
Hi Tom,

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

Cor


 
Reply With Quote
 
Tom
Guest
Posts: n/a
 
      13th May 2004
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

"Cor Ligthert" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi Tom,
>
> I think you would have a look at the anchor before you start doing

difficult
> things wherefore is a special property
>
> Cor
>
>



 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      13th May 2004
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


 
Reply With Quote
 
Tom
Guest
Posts: n/a
 
      13th May 2004
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

"Cor Ligthert" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      13th May 2004
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


 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      13th May 2004
* "Tom" <(E-Mail Removed)> scripsit:
> 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.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      13th May 2004
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


 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      13th May 2004
* "Cor Ligthert" <(E-Mail Removed)> scripsit:
>> 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.


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.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      14th May 2004
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
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Updating a list control dynamically per group? =?Utf-8?B?TGlhbSBN?= Microsoft Access Reports 3 10th Sep 2007 03:06 AM
Need to dynamically re-center a form Robert Morley Microsoft Access Form Coding 3 20th Jul 2007 12:30 AM
dynamically writing property values to a control INSIDE of a user control? Microsoft ASP .NET 2 1st Oct 2006 03:56 AM
Load User Control Dynamically, Cast object dynamically =?Utf-8?B?UmV6YSBOYWJp?= Microsoft ASP .NET 1 5th Mar 2005 12:04 AM
Load User Control Dynamically, Cast object dynamically =?Utf-8?B?UmV6YSBOYWJp?= Microsoft ASP .NET 1 4th Mar 2005 07:57 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:11 PM.