开发者

How do I add specific events to newly assigned controls in an Array [c#]

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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜