首页 > 技术文章 > 关于多控件调用同一个事件的问题

start-from-scratch 2016-04-28 14:56 原文

关于多控件调用同一个事件的问题,看到网上有道题:

如果按照每个控件都单独调用的话,很明显写多很多代码,有什么方法能省去多余的代码都调用同一个方法吗?

普通的单独调用的代码如下,谁看看怎么省去多余的代码

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Threading.Tasks;
  9 using System.Windows.Forms;
 10 
 11 namespace 设置文本框字体样式
 12 {
 13     public partial class Form1 : Form
 14     {
 15         public Form1()
 16         {
 17             InitializeComponent();
 18         }
 19 
 20         private void rdoSimSun_CheckedChanged(object sender, EventArgs e)
 21         {
 22                 txtShow.Font = new Font("宋体", txtShow.Font.Size, txtShow.Font.Style);
 23         }
 24         private void rdoLiSu_CheckedChanged(object sender, EventArgs e)
 25         {
 26             txtShow.Font = new Font("隶书", txtShow.Font.Size, txtShow.Font.Style);
 27         }
 28         private void rdoSimHei_CheckedChanged(object sender, EventArgs e)
 29         {
 30             txtShow.Font = new Font("黑体", txtShow.Font.Size, txtShow.Font.Style);
 31         }
 32         private void rdoSTCaiyun_CheckedChanged(object sender, EventArgs e)
 33         {
 34             txtShow.Font = new Font("华文彩云", txtShow.Font.Size, txtShow.Font.Style);
 35         }
 36         //粗体
 37         private void cbxBold_CheckedChanged(object sender, EventArgs e)
 38         {
 39             if (cbxBold.Checked == true)
 40             {
 41                 txtShow.Font = new Font(txtShow.Font, txtShow.Font.Style | FontStyle.Bold);
 42             }
 43             else if (cbxBold.Checked == false)
 44             {
 45                 txtShow.Font = new Font(txtShow.Font, txtShow.Font.Style ^ FontStyle.Bold);
 46             }
 47 
 48         }
 49         //斜体
 50         private void cbxItalic_CheckedChanged(object sender, EventArgs e)
 51         {
 52             if (cbxItalic.Checked == true)
 53             {
 54                 txtShow.Font = new Font(txtShow.Font, txtShow.Font.Style | FontStyle.Italic);
 55             }
 56             else if (cbxItalic.Checked == false)
 57             {
 58                 txtShow.Font = new Font(txtShow.Font, txtShow.Font.Style ^ FontStyle.Italic);
 59             }
 60         }
 61         //下划线
 62         private void cbxUnderline_CheckedChanged(object sender, EventArgs e)
 63         {
 64             if (cbxUnderline.Checked == true)
 65             {
 66                 txtShow.Font = new Font(txtShow.Font, txtShow.Font.Style | FontStyle.Underline);
 67             }
 68             else if (cbxUnderline.Checked == false)
 69             {
 70                 txtShow.Font = new Font(txtShow.Font, txtShow.Font.Style ^ FontStyle.Underline);
 71             }
 72         }
 73         //删除线
 74         private void cbxStrickout_CheckedChanged(object sender, EventArgs e)
 75         {
 76             if (cbxStrickout.Checked == true)
 77             {
 78                 txtShow.Font = new Font(txtShow.Font, txtShow.Font.Style | FontStyle.Strikeout);
 79             }
 80             else if (cbxStrickout.Checked == false)
 81             {
 82                 txtShow.Font = new Font(txtShow.Font, txtShow.Font.Style ^ FontStyle.Strikeout);
 83             }
 84         }
 85 
 86 
 87         private void rdo9_CheckedChanged(object sender, EventArgs e)
 88         {
 89             txtShow.Font = new Font(txtShow.Font.FontFamily,9,txtShow.Font.Style);
 90         }
 91 
 92         private void rdo10_CheckedChanged(object sender, EventArgs e)
 93         {
 94             txtShow.Font = new Font(txtShow.Font.FontFamily, 10, txtShow.Font.Style);
 95         }
 96 
 97         private void rdo12_CheckedChanged(object sender, EventArgs e)
 98         {
 99             txtShow.Font = new Font(txtShow.Font.FontFamily, 12, txtShow.Font.Style);
100         }
101 
102         private void rdo16_CheckedChanged(object sender, EventArgs e)
103         {
104             txtShow.Font = new Font(txtShow.Font.FontFamily, 16, txtShow.Font.Style);
105         }
106 
107         private void rdoRed_CheckedChanged(object sender, EventArgs e)
108         {
109             txtShow.ForeColor = System.Drawing.Color.Red;
110         }
111 
112         private void rdoYellow_CheckedChanged(object sender, EventArgs e)
113         {
114             txtShow.ForeColor = System.Drawing.Color.Yellow;
115         }
116 
117         private void rdoBlue_CheckedChanged(object sender, EventArgs e)
118         {
119             txtShow.ForeColor = System.Drawing.Color.Blue;
120         }
121 
122         //???多选框调用同个方法不行,谁来看下
123         public void cbxCheckedChanged()
124         {
125             //if (cbxBold.Checked == true)
126             //{
127             //    txtShow.Font = new Font(txtShow.Font, txtShow.Font.Style | FontStyle.Bold);
128             //}
129             //else if (cbxBold.Checked == false)
130             //{
131             //    txtShow.Font = new Font(txtShow.Font, txtShow.Font.Style ^ FontStyle.Bold);
132             //}
133             //if (cbxItalic.Checked == true)
134             //{
135             //    txtShow.Font = new Font(txtShow.Font, txtShow.Font.Style | FontStyle.Italic);
136             //}
137             //else if (cbxItalic.Checked == false)
138             //{
139             //    txtShow.Font = new Font(txtShow.Font, txtShow.Font.Style ^ FontStyle.Italic);
140             //}
141             //if (cbxUnderline.Checked == true)
142             //{
143             //    txtShow.Font = new Font(txtShow.Font, txtShow.Font.Style | FontStyle.Underline);
144             //}
145             //else if (cbxUnderline.Checked == false)
146             //{
147             //    txtShow.Font = new Font(txtShow.Font, txtShow.Font.Style ^ FontStyle.Underline);
148             //}
149             //if (cbxStrickout.Checked == true)
150             //{
151             //    txtShow.Font = new Font(txtShow.Font, txtShow.Font.Style | FontStyle.Strikeout);
152             //}
153             //else if (cbxStrickout.Checked == false)
154             //{
155             //    txtShow.Font = new Font(txtShow.Font, txtShow.Font.Style ^ FontStyle.Strikeout);
156             //}
157         }
158     }
159 }

 附上源代码:

设置文本框字体样式.rar

 

经好心网友提供意见,写成这样

1 private void FontFamily_CheckedChanged(object sender, EventArgs e)
2 {
3 RadioButton radio = sender as RadioButton;
4 txtShow.Font = new Font(radio.Text, txtShow.Font.Size, txtShow.Font.Style);
5 }

然后到前端的字体那四个radiobutton 的属性--事件 里面 把checkedchanged 事件都选择这个方法。

 1         //字号大小
 2         private void FontFamily1_CheckedChanged(object sender, EventArgs e)
 3         {
 4             RadioButton radio = sender as RadioButton;
 5             txtShow.Font = new Font(txtShow.Font.FontFamily, Convert.ToInt32(radio.Text), txtShow.Font.Style);
 6         }
 7         //颜色
 8         private void FontFamily2_CheckedChanged(object sender, EventArgs e)
 9         {
10             RadioButton radio = sender as RadioButton;
11             if (radio.Text == "")
12             {
13                 txtShow.ForeColor = System.Drawing.Color.Red;
14             }
15             else if (radio.Text == "")
16             {
17                 txtShow.ForeColor = System.Drawing.Color.Yellow;
18             }
19             else if (radio.Text == "")
20             {
21                 txtShow.ForeColor = System.Drawing.Color.Blue;
22             }
23         }

CheckBox的方法:

先在窗体加载的时候这样设置:

 1         private void Form1_Load(object sender, EventArgs e)
 2         {
 3             cbxBold.Tag = (int)FontStyle.Bold;
 4             //试下cbxItalic.Tag还能指向其他类型不
 5             //cbxItalic.Tag = Font.Style;
 6             cbxItalic.Tag = (int)FontStyle.Italic;
 7             cbxStrickout.Tag = (int)FontStyle.Strikeout;
 8             cbxUnderline.Tag = (int)FontStyle.Underline;
 9             //MessageBox.Show(cbxUnderline.Tag.ToString());
10         }

然后让所有CheckBox多选框都把CheckedChanged事件指向下面这个方法

 1      private void Style_CheckedChanged(object sender, EventArgs e)
 2         {
 3             //CheckBox cb = new CheckBox();
 4             CheckBox cb = sender as CheckBox;
 5             FontStyle style = (FontStyle)(Convert.ToInt32(cb.Tag));
 6             //FontStyle style = (FontStyle)4;
 7             if (cb.Checked)
 8             {
 9                 txtShow.Font = new Font(txtShow.Font, txtShow.Font.Style | style);
10             }
11             else
12             {
13                 txtShow.Font = new Font(txtShow.Font, txtShow.Font.Style ^ style);
14             }
15             //MessageBox.Show(style.ToString()); 
16         }

附上最新的源代码:

设置文本框字体样式(修改后).rar

推荐阅读