Visual Basic 2010 Creating a Control Array

后端 未结 5 1225
你的背包
你的背包 2020-12-21 09:03

How can I make a control array? Or the equivalent.

I am used to Visual Basic 6 which presents the option of whether to create a control array when I copy and paste a

5条回答
  •  醉梦人生
    2020-12-21 09:26

    Another implicit feature of the control arrays that is easily overlooked is the association of numeric indexes with each element of the array. The indexes can be any positive int as long as they areunique in the collection; they do not have to be sequential. Consequently the array is more like a dictionary indexed by integers with each item value being an instance of a specific type of control.

    Logic in the VB6 event handlers for the control array get the value of the index along with the attributes of the event to be handled. Logic in the handler typically uses the index to determine which instance of the control was raising the event.

    .NET event handlers are quite different. You will typically get a reference to a specific control instance and an instance of a specific event object with the attributes of the event to be handled. You will NOT get the index.

    Also, VB6 apps sometimes have logic that iterates/manipulates the control array as an array.

    In our default translations, we try to support legacy VB6 logic that depends explicitly on the control array and its indexes. We rewrite control arrays as a group of individual control instances and then we add them to a generic OrderDictionary during form initialization. The individual controls subscribe to the events and we can use the collection to lookup the index given a control instance or to iterate/manipulate the items in the "array". If you do not explicitly need the index or the array, you can get rid of the collection.

    Dynamically adding controls is more work now -- it was conceptually like adding an item to the control array and supported with a single statement in VB6 (Load control). As far as I know, in .NET you need to clone a control, deep copy the properties explicitly, and hook up the event handlers explicitly. It can be generalized using reflection and other moderately advanced techniques -- but it sure is not simply calling "load control". If there is an easier way to do this in .NET I would love to hear about it. FWIW: we translate to a helper function.

提交回复
热议问题