473,379 Members | 1,260 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Center a control in a group box dynamically

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
Nov 20 '05 #1
10 8589
Hi Tom,

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

Cor
Nov 20 '05 #2
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

"Cor Ligthert" <no**********@planet.nl> wrote in message
news:ug**************@TK2MSFTNGP10.phx.gbl...
Hi Tom,

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

Cor

Nov 20 '05 #3
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
Nov 20 '05 #4
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

"Cor Ligthert" <no**********@planet.nl> wrote in message
news:Os**************@tk2msftngp13.phx.gbl...
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

Nov 20 '05 #5
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
Nov 20 '05 #6
* "Tom" <to*@nospam.com> 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/>
Nov 20 '05 #7
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
Nov 20 '05 #8
* "Cor Ligthert" <no**********@planet.nl> 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/>
Nov 20 '05 #9
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" <to*@nospam.com> wrote in message
news:OT*************@TK2MSFTNGP10.phx.gbl...
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

Nov 20 '05 #10
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
Nov 20 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: MrBaseball34 | last post by:
In this HTML: <html> <head> <title>Page 1</title> </head> <body style="background:#C2BFA5;"> <span style="background-color:#F9DFB2; position: absolute; border:thin inset;
1
by: Joel Reinford | last post by:
I am working on an ASP.NET web app. One of the pages will be using several user controls. The controls will be loaded dynamically based on SQL Server data. I need to be able to retrieve the...
3
by: Eric | last post by:
I have built a composite user web control that I want to create dynamically. The form will contain a variable number of these controls and as well some of the contents of the user web control...
7
by: who be dat? | last post by:
I need some help here. I'm creating a list on a page where the list is created from a dataset with two tables linked with a datarelation. The first table is a list of groups while the second...
1
by: Aitham alama | last post by:
How can i make a dynamically created web label control to to be Center allignment Thanks
1
by: Reza Nabi | last post by:
Bakground: I have a webform (LoadCtl.aspx) which loads the user control to a placeholder dynamically based on the ctlName querystring passed in the URL. Webform (LoadCtl.aspx) also passes a...
12
by: robertino | last post by:
Hi all, I've put together a few SPs to produce a BOM (bill of materials) listing, which together use a couple of global temp tables, and return the results from a cursor. Here's the code: ...
2
by: Randy | last post by:
I have a form on which various controls are added dynamically depending on actions of the user. One of these actions allows the user to click a delete button that will remove various other...
0
by: Frank Swarbrick | last post by:
We're trying to use SQL Assist in the DB2 Control Center for DB2/LUW 9.5 and we are getting the following error: "Routine "SYSIBM.SQLTABLES" (specific name "TABLES") has returned an error SQLSTATE...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.