Solved

Window Form enlarge text when window resizes?

Posted on 2013-05-14
6
368 Views
Last Modified: 2013-05-21
I have a window forms project. The window is resizeable. Is there a way to make the text resize with the window?

e.g. if we enlarge the window to full screen, no need to have all that text remain tiny.

e.g. I have a button that resizes with the window. No need to have that button text remain teeny tiny if the button is enlarged to a huge size.

Of course I can manually set the font size for text. I suppose I could intercept a window resize event. But how do I select what font size to set the button text to?

And is there an automatic way of doing this?
0
Comment
Question by:deleyd
6 Comments
 
LVL 34

Expert Comment

by:sarabande
ID: 39167357
normally forms would not resize their small controls like buttons, textboxes, comboboxes, ...

they would resize their main controls if they cover the whole form, for example grids or frames. they also would reposition controls or resize columns but not change the font as the font size should be related to the resolution of the screen rather than to the size of the window (at least for desktop applications). that means, you would change the fonts initially when you create the form but not on resize.

Sara
0
 
LVL 16

Expert Comment

by:hjgode
ID: 39167360
There is no automatic way for scaling controls.

You can subclass a control and override the paint method.

To set the text size of a button caption just set the font of the button text property.

Simple code to double the font size:
button1.Font = new Font(button1.Font.FontFamily, button1.Font.Size * 2, button1.Font.Style);

Open in new window

0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39167500
You will have to do some math in your form resize event to calculate appropriate font size. Then you can change all controls using a loop.

Example

Dim fntSize As Integer = 15
Dim fnt As New Font(button1.Font.FontFamily, fntSize)
For Each c As Control In Me.Controls
      c.Font = fnt
Next
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Accepted Solution

by:
hjgode earned 500 total points
ID: 39167535
here is an automatic text scale for a button:

        void sizeButtonText(Button _button)
        {
            Graphics g = this.CreateGraphics();
            float fSize = _button.Font.Size;
            Font f = _button.Font;
            SizeF fontBox = new SizeF(0f, 0f); ;
            Size proposedSize = new Size(int.MaxValue, int.MaxValue);
            String sTest = new string('W', _button.Text.Length); //we use the widest know char 'W' to measure, 
            //but even for "WWWWW" the text may exceed the boundary, bad MeasureString
            System.Diagnostics.Debug.WriteLine("Button: " + _button.ClientSize.Width.ToString() + "x" + _button.ClientSize.Height.ToString());
            f = new Font(f.FontFamily, 999f, f.Style);  //create a very large font
            do{
                //Scale down until smaller
                f = new Font(f.FontFamily, f.Size * 0.9f, f.Style);
                fontBox = TextRenderer.MeasureText(_button.Text, f, proposedSize,
                    TextFormatFlags.ExternalLeading | TextFormatFlags.GlyphOverhangPadding | 
                    TextFormatFlags.LeftAndRightPadding|TextFormatFlags.SingleLine|
                    TextFormatFlags.HorizontalCenter);
                //fontBox = g.MeasureString(sTest, f, _button.ClientSize.Width);
            }while(fontBox.Width > _button.ClientSize.Width || fontBox.Height > _button.ClientSize.Height);

            System.Diagnostics.Debug.WriteLine("FontBox: " + fontBox.Width.ToString() + "x" + fontBox.Height.ToString());
            f = new Font(f.FontFamily, f.Size / 1.1f, f.Style); //scale one step down
            _button.Font = f;
        }

Open in new window


Just add the paint event handler to the button:

        public Form1()
        {
            InitializeComponent();
            button1.Paint += new PaintEventHandler(button1_Paint);
        }

        void button1_Paint(object sender, PaintEventArgs e)
        {
            sizeButtonText(button1);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Windows.Forms.Button")
            {
                ((Button)sender).Size = new Size(((Button)sender).Width + 4, ((Button)sender).Height + 4);
            }
        }

Open in new window

The above code calculates the maximum size and resizes the button on every click. The text size is always adopted to the new size:

Button 1 at app start    button1 after several clicks
0
 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
ID: 39167750
Hi,

Create a new project
Add a textbox named TextBox1
Set its multiline property to True
Set its anchor property to all edges
In the code of the form replace everything with the following:

Imports System.Math
Public Class Form1
    Public FontFactor As Decimal
    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        FontFactor = TextBox3.Font.SizeInPoints / Sqrt(Me.Width ^ 2 + Me.Height ^ 2)

        AddHandler Me.Resize, AddressOf FontResize '(sender, e)
    End Sub

    Private Sub FontResize(sender As Object, e As System.EventArgs) 'Handles Me.Resize
        TextBox3.Font = New Font(TextBox3.Font.FontFamily, FontFactor * Sqrt(Me.Width ^ 2 + Me.Height ^ 2))
    End Sub
End Class

Open in new window


What i have done is calculate a factor based on the initial size of the form. I get the length of the diagonal of the form, as you would like to resize if any of width or height changes.

Then in form_load i add a handler to the form.resize event where i calculate the new font size based on the factor.

Giannis
0
 

Author Comment

by:deleyd
ID: 39175390
Rats, the MeasureText has this limitation:

The MeasureText method requires that the text is drawn on a single line.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question