Dot-Net
VB6 控制項數組最簡單的 .NET 等價物是什麼?
也許我只是對 .NET 還不夠了解,但我還沒有看到一種令人滿意的方法來在 .NET 中輕鬆實現這個簡單的 VB6 程式碼(假設此程式碼位於數組 Command1() 和 N 中包含 N 個 CommandButtons 的表單上數組 Text1()) 中的文本框:
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; }
VB .NET 所做的另一件好事是擁有一個處理多個控制項的事件處理程序:
Private Sub TextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles TextBox1.TextChanged, _ TextBox2.TextChanged, _ TextBox3.TextChanged End Sub