码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2020-04-27 13:49:13      阅读:199      评论:0      收藏:0      [点我收藏+]

标签: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<string> list = new List<string>();
string[] ports = USB.GetPorts(); //SerialPort.GetPortNames();//
if (sender == portComboBox1)
{
portComboBox1.Items.Clear();
for (int i = 0; i < ports.Length; 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 < ports.Length; 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 < ports.Length; 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<string> list = new List<string>();
string[] ports = USB.GetPorts(); //SerialPort.GetPortNames();//

((ComboBox)sender).Items.Clear();
for (int i = 0; i < ports.Length; 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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!