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?
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 ...
Full form of C is “COMPILE”.
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.
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.
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:
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With