![]() |
Desktop Development »
Miscellaneous »
General
Intermediate
Designing Resizable Windows Forms in Visual Studio .NETBy Alex FrUsing the Splitter and Panel controls, Anchor and Dock control properties in Windows Forms. |
C#, Windows, .NET 1.0, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
Designing resizable forms in managed VS7 Windows applications is very simple - we don't need a layout manager, just use Splitter and Panel controls, new Dock and Anchor properties, and a few lines of code to get resizable forms in any configuration.
Consider a Windows form which contains:
StatusBar with number of panels.
TreeView control which may be resized using Splitter.
ListView control which may be resized using Splitter Picture Box (shown with blue color) which should be always square.
RichTextBox control which should fill all place remaining from the PictureBox. Such a form is shown below in two different states:


Here are the steps to make the form:
StatusBar control to the form (in my sample, the control's name is statusBar).
Dock property to Bottom.
StatusBar ShowPanels property to true.
StatusBar.
BorderStyle of the first panel to Raised, BorderStyle of the last panel to None.
Text of the last panel to "" and the width to 20. This panel is a placeholder for the Resize Grip. The width of the first panel will be set dynamically at run time.
BorderStyle of all other panels to Sunken.
ResizeStatusBar to the form: // Resize status bar (keep all panels except first in right side).
private void ResizeStatusBar()
{
int n = statusBar.Panels.Count;
if ( n > 1 )
{
int nRightWidth = 0; // width of panels 1,2...
for ( int i = 1; i < n; i++ )
{
nRightWidth += statusBar.Panels[i].Width;
}
if ( statusBar.Panels[0].MinWidth < statusBar.Width - nRightWidth )
{
statusBar.Panels[0].Width = statusBar.Width - nRightWidth;
}
}
}
Load and Resize message handlers to the form and call ResizeStatusBar from them.
Splitter control to the form (splitStatus). Set its Dock property to Bottom and Enable property to false. Result - the Splitter is placed between the StatusBar and the other part of the form and cannot be moved at run time.
Panel control (panelTop) to the top part of the form and set its Dock property to Fill.
TreeView control (treeView) to the top part of the form and set its Dock property to Left.
Splitter control (splitTop) to the top part of the form. Its Dock property is automatically set to Left. Set Splitter to desired position (to do this, resize TreeView control).
ListView control (listView) to the right part of the form. Set its Dock property to Top. In my sample, I set its View property to Details and added the number of columns.
Splitter control (splitRight) to the right part of the form, under the ListView control. Set its Dock property to Top.
Panel control (panelRightBottom) to the right part of the form, under the Splitter, and set its Dock property to Fill.
PictureBox control (pictureBox) to the right-bottom part of the form and set its Anchor property to Top, Bottom, Left. Fill its image (in the sample, I just changed the control's background color).
RichTextBox control (richTextBox) to the form near the PictureBox and set its Anchor property to Top, Bottom.
ResizeRightBottomPanel to the form: // Resize controls in right-bottom panel.
// Ensure that image is always square and text box
// fills other part of panel.
private void ResizeRightBottomPanel()
{
pictureBox.Width = pictureBox.Height;
richTextBox.Left = pictureBox.Right + 10;
richTextBox.Width = richTextBox.Parent.Width - richTextBox.Left;
}
Resize message handler for the panelRightBottom control. Call ResizeRightBottomPanel from it. Make the same call also from Form1_Load and Form1_Resize. | You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 2 Sep 2001 Editor: Smitha Vijayan |
Copyright 2001 by Alex Fr Everything else Copyright © CodeProject, 1999-2009 Web11 | Advertise on the Code Project |