By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,775 Members | 1,355 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,775 IT Pros & Developers. It's quick & easy.

Can you have a relative control size on a Form?

P: n/a
Hi everyone, I have a DataGrid that displays data on a dialog
application. When the dialog is resized, the datagrid keeps the same
shape, We would need the datagrid to keep a relative shape compared to
the rest of the dialog when the user resizes it. Any ideas on how to
do this?

Thanks!

May 15 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Set the "Anchor" property of the DataGrid. You must likely want to
switch it from "Top, Left" to "Top, Left, Bottom, RIght".

The anchor property says which edge of the form the edge of the control
are tied to. So, by default, the top of the datagrid is always the
same distance from the top of the form, and the left edge mantains it's
distance from the left edge. You alway want the bottom & right edges
to stick to their respective form edges.

May 15 '06 #2

P: n/a
This cannot be done in .NET 1.1... at least, not without doing a lot of
messing around with custom code in Resize events. Warning: getting the
timing right inside the events can be ugly: as I recall it's easy to
end up with infinite resizing and/or flickering, etc.

In .NET 2.0 I don't see why you can't create a custom layout manager
that understands proportions (if there isn't one already... I haven't
run across it if there is one). I'm not familiar with how the .NET 2.0
split panel control works, but it would seem a natural candidate for
allowing resize while retaining relative control sizes.

May 15 '06 #3

P: n/a
er.um...and exactly what's wrong with using the Anchor property
(available in .Net 1.1 & 2.0)

May 15 '06 #4

P: n/a
Anchor and Dock don't maintain relative sizes between elements in a
form.

Take the simplest example: a form containing two controls: one that
fills the left side of the form, one that fills the right side. Design
the form so that no matter how it's resized, each control occupies 50%
of the form space. You'll find that using Anchor and Dock you can't do
it: if you Dock one control Left and the other Fill then resizing the
form will result in the right side control expanding and contracting to
adapt to the form size. If you do the opposite, of course, it's the
left side control that takes up the slack. There's no way to make
_both_ controls change size to maintain the desired 50% ratio. (If you
try to use Anchor on both controls, you'll find that changing the form
size will result in a gap or an overlap between the controls.)

In .NET 2.0 there is the option to create layout managers. I don't know
if any of the existing ones provide for maintaining proportional size,
but I assume that one could write one that would.

May 15 '06 #5

P: n/a
>> Take the simplest example: a form containing two controls: one that fills the left side of the form, one that fills the right side

That's hardly the "simpliest example". Dialogs rarely have two control
that need to expand to two directions. (One might have two listboxes,
but as you expanded the dialog, you'd probably only want them to get
longer, not necessarily wider).

Specifically, the original poster only had one: "Hi everyone, I have a
DataGrid that displays data on a dialog
application." In this specific case, the Anchor property should work
fine.

Expanding two controls in two direction can be done in .Net 1.1, by
putting them in a panel with a splitter, but that get messy.

May 15 '06 #6

P: n/a
I was thinking of the following line from the original post:
We would need the datagrid to keep a relative shape compared to the rest of the dialog when the user resizes it.


Note the word "relative." Given this, what I posted is a simple thought
experiment illustrating why this is a hard thing to do.

The OP may well be happy with the results that Anchor and Dock provide.
I know that I've only once found sufficient cause to monkey with Resize
events. However, if the OP really wants to maintain "relative shape"
then Anchor and Dock will not do it.

May 15 '06 #7

P: n/a
In .Net 2.0 a TableLayoutPanel would do the trick...

May 16 '06 #8

P: n/a
If you don't want full control over where your windows are you could
override the onsizechanged event. Then you should resize your controls
relative to the size of the form. It's more work when your form gets
complicated, but you get more control.

Hope this Helps
Tony

C# Software Engineer

protected override void OnSizeChanged(EventArgs e)
{
dataGrid1.SetBounds((int)(this.Width*0.1),(int)(th is.Height*0.1),
(int)(this.Width*0.8),(int)(this.Height*0.8));

base.OnSizeChanged (e);
}

May 16 '06 #9

P: n/a
Hi guys,

Bruce is right, I've tried the anchor method, and the datagrid keeps
the same size when the dialog is resized. I'll try Tony's method in a
minute, but a lot of people seem to think that this can only be done in
2.0. We are running 1.1 here, and if there's a way to do it in 1.1,
that would be great...

Thanks

May 17 '06 #10

P: n/a
Hi everyone, I was able to work through this by setting a minimum size
for the dialog, and anchoring the control and the group box to all four
cardinal directions. Thaks for the help.

May 17 '06 #11

P: n/a
If the datagrid keeps the same size, then you are using the Anchor
property wrong.

What you should be seeing is the top & bottom of the datagrid keeps the
same distance from the top of the form, regardless of where the bottom
of the form is.
(with the right & left side of the datagrid acting similarly with
regard to the left side of the form.)

What I believe you want to see is the top of the Datagrid keep a fixed
distance from the top of the form, and the bottom keep a fixed distance
from the bottom of the form. This can be accomplished by changing the
Anchor property from "Top, Left" to "Top, Left, Bottom, Right".

Bruce/Tony methods allow you to keep the same relative sizes for the
Datagrid and the form (so that the margin between edge of the form and
the edge datagrid increases along with the form & datagrid). In my
experience, one almost never actually wants this behavior.

May 17 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.