
Dynamic Objects with VB.net
Quote:
> Hi;
> Do you mean that for every object created at runtime I have to
> AddHandler to the object is going to manage his event?
You can. The other way is to declare a WithEvents variable. Withevents
implicitly calls Addhandler for the object assigned to the variable using
the given event procedure (Handles keyword).
Quote:
> I come from VB6 and it was so easy to build at design time an
> array of controls (Textboxes for example), and manage the event
> with a select case: public sub txt_KeyPress( index as integer)
> select case index
> case t.tNombre
> ...
> case t.tApellido
> ...
> end select
> end sub
> Is there any "similar" way to work with an array of objects in
> the way I did in VB6?
> Why is not possible to build objects arrays in design time?
There's no need for it. You can use Addhandler now. Addhandler was not there
in VB6 that's why you had to use either control arrays for creating an
undetermined number of controls or use a fixed number of
WithEvents-variables.
You can still simply add multiple controls to one event handler at design
time in VB.net:
1. Put the first textbox on the designer
2. create the event routine (keypress etc.)
3. create more textboxes
4. change your code by typing the name of all
textboxes after the Handles keyword in the procedure head:
public sub txt_KeyPress(...) _
Handles txt1.keypress, txt2.keypress, txt3.keypress
Apart from 4. it's the same as in VB6. But you're right, it's a little more
to type.
The other way is to add the handler at runtime:
AddHandler Text1.KeyPress, AddressOf txt_KeyPress
AddHandler Text2.KeyPress, AddressOf txt_KeyPress
Preferred for a large number of controls:
Dim txt As TextBox
For Each txt In New TextBox() {Text1, Text2, Text3, Text4, [...TextN]}
AddHandler txt.KeyPress, AddressOf txt_KeyPress
Next
Armin