Question

WARNING: Embedded software delevoper trying to build PC software!

I'm trying to interface a piece of hardware that communicates with the PC via serial interface. The PC software (C#) periodicaly sends a byte array, which I would like to adjust using some trackbars.

Instead of adding 8 trackbars on the design view, I add one to help me align it and then I create a List which I populate on load like so:

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

Is it possible to have events for all the List members like this one?

private void trackBarDmx1_Scroll(object sender, EventArgs e)
{

}

Which means I'd like update the relevant byte in my byte array to match the TrackBar value, using events if possible.

NOTE: This is a form template which I load and close via another form.

Was it helpful?

Solution

You can subscribe to the events when creating the TrackBars. All can have the same event handler:

trackBarDmx[i].Scroll += trackBarDmx1_Scroll;

Then in the handler you can find out which is this TrackBar and at which index it is (if necessary)

private void trackBarDmx1_Scroll(object sender, EventArgs e)
{
    TrackBar bar = sender as TrackBar;
    int trackBarIndex = this.trackBarDmx.IndexOf(bar);
}

OTHER TIPS

Not sure if I understood what you are trying to achieve. Does this do what you need:

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

In the handler:

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
        }

BTW, do you really need to retain the list of TrackBar?

You can do this :

    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;
     }

Now in the handler the simplest thing to do would be :

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

   }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top