C#从控件列表中提升事件
作者:互联网
警告:嵌入式软件delevoper尝试构建PC软件!
我正在尝试连接一块通过串行接口与PC通信的硬件. PC软件(C#)会定期发送一个字节数组,我想用一些跟踪条进行调整.
我没有在设计视图中添加8个轨道栏,而是添加一个以帮助我对齐它,然后创建一个List,我在加载时填充如下:
public partial class FormDmxTemplate : Form
{
// Controls
// Create a list of tracbars.
List<TrackBar> trackBarDmx = new List<TrackBar>();
public FormDmxTemplate()
{
InitializeComponent();
}
private void FormDmxTemplate_Load(object sender, EventArgs e)
{
// Add first instance on the list
trackBarDmx.Add(trackBarDmx1);
// Generate 7 more, 8 total, of each
// Copy settings, and place them next to each other
for (int i = 1; i < 8; i++)
{
// Trackbars
trackBarDmx.Add(new TrackBar());
trackBarDmx[i].TickStyle = trackBarDmx[0].TickStyle;
trackBarDmx[i].Orientation = trackBarDmx[0].Orientation;
trackBarDmx[i].Minimum = trackBarDmx[0].Minimum;
trackBarDmx[i].Maximum = trackBarDmx[0].Maximum;
trackBarDmx[i].Size = new System.Drawing.Size(trackBarDmx[0].Size.Width, trackBarDmx[0].Size.Height);
trackBarDmx[i].Location = new System.Drawing.Point(trackBarDmx[i-1].Location.X + 60, trackBarDmx[0].Location.Y);
this.Controls.Add(trackBarDmx[i]);
}
}
}
是否可以为所有列表成员提供此类活动?
private void trackBarDmx1_Scroll(object sender, EventArgs e)
{
}
这意味着我想更新我的字节数组中的相关字节以匹配TrackBar值,如果可能的话使用事件.
注意:这是一个表单模板,我通过另一个表单加载和关闭.
解决方法:
您可以在创建TrackBars时订阅事件.所有可以拥有相同的事件处理程序:
trackBarDmx[i].Scroll += trackBarDmx1_Scroll;
然后在处理程序中,您可以找到哪个TrackBar以及它在哪个索引(如果需要)
private void trackBarDmx1_Scroll(object sender, EventArgs e)
{
TrackBar bar = sender as TrackBar;
int trackBarIndex = this.trackBarDmx.IndexOf(bar);
}
标签:c,events,controls,list,trackbar 来源: https://codeday.me/bug/20190624/1279899.html