C# 用toolStripComboBox定义下拉选项来修改richTextBox中的字体大小,就像word那样,运行中出错。
publicstring[]FontSizeName={"8","9","10","12","14","16","18","20","22","24","26","28"...
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, 42, 36, 26, 24, 22, 18, 16, 15, 14, 12, 10.5F, 9, 7.5F, 6.5F, 5.5F, 5 };
//定义字号数组
private void toolStripComboBox2_DropDown_1(object sender, EventArgs e)
{
System.Drawing.Text.InstalledFontCollection fonts = new System.Drawing.Text.InstalledFontCollection();
foreach (string name in FontSizeName)
{
this.toolStripComboBox2.Items.Add(name);
}
}
//当下拉菜单时,遍历数组,输出字体型号
private void toolStripComboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
this.richTextBox1.SelectionFont = new Font(this.toolStripComboBox1.Text,FontSize[this.toolStripComboBox2.SelectedIndex], this.richTextBox1.SelectionFont.Style);
}
//通过更改被选定的项来选择字号并修改richTextBox1的字体大小
运行的时候修改几遍字体字号就会出错:
求教各位哥哥姐姐们,该怎么改呢 展开
public float[] FontSize = { 8, 9, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72, 42, 36, 26, 24, 22, 18, 16, 15, 14, 12, 10.5F, 9, 7.5F, 6.5F, 5.5F, 5 };
//定义字号数组
private void toolStripComboBox2_DropDown_1(object sender, EventArgs e)
{
System.Drawing.Text.InstalledFontCollection fonts = new System.Drawing.Text.InstalledFontCollection();
foreach (string name in FontSizeName)
{
this.toolStripComboBox2.Items.Add(name);
}
}
//当下拉菜单时,遍历数组,输出字体型号
private void toolStripComboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
this.richTextBox1.SelectionFont = new Font(this.toolStripComboBox1.Text,FontSize[this.toolStripComboBox2.SelectedIndex], this.richTextBox1.SelectionFont.Style);
}
//通过更改被选定的项来选择字号并修改richTextBox1的字体大小
运行的时候修改几遍字体字号就会出错:
求教各位哥哥姐姐们,该怎么改呢 展开
1个回答
展开全部
给Combox添加Item的时候在初始化时执行一次就OK了,
如果放在DropDown事件里就会DropDown一次执行一次,
改成下面方式没有发现你所描述的问题。
public Form1()
{
InitializeComponent();
System.Drawing.Text.InstalledFontCollection fonts = new System.Drawing.Text.InstalledFontCollection();
foreach (string name in FontSizeName)
{
this.toolStripComboBox2.Items.Add(name);
}
}
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, 42, 36, 26, 24, 22, 18, 16, 15, 14, 12, 10.5F, 9, 7.5F, 6.5F, 5.5F, 5 };
//定义字号数组
private void toolStripComboBox1_DropDown(object sender, EventArgs e)
{
}
private void toolStripComboBox2_DropDown(object sender, EventArgs e)
{
/*System.Drawing.Text.InstalledFontCollection fonts = new System.Drawing.Text.InstalledFontCollection();
foreach (string name in FontSizeName)
{
this.toolStripComboBox2.Items.Add(name);
}*/
}
private void toolStripComboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
this.richTextBox1.SelectionFont = new Font(this.toolStripComboBox1.Text, FontSize[this.toolStripComboBox2.SelectedIndex], this.richTextBox1.SelectionFont.Style);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询