Dynamic Objects with VB.net 
Author Message
 Dynamic Objects with VB.net

Hi
how can i create dynamic Objects like Buttons, and how can i react to this
object events. Little Sample pls

JD



Fri, 08 Oct 2004 00:10:12 GMT  
 Dynamic Objects with VB.net
J?rg,
Use 'New Button()' to create a new button. Remembering to add it to the
Controls collection of thr form/control that you are one.

Then use 'AddHandler' to set any event handlers you want...

    Sub AddButton()
        Dim btn as New Button()
        ControlsAdd(btn)
        AddHandler btn.Click, AddressOf Button1_Click
    End Sub

You can cast the 'sender' parameter of the Click event to type Button to
find out specifically which button fired the event...

Hope this helps
Jay


Quote:
> Hi
> how can i create dynamic Objects like Buttons, and how can i react to this
> object events. Little Sample pls

> JD



Fri, 08 Oct 2004 03:24:38 GMT  
 Dynamic Objects with VB.net
Thanks, but how is the syntax to cast the different events (example to cast
the sender of my listview) ?

JD



Sat, 09 Oct 2004 01:15:54 GMT  
 Dynamic Objects with VB.net
J?rg,
I don't entirely follow your question.

The parameters to the event handler have to match what the event expects. I
normally start with an event procedure to a Button that I manually add to
the form, then remove the 'Handles' clause on the end...

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
        If TypeOf sender Is Button Then
            Dim btn As Button
            btn = CType(sender, Button)
            btn...
        End If
    End Sub

Hope this helps
Jay


Quote:
> Thanks, but how is the syntax to cast the different events (example to
cast
> the sender of my listview) ?

> JD



Sat, 09 Oct 2004 09:27:16 GMT  
 Dynamic Objects with VB.net
OK, the Problem is the LISTview, not the Button

I want to find out witch item was clicked

Thanks for your Help
JD



Sun, 10 Oct 2004 01:12:34 GMT  
 Dynamic Objects with VB.net
Private Sub ListViewWindowsTools_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ListViewWindowsTools.SelectedIndexChanged

If TypeOf sender Is ListView Then

Dim a As ListView

a = CType(sender, ListView)

MessageBox.Show(a.SelectedItems.Item(0).Text.ToString)

End If

End Sub

This doesn't work

JD



Sun, 10 Oct 2004 01:31:42 GMT  
 Dynamic Objects with VB.net
JD,
If you put a break point on the first line of the event handler (If
TypeOf...)

What does the Watch window say the type of sender is?

Hope this helps
Jay


Quote:
> Private Sub ListViewWindowsTools_SelectedIndexChanged(ByVal sender As
> System.Object, ByVal e As System.EventArgs) Handles
> ListViewWindowsTools.SelectedIndexChanged

> If TypeOf sender Is ListView Then

> Dim a As ListView

> a = CType(sender, ListView)

> MessageBox.Show(a.SelectedItems.Item(0).Text.ToString)

> End If

> End Sub

> This doesn't work

> JD



Sun, 10 Oct 2004 09:35:17 GMT  
 Dynamic Objects with VB.net
Sender is "ListView", the MessageBox pops up only one time !?


Mon, 11 Oct 2004 01:08:11 GMT  
 Dynamic Objects with VB.net

Quote:
> Private Sub ListViewWindowsTools_SelectedIndexChanged(ByVal
> sender As System.Object, ByVal e As System.EventArgs) Handles
> ListViewWindowsTools.SelectedIndexChanged

> If TypeOf sender Is ListView Then

> Dim a As ListView

> a = CType(sender, ListView)

> MessageBox.Show(a.SelectedItems.Item(0).Text.ToString)

> End If

> End Sub

> This doesn't work

"Doesn't work" means what? Compiler error? Runtime error? Logical error?

Armin



Mon, 11 Oct 2004 01:38:33 GMT  
 Dynamic Objects with VB.net
One problem I can see off the bat is that if nothing's selected (i.e. if the
user clicks on an empty area of the listview), you'll get an error because
SelectedItems.Item(0) doesn't exist.


Quote:

> > Private Sub ListViewWindowsTools_SelectedIndexChanged(ByVal
> > sender As System.Object, ByVal e As System.EventArgs) Handles
> > ListViewWindowsTools.SelectedIndexChanged

> > If TypeOf sender Is ListView Then

> > Dim a As ListView

> > a = CType(sender, ListView)

> > MessageBox.Show(a.SelectedItems.Item(0).Text.ToString)

> > End If

> > End Sub

> > This doesn't work

> "Doesn't work" means what? Compiler error? Runtime error? Logical error?

> Armin



Mon, 11 Oct 2004 04:35:56 GMT  
 Dynamic Objects with VB.net
Hi;
Do you mean that for every object created at runtime I have to AddHandler to
the object is going to manage his event?

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?

Thank you.

--
Saludos;

Eladio Rincn


Quote:
> J?rg,
> Use 'New Button()' to create a new button. Remembering to add it to the
> Controls collection of thr form/control that you are one.

> Then use 'AddHandler' to set any event handlers you want...

>     Sub AddButton()
>         Dim btn as New Button()
>         ControlsAdd(btn)
>         AddHandler btn.Click, AddressOf Button1_Click
>     End Sub

> You can cast the 'sender' parameter of the Click event to type Button to
> find out specifically which button fired the event...

> Hope this helps
> Jay



> > Hi
> > how can i create dynamic Objects like Buttons, and how can i react to
this
> > object events. Little Sample pls

> > JD



Wed, 13 Oct 2004 17:46:04 GMT  
 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



Wed, 13 Oct 2004 20:07:50 GMT  
 Dynamic Objects with VB.net
Thanks Armin.

That works great.

(I was looking for this answer for sometime and finally a Google
search brought me to your post.)

I appreciate the help.

--Mark.

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).

> > 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



Wed, 20 Oct 2004 03:09:46 GMT  
 
 [ 13 post ] 

 Relevant Pages 

1. VB.NEt Beginner: Creating a COM+ Object in VB.net

2. Passing Form Fields to VB.NET Custom Business Object in an ASP.NET Code Behind Page

3. Dynamic arrays in VB.NET

4. Dynamic creation of web controls in VB.NET

5. dynamic arrays in VB.net

6. help in vb.net (dynamic or otherwise)

7. help in vb.net -dynamic or otherwise

8. dynamic help not available in vb.net

9. vb.net datagrid dynamic array and navigation

10. How can I get the dynamic ip adress with vb.net

11. Passing a valid Session Object from .NET to VB 6 objects

12. Help Please - Dynamic Menus in VB.NET (or VB6)

 

 
Powered by phpBB® Forum Software