也许我还不太熟悉.NET,但我还没有看到一种令人满意的方式在.NET中轻松实现这个简单的VB6代码(假设这个代码在一个带有N CommandButtons的表单Command1()和N中数组Text1()中的TextBoxes:
- Private Sub Command1_Click(Index As Integer)
- Text1(Index).Text = Timer
- End Sub
我知道它不是非常有用的代码,但它证明了控制数组在VB6中的易用性. C#或VB.NET中最简单的等价物是什么?
制作文本框的通用列表:
- var textBoxes = new List<TextBox>();
- // Create 10 textBoxes in the collection
- for (int i = 0; i < 10; i++)
- {
- var textBox = new TextBox();
- textBox.Text = "TextBox " + i;
- textBoxes.Add(textBox);
- }
- // Loop through and set new values on textBoxes in collection
- for (int i = 0; i < textBoxes.Count; i++)
- {
- textBoxes[i].Text = "New value " + i;
- // or like this
- var textBox = textBoxes[i];
- textBox.Text = "New val " + i;
- }