Solved

Window Form enlarge text when window resizes?

Posted on 2013-05-14
6
365 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 33

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

895 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now