Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Changing the Size of a button text

I want that the Size of the Button font changes dynamically, when I change the size of the Button. So far I have placed the button on the right Location and the Size of the Button changes, when I resize the Form. But When the Button becomes to small for the text in the Button the letters just 'fall' out.

How can I change the Size of the Buttons text depending on the Button size itself?

like image 520
Stujo Avatar asked Oct 26 '16 18:10

Stujo


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C full form?

Full form of C is “COMPILE”.

What is C language basics?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

To make the text in the button responsive use the following code:

    //paint event from button:
    private void button1_Paint(object sender, PaintEventArgs e)
    {
        float fontSize = NewFontSize(e.Graphics, button1.Size, button1.Font, button1.Text);

        // set font with Font Class and the returned Size from NewFontSize();
        Font f = new Font("Arial", fontSize, FontStyle.Bold);
        button1.Font = f;
    }

    // method to calculate the size for the font:
    public static float NewFontSize(Graphics graphics, Size size, Font font, string str)
    {
        SizeF stringSize = graphics.MeasureString(str, font);
        float wRatio = size.Width / stringSize.Width;
        float hRatio = size.Height / stringSize.Height;
        float ratio = Math.Min(hRatio, wRatio);
        return font.Size * ratio;
    }

Example of code in action:

Example of code in action

As you see font will be resizeable inside the button. And the text will not be thrown out of the button. You also can use this for other Controllers as well.

like image 76
Timon Post Avatar answered Sep 28 '22 02:09

Timon Post


Since you didn't provide details in regards to font, size, and what not, I will do a general snippet just to point you in the right direction. Please modify its parameters according to your needs.

Button button = (Button)sender;
button.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

Or even something simpler

button.Font = new Font("Microsoft Sans Serif", 10);
like image 27
Vadzim Savenok Avatar answered Sep 28 '22 01:09

Vadzim Savenok