Solved

Window Form enlarge text when window resizes?

Posted on 2013-05-14
6
370 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

 
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

627 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