c# 多个控件(combobox)共用同一点击事件

2021-03-08 16:28

阅读:563

标签:www   mes   gui   tco   href   res   vat   ESS   gate   

mark:

https://zhidao.baidu.com/question/1754127113219248788.html

https://www.cnblogs.com/gxy19fly/archive/2007/07/10/812854.html

https://www.cnblogs.com/guiqiang/p/4632667.html

 

方法一:

portComboBox1.Click += new EventHandler(refreshPort3);
portComboBox2.Click += new EventHandler(refreshPort3);
portComboBox3.Click += new EventHandler(refreshPort3);

 

private void refreshPort3(object sender, EventArgs e)
{

try
{
List list = new List();
string[] ports = USB.GetPorts(); //SerialPort.GetPortNames();//
if (sender == portComboBox1)
{
portComboBox1.Items.Clear();
for (int i = 0; i {
portComboBox1.Items.Add(ports[i]);
}
if (ports.Length > 0)
{
portComboBox1.SelectedIndex = ports.Length - 1;
}
}
if (sender == portComboBox2)
{
portComboBox2.Items.Clear();
for (int i = 0; i {
portComboBox2.Items.Add(ports[i]);
}
if (ports.Length > 0)
{
portComboBox2.SelectedIndex = ports.Length - 1;
}
}
if (sender == portComboBox3)
{
portComboBox3.Items.Clear();
for (int i = 0; i {
portComboBox3.Items.Add(ports[i]);
}
if (ports.Length > 0)
{
portComboBox3.SelectedIndex = ports.Length - 1;
}
}
}
catch (Exception ex)
{
MetroMessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK);

}
}

 

 

方法二:

foreach (Control c in Panel1.Controls)
{
if (c is ComboBox)
{
c.Click += delegate (object sender, EventArgs e)
{

((ComboBox)sender).Items.Clear();
try
{
List list = new List();
string[] ports = USB.GetPorts(); //SerialPort.GetPortNames();//

((ComboBox)sender).Items.Clear();
for (int i = 0; i {
((ComboBox)sender).Items.Add(ports[i]);
}
if (ports.Length > 0)
{
((ComboBox)sender).SelectedIndex = ports.Length - 1;
}
}
catch (Exception ex)
{
MetroMessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK);

}
};
}

}

 

c# 多个控件(combobox)共用同一点击事件

标签:www   mes   gui   tco   href   res   vat   ESS   gate   

原文地址:https://www.cnblogs.com/chinayixia/p/12785748.html


评论


亲,登录后才可以留言!