views:

22

answers:

1

Basically I want to assign events to some controls that are created. I want to update the labels associated with each trackbar when each trackbar is changed (change label1[i] and label2[i] when trackbar[i] changes). I have waded through google and I found that this probably has be done by using delegates. I had a blast using lambda expressions but I now understand that this is for anonymous events only?

Please could someone push me in the right direction?

            // assign control Arrays
           TrackBar[] criteriatbs = new TrackBar[numberofsliders];
           TextBox[] criteriatextbs = new TextBox[numberofsliders];
           Label[] criterialabel1 = new Label[numberofsliders];
           Label[] criterialabel2 = new Label[numberofsliders];
           Label[] criterialabel3 = new Label[numberofsliders];
           Label[] criterialabel4 = new Label[numberofsliders];

                for (int i = 0; i < numberofsliders; i++)
            {

                // Add Labels1
                Label label1x = new Label();
                label1x.Location = new Point(40, 135 + (i * 200));
                label1x.Size = new Size(250, 15);
                label1x.TextAlign = ContentAlignment.MiddleCenter;
                label1x.Text = "Test";
                label1x.ForeColor = Color.Black;

                criterialabel1[i] = label1x;
                Controls.Add(criterialabel1[i]);


                // Add Labels2
                Label label2x = new Label();
                label2x.Location = new Point(448, 135 + (i * 200));
                label2x.Size = new Size(250, 15);
                label2x.TextAlign = ContentAlignment.MiddleCenter;
                label2x.Text = "Test";
                label2x.ForeColor = Color.Black;

                criterialabel2[i] = label2x;
                Controls.Add(criterialabel2[i]);


                // Add Labels3
                Label label3x = new Label();
                label3x.Location = new Point(40, 195 + (i * 200));
                label3x.Size = new Size(250, 15);
                label3x.TextAlign = ContentAlignment.MiddleCenter;
                label3x.Text = "Equal";
                label3x.ForeColor = Color.DimGray;

                criterialabel3[i] = label3x;
                Controls.Add(criterialabel3[i]);


                // Add Labels4
                Label label4x = new Label();
                label4x.Location = new Point(448, 195 + (i * 200));
                label4x.Size = new Size(250, 15);
                label4x.TextAlign = ContentAlignment.MiddleCenter;
                label4x.Text = "Equal";
                label4x.ForeColor = Color.DimGray;

                criterialabel4[i] = label4x;
                Controls.Add(criterialabel4[i]);


            // Add TrackBars
                TrackBar tbx = new TrackBar();
                tbx.Location = new Point(28, 150 + (i * 200));
                tbx.Size = new Size(686, 45);
                tbx.Minimum = 0;
                tbx.Maximum = 16;
                tbx.SmallChange = 1;
                tbx.LargeChange = 2;
                tbx.Value = 8;

                // use lambda expressions to enable events for each trackbar???
                tbx.Scroll += new EventHandler(delegate (Object o, EventArgs a) {

                    int trackbarnumber = i;
                    MessageBox.Show("worked " + trackbarnumber);




                                                                                });


                criteriatbs[i] = tbx;
                Controls.Add(criteriatbs[i]);


            }

Many thanks in advance for any help you can provide!

A: 

You can use either a lambda expression or an anonymous method. For example:

tbx.Scroll += (o, a) => {
  label1x.Text = tbx.Value.ToString();
};

You won't run into the common problems of anonymous functions unless you capture the loop variable itself, or declare the variables (tbx, label1x etc) outside the loop.

Jon Skeet
Thanks for the quick reply Jon, The problem is I don't want the functions to be anonymous. I need specific text boxes to update when a specific trackbar is changed. Any ideas? thanks again.
RHodgett
@RHodgett: Sorry, it's really not clear why you can't use the code I've provided. *Why* don't you want the functions to be anonymous? The sample I've given will update `criterialabel1[i]` when `criteriatbs[i]` changes.
Jon Skeet
you are 100% right, I'm sorry!
RHodgett