CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2008
    Posts
    10

    Auto sizing forms

    Environment: Visual Studio 2005
    Language: C# or C++.NET
    Using Forms

    By means of AutoSize and Dock, it is very easy to obtain that sizing the form will automatically resize the controls it contains, a situation that may be logically represented as:
    Size Form -> Auto Size Controls

    I would like to obtain the opposite:
    Auto Size Form <- (Auto) Size Control(s)

    I have a multiline TextBox and/or DataGridView and I want to be able to select the font that will be used inside this/these controls. When I change the font, I want that the entire Form resizes, together with the controls, in order to keep in view exactly a specified number of rows and columns without clipping or wrapping. Thus if I select a larger font, the control and the entire Form must enlarge; if I select a smaller font, they must shrink.

    I wonder if there is a simple method to obtain this. If there is none, I figure my program must calculate the required size for the Form and programmatically re-size it. Then, the controls will resize accordingly. The sequence would be:

    1. Select font -> calculate required total size of the Form window
    2. Resize myForm
    3. The controls automatically resize

    After I select the font, I multiply myFont.Height by the desired number of rows, take into account the other items in the form, and obtain the required total height.
    Similar calculations should be made for the width, but it's less easy as unfortunately the Font class does not provide a Width property; restricting my choices to monospaced fonts, the character width should be a constant, but I cannot find an easy way to obtain this value. I guess TextMetrics provides this information, but it's use seems rather complex and I've not been able to figure out how to do.

    Can anybody help?

  2. #2
    Join Date
    Oct 2010
    Location
    VS2008 .Net 3.5
    Posts
    10

    Re: Auto sizing forms

    If you obtain the Graphics object from the PaintEventArgs, it use the method MeasureString() to return the size of a string. The method call takes as parameters the text, and the font.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured