How Does GetPreferredSize Work?

  • Thread starter Thread starter TC
  • Start date Start date
T

TC

I'm trying to layout controls on a form, and I'm finding it very
difficult. Net offers several properties and methods designed to help
with layout, but they are poorly documented and I can't figure out how
to use them.

In order to arrange the controls the way I want them, I need to know
the size each control needs to display its text and borders. My
research suggests that the method GetPreferredSize() can tell me this.
I found this page:

http://windowsclient.net/blogs/faqs...do-controls-behave-with-getpreferredsize.aspx

However, that page doesn't explain the ProposedSize argument, which is
required by the GetPreferredSize() method. Can anyone tell me how
ProposedSize is used by the GetPreferredSize() method?

-TC
 
Can you give us an idea why and how you want to use it.

Before you wrote this message I had not noticed this property ever

Cor

"TC" wrote in message

I'm trying to layout controls on a form, and I'm finding it very
difficult. Net offers several properties and methods designed to help
with layout, but they are poorly documented and I can't figure out how
to use them.

In order to arrange the controls the way I want them, I need to know
the size each control needs to display its text and borders. My
research suggests that the method GetPreferredSize() can tell me this.
I found this page:

http://windowsclient.net/blogs/faqs...do-controls-behave-with-getpreferredsize.aspx

However, that page doesn't explain the ProposedSize argument, which is
required by the GetPreferredSize() method. Can anyone tell me how
ProposedSize is used by the GetPreferredSize() method?

-TC
 
Can you give us an idea why and how you want to use it.

Before you wrote this message I had not noticed this property ever

Cor

"TC"  wrote in message


I'm trying to layout controls on a form, and I'm finding it very
difficult. Net offers several properties and methods designed to help
with layout, but they are poorly documented and I can't figure out how
to use them.

In order to arrange the controls the way I want them, I need to know
the size each control needs to display its text and borders. My
research suggests that the method GetPreferredSize() can tell me this.
I found this page:

http://windowsclient.net/blogs/faqs/archive/2006/07/12/how-do-control...

However, that page doesn't explain the ProposedSize argument, which is
required by the GetPreferredSize() method. Can anyone tell me how
ProposedSize is used by the GetPreferredSize() method?

-TC

Cor,

I'm trying to layout controls on a form. I've established logic to
arrange the controls neatly within the space available, adding scroll
bars when necessary. In order to do everything right, I need to
compute the minimum space required for each control at runtime. I've
seen code which figures out this minimum size by setting the AutoSize
property to True, then letting the control size itself, and
interpreting that size as the minimum size. However, I believe this is
the wrong approach because it requires two layout passes (one to
autosize the controls, then another for my arrangement), and also
because I simply don't want to autosize the controls; I want to give
them custom sizes.

I expect there is some method to programmatically determine the
minimum size for a control without having to autosize it first. I've
guessed that GetPreferredSize is that method. However, the
documentation for GetPreferredSize is very poor. In my experiments,
I've determined that GetPreferredSize(Size.Empty) works more or less
as I would expect, but I have no idea what it is supposed to do when
you provide it with a real ProposedSize argument.

Note that there are many ways to define the minimum size for a
control. A label, for instance, might fit inside both (200, 25) and
(100, 50), depending on whether its text is broken into two lines or
not. I'd like to think that ProposedSize somehow lets me control this.
I've experimented to see if GetPreferredSize(New Size(0, 0)) gives me
an answer of (200, 25) and if GetPreferredSize(New Size(0, 50)) gives
me an answer of (100, 50), but it doesn't work that way -- I always
get the answer (200, 25). [These numbers are hypothetical -- I can't
remember the real numbers I used.]

In any case, this entire effort is motivated by my desire to create
pretty, resizable forms. If you, Cor -- or other developers reading
this -- have never used the GetPreferredSize method, how to you get
your forms to layout properly?


-TC
 
TC brought next idea :
On Oct 12, 11:24 pm, "Cor" <[email protected]> wrote:

If you, Cor -- or other developers reading
this -- have never used the GetPreferredSize method, how to you get
your forms to layout properly?


-TC

I have heard of that method - but, have never had an occasion to use
it... It's completely unecessary, in all but very special
circumstances.

Why? Because there are already controls that handle 99% of layout
needs - particularly, TableLayoutPanel and FlowLayoutPanel (used in
conjuction with Docking and Anchoring). So, unless you have some need
that is not accounted for by these two layout controls (who use this
method to do their job) - you don't need to mess with it.
 
TC brought next idea :





I have heard of that method - but, have never had an occasion to use
it...  It's completely unecessary, in all but very special
circumstances.

Why?  Because there are already controls that handle 99% of layout
needs - particularly, TableLayoutPanel and FlowLayoutPanel (used in
conjuction with Docking and Anchoring).  So, unless you have some need
that is not accounted for by these two layout controls (who use this
method to do their job) - you don't need to mess with it.

Tom,

Thanks for the feedback. I frequently use FlowLayoutPanel. I seldom
use TableLayoutPanel because I don't find it very useful. I agree that
FlowLayoutPanel, with docking and anchoring, can do a lot. However,
there are also many things it can't do. I would say it is very far
from providing 99% of my layout needs. In any case, I need to go
beyond it now, and am looking for advice on the behavior of
GetPreferredSize().

-TC
 
TC wrote :
Tom,

Thanks for the feedback. I frequently use FlowLayoutPanel. I seldom
use TableLayoutPanel because I don't find it very useful. I agree that
FlowLayoutPanel, with docking and anchoring, can do a lot. However,
there are also many things it can't do. I would say it is very far
from providing 99% of my layout needs. In any case, I need to go
beyond it now, and am looking for advice on the behavior of
GetPreferredSize().

-TC

Well, I'm not sure what you find not useful about TableLayoutPanel...
It works very much like an html table - dyanmically resizing it's
content. The only real issue is that it can be a bit tricky to use if
you don't understand how it interacts with the docking and anchoring
properties.

Anyway, good luck - you might want to try the msdn web forums. They
are way more active then this group, so you might actually get someone
who has implemented a custom layout panel and knows how to use that
method. I seriously doubt your going to get a definitive answer here
:)

I'm going to make one other plug... If you are using any version of
..NET greater then 3.0, you might seriously consider using WPF for your
user interface. It was designed from the begining with dynamic layout
and resolution independance built in (the layout stuff in windows forms
was sort of bolted on in the 2.0 release).
 
TC wrote :





Well, I'm not sure what you find not useful about TableLayoutPanel...  
It works very much like an html table - dyanmically resizing it's
content.  The only real issue is that it can be a bit tricky to use if
you don't understand how it interacts with the docking and anchoring
properties.

Anyway, good luck - you might want to try the msdn web forums.  They
are way more active then this group, so you might actually get someone
who has implemented a custom layout panel and knows how to use that
method.  I seriously doubt your going to get a definitive answer here
:)

I'm going to make one other plug...  If you are using any version of
.NET greater then 3.0, you might seriously consider using WPF for your
user interface.  It was designed from the begining with dynamic layout
and resolution independance built in (the layout stuff in windows forms
was sort of bolted on in the 2.0 release).

Tom,

On your advice, I'll take another look at the TableLayoutPanel. In the
past, I've found that I can get better results by writing my own
layout logic and avoiding the TableLayoutControl, but maybe it is time
to give it another chance. By the way, I loathe HTML tables, but I
know how they work, so comparing TableLayoutControl to HTML tables
gives me a good insight into what to expect.

I'm using NET 2.0, but I may consider upgrading for WPF. Thanks for
the tip.

-TC
 
Back
Top