So I have 2 text boxes, one of them for writing in and one of them for changing the font size, how could I make the font size text box make the font size in the writing text box change?
Asked
Active
Viewed 92 times
0
-
1https://stackoverflow.com/questions/19834635/how-to-set-textbox-font-size-in-c – stuartd Apr 19 '21 at 23:12
1 Answers
0
Well for starters, assuming it is a Windforms app with 2 textbox objects named `textBox1' and 'textBox2' respectively, it isn't so difficult to rig up a quick way to to this.
First, in your forms load event you need to set the initial value of textBox2
private void Form1_Load(object sender, EventArgs e)
{
textBox2.Text = textBox1.Font.Size.ToString();
}
then, from there you just need to create a TextChanged event for textBox2 either by double clicking the textBox2 object(in visual editor) or selecting the event from the events creator.
Finally, to handle the event in a quick and easily broke down fashion for beginners:
private void textBox2_TextChanged(object sender, EventArgs e)
{
//use try/catch incase user inputs invalid value and cannot parse it as a float
try
{
//Cannot set the Size value of a font, so create a new font with desired size, referencing currrent textBox1.Font properties,
textBox1.Font = new Font(textBox1.Font.FontFamily.ToString(), float.Parse(textBox2.Text), textBox1.Font.Style, textBox1.Font.Unit, textBox1.Font.GdiCharSet, textBox1.Font.GdiVerticalFont);
//It worked, textBox2 background color stays white to signify success.
textBox2.BackColor = Color.White;
}
catch (Exception)
{
//An exception was thrown parsing input, set textBox2 background to red so user knows this is invalid.
textBox2.BackColor = Color.Red;
}
}

Michael Sullender
- 36
- 1