What's the simplest .NET equivalent of a VB6 c

2019-01-20 09:09发布

Maybe I just don't know .NET well enough yet, but I have yet to see a satisfactory way to implement this simple VB6 code easily in .NET (assume this code is on a form with N CommandButtons in array Command1() and N TextBoxes in array Text1()):

Private Sub Command1_Click(Index As Integer)

   Text1(Index).Text = Timer

End Sub

I know it's not very useful code, but it demonstrates the ease with which control arrays can be used in VB6. What is the simplest equivalent in C# or VB.NET?

8条回答
时光不老,我们不散
2楼-- · 2019-01-20 09:40

VisualBasic .NET's compatibility library contains strong typed control arrays. This is what the upgrade wizard uses to replace the current VB6 control arrays.

However, A control array in VB6 is just a collection of objects with VB6 doing some syntax magic on the surface. In the .NET world, by removing this, they are forcing better practices.

In closing, with the advent of generics, there is nothing stopping you from using

List<YourControl> MyControlArray.
查看更多
Viruses.
3楼-- · 2019-01-20 09:41

The two main benefits of control arrays in VB6 were: (1) They provided a way for you to iterate through a collection of controls (2) They allowed you to share events between controls

(1) can be accomplished in .Net using an array of controls (2) can be accomplished by having one event handle multiple controls (the syntax is a little different because you use the sender argument instead of myArray(index)).

One nice thing about .Net is that these features are decoupled. So for instance you can have controls that share events even if they aren't part of an array and have different names and even a different type. And you can iterate through a collection of controls even if they have totally different events.

查看更多
登录 后发表回答