效果图
【新的知识点】
1、Sender As 尝试进行类型转换。
若能转换失败则返回null。
private void ToolStripMenuItem_Click(object sender, EventArgs e) ToolStripMenuItem menu = sender as ToolStripMenuItem;
2、颜色部分处理
1)、菜单栏 前置已选对勾 Name.Checked = true 。
2)、更换颜色:
richTextBox1.ForeColor = Color.Black;
3)、状态框显示
toolStripStatusLabel2.Text = menu.Text;
4)、自定义颜色窗口
//1、拖动颜色对话框到窗体上;创建了一个颜色对话框 ColorDialog colorDialog1 = new ColorDialog(); //2、显示对话框 DialogResult dr = colorDialog1.ShowDialog(); if (dr == DialogResult.OK) { richTextBox1.ForeColor = colorDialog1.Color; }
【展示效果】
【具体代码】
1 private void ToolStripMenuItem_Click(object sender, EventArgs e) 2 { 3 ToolStripMenuItem menu = sender as ToolStripMenuItem; 4 if (menu != null) 5 { 6 //设置Check属性+文本框的前景色+状态框 7 Red.Checked = false; 8 Green.Checked = false; 9 Default.Checked = false; 10 CustomizeColor.Checked = false; 11 12 menu.Checked = true; 13 switch (menu.Name) 14 { 15 case "Red": 16 richTextBox1.ForeColor = Color.Red; 17 break; 18 case "Green": 19 richTextBox1.ForeColor = Color.Green; 20 break; 21 case "Default": 22 richTextBox1.ForeColor = Color.Black; 23 break; 24 default: 25 //1、拖动颜色对话框到窗体上;创建了一个颜色对话框 26 ColorDialog colorDialog1 = new ColorDialog(); 27 //2、显示对话框 28 DialogResult dr = colorDialog1.ShowDialog(); 29 if (dr == DialogResult.OK) 30 { 31 richTextBox1.ForeColor = colorDialog1.Color; 32 } 33 break; 34 } 35 toolStripStatusLabel2.Text = menu.Text; 36 } 37 }文本框颜色部分
3、字体大小部分
toolStripComboBox1(集合 - Items )
效果图
private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e) { if (toolStripComboBox1.SelectedIndex == 0) richTextBox1.Font = new Font(richTextBox1.Font.FontFamily, 10); else richTextBox1.Font = new Font(richTextBox1.Font.FontFamily, 20); }
4、快捷菜单(右键显示)
图示:
【具体代码】
private void 撤销ToolStripMenuItem_Click(object sender, EventArgs e) { richTextBox1.Undo(); }