制作索引控件数组? [英] Making an indexed control array?

查看:124
本文介绍了制作索引控件数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有C#索引的控件数组或没有?我想提出一个按钮阵列,例如与只使用一个事件处理程序处理所有这5控制的指标(如VB6一样)5个按钮。否则我必须写为每个5个按键一个额外的事件处理程序。如果我有100个按键,我需要100事件处理程序?我的意思是类似的东西:

Has C# indexed control arrays or not? I would like to put a "button array" for example with 5 buttons which use just one event handler which handles the index of all this 5 controls (like VB6 does). Else I have to write for each of these 5 buttons one extra event handler. And if I have 100 buttons, I need 100 event handlers? I mean something like that:

TextBox1[i].Text="Example";

这可能使编码肯定我更容易与控制数组。现在,我已经看到,C#,至少对用户控件没有可见的阵列功能,并在用户控件没有指标属性。所以我想C#有没有控制阵列,或者我必须用已知的名称每个元素调用。

It could make coding definitely easier for me to work with control arrays. Now I have seen, that C# at least has no visible array functionality on user controls and no "index" property on the user controls. So I guess C# has no control arrays, or I must each element call by known name.

而不是在给100文本框循环递增100价值,我必须写:

Instead of giving 100 TextBoxes in a for loop 100 incrementing values, I have to write:

TextBox1.Text = Value1;
TextBox2.Text = Value2;
...
...
TextBox100.Text = Value100;

很多更多的工作+所有这些100事件处理程序为每一个额外的文本框额外的费用。

A lot of more work + all these 100 event handlers each for one additional TextBox extra.

推荐答案

正如我在评论中提及了由HatSoft的解决方案,C#的WinForms不允许您创建控件数组像老VB6使我们。我想我们可以去最近的是HatSoft和伯特·埃文斯在自己的岗位上表现出来了。

As I mentioned in comment to a solution by HatSoft, C# Winforms does not allow you to create control arrays like old VB6 allowed us. The nearest I think we can get to is what HatSoft and Bert Evans in their posts have shown.

一件事,我希望能够满足您的要求是事件处理程序,你会得到一个共同的事件处理程序,并在事件处理程序,当你强制转换发件人你直接得到控制,就像你会在VB6

One thing that I hope would satisfy your requirement is the event handler, you get a common event handler and in the event handler when you typecast the "sender" you get the control directly just like you would in VB6

C#

TextBox textBox = sender as TextBox;

VB6

TextBox textBox = TextBox1[i];

所以,你可能已经是接线的100文本框到一个单一的,甚至处理,如果你不通过code创建控件动态,而在设计时手动创建它,然后所有的人能建议的唯一麻烦的是他们组容器好比说面板。然后在窗体的Load丝他们都到这样一个单一的事件处理程序:

So the only trouble you might have is wiring those 100 TextBoxes to a single even handler, if you are not creating the controls dynamically through code rather creating it manually at design time then all one can suggest is group them in a container like say Panel. Then on Form Load wire them all up to a single event handler like this:

foreach (Control control in myTextBoxPanel.Controls)
{
    if(control is TextBox)
         control.TextChanged += new EventHandler(control_TextChanged);
}

这篇关于制作索引控件数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆