Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Window Form enlarge text when window resizes?

Posted on 2013-05-14
6
Medium Priority
?
372 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 35

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
Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

 
LVL 16

Accepted Solution

by:
hjgode earned 2000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

730 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