如何使用控制数组获取 C# 中给定对象的数组索引?

发布于 2024-07-24 01:51:22 字数 571 浏览 5 评论 0原文

我正在向表单动态添加一堆控件。 每个控件调用相同的方法,在该方法中我需要知道执行该操作的控件的数组索引。

CheckBox[] myCB = new CheckBox[100];
int i;
for (i = 0; i < 100; i++)
{
    myCB[i] = new CheckBox();
    myCB[i].Text = "Clicky!";
    myCB[i].Click += new System.EventHandler(dynamicbutton_Click);
    tableLayoutPanel1.Controls.Add(myCB[i]);
}

private void dynamicbutton_Click(Object sender, System.EventArgs e)
{
    label1.Text = sender.???array index property???.ToString();
}

因此,如果我单击 myCB[42] label1 将读取“42” 当然,如果有更简单的方法来处理动态控件,我会很感激指针。

I am dynamically adding a bunch of controls to a form. Each control calls the same method, and in that method I need to know the array index of the the control that performed the action.

CheckBox[] myCB = new CheckBox[100];
int i;
for (i = 0; i < 100; i++)
{
    myCB[i] = new CheckBox();
    myCB[i].Text = "Clicky!";
    myCB[i].Click += new System.EventHandler(dynamicbutton_Click);
    tableLayoutPanel1.Controls.Add(myCB[i]);
}

private void dynamicbutton_Click(Object sender, System.EventArgs e)
{
    label1.Text = sender.???array index property???.ToString();
}

So if I click myCB[42] label1 will read "42" Of course, if there is an easier way to handle dynamic controls I'd appreciate pointers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

辞旧 2024-07-31 01:51:22
private void dynamicbutton_Click(Object sender, System.EventArgs e)
{
    label1.Text = Array.IndexOf(myCB, (CheckBox)sender).ToString();
}
private void dynamicbutton_Click(Object sender, System.EventArgs e)
{
    label1.Text = Array.IndexOf(myCB, (CheckBox)sender).ToString();
}
伴随着你 2024-07-31 01:51:22

控件应该有一个 Tag 属性。 也许您可以将索引附加到标签上。 不过你会招致拳击……

Control's should have a Tag property. Maybe you can attach the index to the Tag. You will incur boxing though...

攒眉千度 2024-07-31 01:51:22
int j = i;
myCB[i].Click += delegate(object sender, EventArgs e) {
 // here you can use "j"

};
int j = i;
myCB[i].Click += delegate(object sender, EventArgs e) {
 // here you can use "j"

};
噩梦成真你也成魔 2024-07-31 01:51:22

一个明显的解决方案是设置标签:

CheckBox[] myCB = new CheckBox[100];
for (int i = 0; i < myCB.Length; i++)
{
    myCB[i] = new CheckBox();
    myCB[i].Text = "Clicky!";
    myCB[i].Click += new System.EventHandler(dynamicbutton_Click);
    myCB[i].Tag = i;
    tableLayoutPanel1.Controls.Add(myCB[i]);
}

然后:

private void dynamicbutton_Click(Object sender, System.EventArgs e)
{
    Control control = (Control) sender;
    label1.Text = sender.Tag.ToString();
}

另一种替代方法是捕获事件处理程序中的信息,最简单地使用 lambda 表达式或匿名方法:

CheckBox[] myCB = new CheckBox[100];
for (int i = 0; i < myCB.Length; i++)
{
    int index = i; // This is very important, as otherwise i will
                  // be captured for all of them
    myCB[i] = new CheckBox();
    myCB[i].Text = "Clicky!";
    myCB[i].Click += (s, e) => label1.Text = index.ToString();
    tableLayoutPanel1.Controls.Add(myCB[i]);
}

或者对于更复杂的行为:(

CheckBox[] myCB = new CheckBox[100];
for (int i = 0; i < myCB.Length; i++)
{
    int index= i; // This is very important, as otherwise i will
                  // be captured for all of them
    myCB[i] = new CheckBox();
    myCB[i].Text = "Clicky!";
    myCB[i].Click += (s, e) => DoSomethingComplicated(index, s, e);
    tableLayoutPanel1.Controls.Add(myCB[i]);
}

在其中声明 DoSomethingComplicated适当)。

One obvious solution would be to set the tag:

CheckBox[] myCB = new CheckBox[100];
for (int i = 0; i < myCB.Length; i++)
{
    myCB[i] = new CheckBox();
    myCB[i].Text = "Clicky!";
    myCB[i].Click += new System.EventHandler(dynamicbutton_Click);
    myCB[i].Tag = i;
    tableLayoutPanel1.Controls.Add(myCB[i]);
}

Then:

private void dynamicbutton_Click(Object sender, System.EventArgs e)
{
    Control control = (Control) sender;
    label1.Text = sender.Tag.ToString();
}

Another alternative is to capture the information in the event handler, most simply using a lambda expression or anonymous method:

CheckBox[] myCB = new CheckBox[100];
for (int i = 0; i < myCB.Length; i++)
{
    int index = i; // This is very important, as otherwise i will
                  // be captured for all of them
    myCB[i] = new CheckBox();
    myCB[i].Text = "Clicky!";
    myCB[i].Click += (s, e) => label1.Text = index.ToString();
    tableLayoutPanel1.Controls.Add(myCB[i]);
}

or for more complicated behaviour:

CheckBox[] myCB = new CheckBox[100];
for (int i = 0; i < myCB.Length; i++)
{
    int index= i; // This is very important, as otherwise i will
                  // be captured for all of them
    myCB[i] = new CheckBox();
    myCB[i].Text = "Clicky!";
    myCB[i].Click += (s, e) => DoSomethingComplicated(index, s, e);
    tableLayoutPanel1.Controls.Add(myCB[i]);
}

(where you declare DoSomethingComplicated appropriately).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文