Anything better than "Anchor"?

R

Richard Brown

Ok, I celebrate and rejoice in the Anchor property. So wonderful compared
to the horrible 'resize' code I had to write in VB6, there is just no end to
the wonders of VB.NET..... uh, ok..... BUT...

I have a form with a lot of controls side by side, etc. The Anchor is
really only useful to resize 1 control on each line (and move all the
others). BUT, does anyone have any articles or other ways to do this?

For example, I am working on a piece of code that is similar to Anchor, and
is an 'Extender' that allows you to specify specifc a related control,
specify your edge and its edge and then do the math from there (if that
makes sense).

So, for example, the signature looks something like:
control.ExtendedAnchor(relatedcontrol As Object, myedge as AnchorStyles,
relatededge as AnchorStyles)

Where is is binding the specified edges of two specified controls (or the
form).
This is particularly useful in keeping textbox labels positioned over
textboxes where there are multiple across.

I just want to know if someone has already done this? Or has *anything*
better than just the Anchor property?
 
T

Tom Spink

What exactly is your problem? What can you not do with the Anchor property?

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

"Maybe it's a game called 'Punish the User'"
 
R

Richard Brown

Ok, for example, lets say that you have three text boxes, side by side,
First Name, Middle Initial, Last Name.
The middle one should just float and stay the same size, the left should
grow and be fixed at the left edge, the right one should grow, but be fixed
at the right edge.

How can the anchor property accomodate that?
Its almost like the middle text box needs to be 'anchored' to the
width-center of the form, the left textbox to the left of the form and right
of the middle text box, and the right text box anchored to right edge of the
middle text box and right edge of the form.

I'm not saying I have thought all of this through completely, but situations
*like* this don't seem to be able to be handled by 'anchor'.
 
J

Jay B. Harlow [MVP - Outlook]

Richard,
In case you didn't know you handle the Layout event to do your own layout of
controls. (or override the OnLayout method). The layout event has
SuspendLayout & ResumeLayout so you can control when you layout controls...

For side by side controls I find putting them into a Panel (or their own
user control :)) helps. For example, at my last job, we had a form that had
3 or 4 buttons centered on the bottom of the form. Placing the 3 or 4
buttons in a panel, then centering the panel on the control, greatly
simplified things. The panel was anchored to only the bottom, keeping it a
fixed distance to the bottom, because the side were not anchored it stayed
centered to the project. You can also use the layout event of the panel to
the contained controls resize in relation to each other, as the panel is
resized.

For my current project I have a user control with two contained controls.
One is docked to the top, the other is docked to the bottom. In the layout
event, I change the height of both, so they both always take up half the
space of the user control.

If you have not already seen it, the following article provides a custom
layout engine for Windows Forms:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/custlaywinforms.asp

The following article describes the panel trick:
http://www.devx.com/dotnet/Article/6964/0/page/1

If you search google for "windows forms layout" you get a number of 'good'
hits'

Hope this helps
Jay
 
R

Richard Brown

Gosh darn Jay, I knew someone on here had *all* the answers!
Thank you, I have not run across the 'layout' event yet and it sounds
exactly (almost) just like I need.
At least, that event seems to be the proper place to perform the coding (I
guess similar to placing the coding in the Resize event in Classic VB).

I think I would still have to have some sort of 'mapped linking' or
something if I wanted to make that code generic though.
I'm off to jump into the SDK docs to look up all of this, thanks again.
 
J

Jay B. Harlow [MVP - Outlook]

Richard,
The resize event among other things calls the PerformLayout method.

The PerformLayout method raises the Layout event, based on whether or not
SuspendLayout has been called without a matching ResumeLayout.

This allows the constructor & load events to SuspendLayout, make a bunch of
changes then ResumeLayout and all your controls will change, otherwise you
have the risk of repeated 'cascading' changes. which would slow everything
down.

Personally I think developing an enhanced anchor control, using a Property
Extender as you initially stated is a good model to follow, I believe that
is the model the first article uses.

The other option is more of a Strategy Pattern. Of course you Property
Extender could support a Strategy.

Hope this helps
Jay
 
R

Richard Brown

I've read that first article and had reminiscent (sp?) dreams of the Java
Programmer Certification tests -- the layouts they model are... umm...
EXACTLY the same.... gee, wonder why.

But its a great starting point and I am going to work on something in that
type of framework. Most notably, I took an interest in the 'Spring' format,
based on percentages, which would kind of work out the kinks with the
'anchoring to other controls'.
 

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