为什么用C#编写richtextbox字体样式和字体大小随下拉框中comboBox的值变化不起作用
如图所示,我要实现的功能是当选择下拉菜单comboBox中字体类型和字体大小的时候,richtextbox选中的文本会变为相应的字体
。可是我的代码根本没反应(注:comboBox1的名字为fontCB,comboBox2的名字为sizeCB,RichTextBox的名字为rtb):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
foreach (FontFamily font in FontFamily.Families)
this.fontCB.Items.Add(font.Name);//设置fontCB下拉菜单内容
this.fontCB.SelectedItem = "宋体";//设置fontCB显示内容
foreach (string name in FontSizeName)
this.sizeCB.Items.Add(name);//设置sizeCB下拉菜单内容
this.sizeCB.SelectedItem = "10";//设置sizeCb显示内容
}
public string[] FontSizeName =
{
"8","9","10","12","14","16","18","20","22","24","26","28","36","48","72"
};
//定义字号数组
public float[] FontSize =
{
8,9,10,12,14,16,18,20,22,24,26,28,36,48,72
};
private string fontName;
private int fontSize;
private void fontCB_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
this.rtb.SelectionFont = new Font(this.fontCB.Text, this.rtb.SelectionFont.Size, this.rtb.SelectionFont.Style);
}
&nbs