如何将特定事件添加到数组中新分配的控件 [c#]
基本上我想将事件分配给创建的一些控件。我想在每个轨迹栏更改时更新与每个轨迹栏关联的标签(当 trackbar[i] 更改时更改 label1[i] 和 label2[i])。我通过谷歌搜索了一下,发现这可能是通过使用委托来完成的。我很喜欢使用 lambda 表达式,但我现在明白这仅适用于匿名事件?
请问有人可以把我推向正确的方向吗?
// 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]);
}
非常感谢您提供的任何帮助!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 lambda 表达式或匿名方法。例如:
除非您捕获循环变量本身,或者在循环外部声明变量(
tbx
、label1x
等),否则您不会遇到匿名函数的常见问题。You can use either a lambda expression or an anonymous method. For example:
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.