所以我有 2 个文本框,一个用于书写,一个用于更改字体大小,如何让字体大小文本框使书写文本框中的字体大小发生变化?
答案 0 :(得分:0)
对于初学者来说,假设它是一个 Windforms 应用程序,它有 2 个分别名为“textBox1”和“textBox2”的文本框对象,那么快速设置一个方法并不难。
首先,在你的表单加载事件中你需要设置 textBox2 的初始值
private void Form1_Load(object sender, EventArgs e)
{
textBox2.Text = textBox1.Font.Size.ToString();
}
然后,从那里您只需要通过双击 textBox2 对象(在可视化编辑器中)或从事件创建器中选择事件,为 textBox2 创建一个 TextChanged 事件。
最后,为初学者以快速、轻松的方式处理事件:
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;
}
}