gpt4 book ai didi

c# - 如何使用组合框更改 richTextBox 字体大小

转载 作者:太空宇宙 更新时间:2023-11-03 14:48:53 25 4
gpt4 key购买 nike

private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)//change font size
{
if (toolStripComboBox1.SelectedIndex == 0)
{
richTextBox1.SelectionFont = new Font("Comic Sans MS", 12);
}

if (toolStripComboBox1.SelectedIndex == 1)
{
richTextBox1.SelectionFont = new Font("Comic Sans MS", 19);
}
}

这是我的代码,在这种情况下,我必须单击“19”两次才能使其正常工作,我的代码有什么错误

enter image description here

enter image description here

最佳答案

private void toolStripComboBox1_Click(object sender, EventArgs e)
{
toolStripComboBox1.ComboBox.SelectionChangeCommitted += ComboBoxOnSelectionChangeCommitted;
}


private void ComboBoxOnSelectionChangeCommitted(object o, EventArgs eventArgs)
{
switch (toolStripComboBox1.SelectedIndex)
{
case 0:
richTextBox1.SelectionFont = new Font("Comic Sans MS", 12);
break;
case 1:
richTextBox1.SelectionFont = new Font("Comic Sans MS", 19);
break;
default:
richTextBox1.SelectionFont = new Font("Comic Sans MS", 9);
break;
}
}

您也可以使用 if 而不是 switch,但在这种情况下我个人更喜欢 switch。

关于c# - 如何使用组合框更改 richTextBox 字体大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52793237/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com