C# panel resizable

thesadguy

Active Member
Joined
Nov 29, 2008
Messages
70
Reaction score
0
Hi all...

I'd like to make a panel's size a percentage of a form's size. So if I resize the form, the panel will resize accordingly. In the same way, any panels within the main panel should resize themselves accordingly.

Is there a striaghtforward way to accomplish this, or do I have to write a bunch of SizeChanged event handlers?
 
Anchors.

Play with the Anchor property of the control. By default is anchors to the Top, Left, but if you anchor to the Right as well it will resize horizonally on a form resize. If you anchor to the Bottom it will resize vertically.
 
Having a bit of an issue with Anchor...

It works great when you have a single component that you want to anchor, but in the context of my custom calendar control, resizing means that some of the blocks end up overlapping each other.

Any other ideas?
 
Having a bit of an issue with Anchor...

It works great when you have a single component that you want to anchor, but in the context of my custom calendar control, resizing means that some of the blocks end up overlapping each other.

Any other ideas?

All I can say is that you have to carefully consider how to anchor each control to the borders. You have to figure out which edges are fixed and which are variable.

It sounds like you have some edges which you've made variable, but which need to be fixed.
 
All I can say is that you have to carefully consider how to anchor each control to the borders. You have to figure out which edges are fixed and which are variable.

It sounds like you have some edges which you've made variable, but which need to be fixed.

Well what would be really useful is to be able to anchor to the next component...

The problem is as follows...

Imagine two identical panels, one above the other ([1] at top, [2] at bottom) like the following:
[1]
[2]

Now anchoring [1] on top and [2] on bottom makes [1] "stick" to the top and [2] to the bottom. [1]'s bottom edge remains a fixed distance from the top of the window and [2]'s top edge remains a fixed distance from the bottom of the window. What's worse is that resizing smaller ends up with [1] and [2] overlapping.

Similar results occur with various other combinations of anchoring.

If it's not clear already, the reason for this is because enlarging the window vertically by 100 pixels should make [1] resize by 50 pixels and [2] resize by 50 pixels (both vertically). But this does not happen! They both resize by 100 pixels, "eating up" the "whitespace".

Anchoring relative to another component would be a nice feature :(
 
Remember with .NET you can override the default behavior of components very easily to insert your own code.

Perhaps try a manual math calculation onResize of the window. Get the pixels it's at and then set the two panels sizes as you like.

The math would be the tough part but it's do-able. Unfortunately not that easy. IMO I'd make the application size x and keep it that. Much easier than trying to move controls in their right spots all the time on window resizes
 
Anchoring relative to another component would be a nice feature :(

Nice feature indeed. Maybe someone from Microsoft is listening :D

I think for now you are stuck with having only one variable sized element per 'row' or 'column' in your layout.
 
Hi all...

I'd like to make a panel's size a percentage of a form's size. So if I resize the form, the panel will resize accordingly. In the same way, any panels within the main panel should resize themselves accordingly.

Is there a striaghtforward way to accomplish this, or do I have to write a bunch of SizeChanged event handlers?

yes, go to options and enable all controls....:D
then automatically under properties it will show if you want to enable it in the app...
 
easy, stop trying to GUI everything in vs.net and do what I said and play around with the onresize event that fires ;)

btw, I'm not using VS to do any of the development. That's why I'm not following, because I'm unfamiliar with the interface :)
 
Ah. Full disclosure sure makes it easier to help. What are you using?

Call it notepad... I'm trying to understand just [-]now[/-]how .NET handles this stuff... I have VS open next to me, but trying to generate the code by hand
 
Last edited:
Like Deenem said, use the Anchors property.

Code:
panel.Anchor = System.Windows.Forms.AnchorStyles.Left;
// Multiple anchors:
panel.Anchor = 
    (System.Windows.Forms.AnchorStyles.Top |
     System.Windows.Forms.AnchorStyles.Right);
 
Like Deenem said, use the Anchors property.

Code:
panel.Anchor = System.Windows.Forms.AnchorStyles.Left;
// Multiple anchors:
panel.Anchor = 
    (System.Windows.Forms.AnchorStyles.Top |
     System.Windows.Forms.AnchorStyles.Right);

As mentioned in the thread, this does NOT solve the problem; several components in each row and column.
 
Last edited:
I'd play around with the OnResize event-handlers. I like mathematical challenges with GUI. I created a process-flow mapper in ASP.NET recently that dynamically creates the lines with arrow points anchored to the correct sides of a process block. Twas a lot of brain-wracking fun! :D
 
I'd play around with the OnResize event-handlers. I like mathematical challenges with GUI. I created a process-flow mapper in ASP.NET recently that dynamically creates the lines with arrow points anchored to the correct sides of a process block. Twas a lot of brain-wracking fun! :D

yay, someone who concurs that if you played around with onresize event handlers and maff's you would've been done already ;)
 
As mentioned in the thread, this does NOT solve the problem; several components in each row and column.
/walks off in a huff, mumbling something about.. I don't know. You can't hear him, can you? :D
 
Try the TableLayoutPanel. You specify its column and row count and it creates a grid to which you can add controls to each cell. When you add controls to the cells you can set the added control's Dock property to "DockStyle.Fill" also set the TableLayoutPanel's Dock property to "DockStyle.Fill".

When the form resizes, the tableLayoutPanel should resize and the controls within it.
 
Top
Sign up to the MyBroadband newsletter
X