C#从控件列表中提升事件

时间:2014-02-24 12:10:33

标签: c# list events controls trackbar

警告:嵌入式软件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值,如果可能的话使用事件。

注意:这是一个表单模板,我通过其他表单加载和关闭。

3 个答案:

答案 0 :(得分:2)

您可以在创建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);
}

答案 1 :(得分:1)

不确定我是否理解你想要实现的目标。这是否符合您的要求:

for (int i = 0; i < 7; i++)
            {
                TrackBar trackBar = new TrackBar();
                trackBar.Tag = i;
                // Other properties
                trackBar.Scroll += new EventHandler(trackBar_Scroll);
            }

在处理程序中:

void trackBar_Scroll(object sender, EventArgs e)
        {
           // Get the trackbar
            TrackBar current = sender as TrackBar;
            // Do something here. Use tag property to identify which byte array should be changed
        }
顺便说一句,你真的需要保留TrackBar的列表吗?

答案 2 :(得分:1)

你可以这样做:

    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]);

       // Notice no number in the handler name
       trackBarDmx[i].Scroll += trackBarDmx_Scroll;
     }

现在在处理程序中,最简单的事情是:

private void trackBarDmx_Scroll(object sender, EventArgs e)
{

   var tb = sender as TrackBar;

   if(sender == null)
   {return;}

   switch (sender.Name)
   {

       case "trackBarDmx1_Scroll" :
       // handle changes to bar 1
       break;

       // and so on

   }

}