Click here to Skip to main content
6,635,160 members and growing! (17,579 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » General     Intermediate

Designing Resizable Windows Forms in Visual Studio .NET

By Alex Fr

Using the Splitter and Panel controls, Anchor and Dock control properties in Windows Forms.
C#, Windows, .NET 1.0, Dev
Posted:2 Sep 2001
Views:151,164
Bookmarked:56 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
14 votes for this article.
Popularity: 3.94 Rating: 3.44 out of 5
2 votes, 20.0%
1
2 votes, 20.0%
2
1 vote, 10.0%
3

4
5 votes, 50.0%
5

Introduction

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:

Sample Image 1

Sample Image 2

Here are the steps to make the form:

  1. Create new managed Windows application. In my sample, I use C#.
  2. Add StatusBar control to the form (in my sample, the control's name is statusBar).
  3. Set its Dock property to Bottom.
  4. Set StatusBar ShowPanels property to true.
  5. Add number of panels to the StatusBar.
  6. Set the BorderStyle of the first panel to Raised, BorderStyle of the last panel to None.
  7. Set the 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.
  8. Set the BorderStyle of all other panels to Sunken.
  9. Add the function 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;
                }
            }
        }
  10. Add the Load and Resize message handlers to the form and call ResizeStatusBar from them.
  11. Add a 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.
  12. Add the Panel control (panelTop) to the top part of the form and set its Dock property to Fill.
  13. Add the TreeView control (treeView) to the top part of the form and set its Dock property to Left.
  14. Add the 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).
  15. Add the 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.
  16. Add the Splitter control (splitRight) to the right part of the form, under the ListView control. Set its Dock property to Top.
  17. Add the Panel control (panelRightBottom) to the right part of the form, under the Splitter, and set its Dock property to Fill.
  18. Add the 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).
  19. Add the RichTextBox control (richTextBox) to the form near the PictureBox and set its Anchor property to Top, Bottom.
  20. Add the function 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;
        }
  21. Add the Resize message handler for the panelRightBottom control. Call ResizeRightBottomPanel from it. Make the same call also from Form1_Load and Form1_Resize.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Alex Fr


Member

Occupation: Software Developer
Location: Israel Israel

Other popular Miscellaneous articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 17 of 17 (Total in Forum: 17) (Refresh)FirstPrevNext
GeneralResizing without Coding Pinmembervkhaitan20:52 6 Sep '08  
GeneralThank you Pinmembernamedpipes7:45 7 Oct '07  
Generalresizing makes windows disappear Pinmemberkarin thues0:24 27 Apr '07  
Questionconfused Pinmembervincent_so17:43 26 Oct '06  
AnswerRe: confused PinmemberAlex Fr21:46 26 Oct '06  
GeneralAhh Finally PinmemberAshley Dawson14:29 10 Nov '04  
GeneralRe: Ahh Finally PinsussAnonymous1:14 12 May '05  
GeneralRe: Ahh Finally Pinmembervidhyaravichandar19:03 12 Aug '09  
GeneralAnother Delphi ripoff PinmemberAnonymous11:28 6 Sep '01  
GeneralRe: Another Delphi ripoff PinmemberAnonymous5:12 10 Sep '01  
GeneralRe: Another Delphi ripoff PinmemberAnonymous5:55 8 May '02  
GeneralRe: Another Delphi ripoff PinsussAnonymous15:46 11 Oct '02  
GeneralRe: Another Delphi ripoff PinsussAnonymous11:24 21 Oct '02  
GeneralRe: Another Delphi ripoff Pinmemberrajbow7:05 27 Feb '04  
GeneralRe: Another Delphi ripoff PinmemberCode Monkey5:57 3 Sep '02  
GeneralRe: Another Delphi ripoff Pinmemberluanzhu8:55 13 Nov '02  
GeneralRe: Another Delphi ripoff PinmemberVladimir L.13:23 16 Dec '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin 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