在C#中如何通过改变组合框中字体的大小来改变富文本框中文本字体的大小

如:当组合框中的值为20时,富文本框中文本的字体就会变为20...
2026年09月25日 22:24
有1个网友回答
网友(1):

01.///


02./// 设置字体大小,对应于字号下拉框选项
03. ///

04./// 被选中的字号
05.private void ChangeFontSize(float fontSize)
06.{
07. if (fontSize <= 0.0)
08. throw new InvalidProgramException("字号参数错误,不能小于等于0.0");
09.
10. RichTextBox tempRichTextBox = new RichTextBox();
11.
12. int curRtbStart = curRichTextBox.SelectionStart;
13. int len = curRichTextBox.SelectionLength;
14. int tempRtbStart = 0;
15.
16. Font font = curRichTextBox.SelectionFont;
17. if (len <= 1 && null != font)
18. {
19. curRichTextBox.SelectionFont = new Font(font.Name, fontSize, font.Style);
20. return;
21. }
22.
23. tempRichTextBox.Rtf = curRichTextBox.SelectedRtf;
24. for (int i = 0; i < len; i++)
25. {
26. tempRichTextBox.Select(tempRtbStart + i, 1);
27. tempRichTextBox.SelectionFont =
28. new Font(tempRichTextBox.SelectionFont.Name,
29. fontSize, tempRichTextBox.SelectionFont.Style);
30. }
31.
32. tempRichTextBox.Select(tempRtbStart, len);
33. curRichTextBox.SelectedRtf = tempRichTextBox.SelectedRtf;
34. curRichTextBox.Select(curRtbStart, len);
35. curRichTextBox.Focus();
36.}