Solved

Window Form enlarge text when window resizes?

Posted on 2013-05-14
6
361 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 32

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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

705 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

20 Experts available now in Live!

Get 1:1 Help Now