469,295 Members | 1,927 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,295 developers. It's quick & easy.

Events registered with AddHandler not firing

I am writing a VS .NET Addin and I need to create menu items
(CommandBarButtons) at run-time. I can do that but the event handlers
do not work. They are registered using AddHandler (wich was created
for that, isn't it?), but I think I am using it wrongly, because only
the last registered button fires the event and only if it the button
variable is declared as a class field (sorry if the term is incorrect,
I am new to VB and .NET).

Any solution in C# or VB .NET is welcome. I actually started writing
the Addin in C# but changed to VB because the C# code couldn't fire
events. Events could be fired in VB using the Handles clause but now I
need to add handlers at runtime...

My Code (obviously simplified):

Public Class Connect

....

Dim button As CommandBarButton 'button as class field
Private Sub GuiInit()

...

For Each filestr In files

button =
myMenu.Controls.Add(MsoControlType.msoControlButto n)
button.Caption = Path.GetFileName(filestr)
button.Tag = filestr
AddHandler button.Click, AddressOf WizardButtonClick

Next

End Sub
Public Sub WizardButtonClick(ByVal Ctrl As CommandBarButton, ByRef
CancelDefault As Boolean)

handler.ProcessFile(Ctrl.Tag)

End Sub

If you don't want to post to the newsgroup, e-mail to
ar***********@nnospamm.qualiti.com
Nov 20 '05 #1
3 14262
Try the dim for 'button' WithEvents...

--Prester John
"Thiago Arrais" <th******************@qualiti.com> wrote in message
news:8e**************************@posting.google.c om...
I am writing a VS .NET Addin and I need to create menu items
(CommandBarButtons) at run-time. I can do that but the event handlers
do not work. They are registered using AddHandler (wich was created
for that, isn't it?), but I think I am using it wrongly, because only
the last registered button fires the event and only if it the button
variable is declared as a class field (sorry if the term is incorrect,
I am new to VB and .NET).

Any solution in C# or VB .NET is welcome. I actually started writing
the Addin in C# but changed to VB because the C# code couldn't fire
events. Events could be fired in VB using the Handles clause but now I
need to add handlers at runtime...

My Code (obviously simplified):

Public Class Connect

...

Dim button As CommandBarButton 'button as class field
Private Sub GuiInit()

...

For Each filestr In files

button =
myMenu.Controls.Add(MsoControlType.msoControlButto n)
button.Caption = Path.GetFileName(filestr)
button.Tag = filestr
AddHandler button.Click, AddressOf WizardButtonClick

Next

End Sub
Public Sub WizardButtonClick(ByVal Ctrl As CommandBarButton, ByRef
CancelDefault As Boolean)

handler.ProcessFile(Ctrl.Tag)

End Sub

If you don't want to post to the newsgroup, e-mail to
ar***********@nnospamm.qualiti.com

Nov 20 '05 #2
Tried that. And also declaring it Private.

Only the last created button works.

"PresterJohn" <pr*********@comcast.net> wrote in message news:<_rZRb.172681$I06.1724156@attbi_s01>...
Try the dim for 'button' WithEvents...

--Prester John
"Thiago Arrais" <th******************@qualiti.com> wrote in message
news:8e**************************@posting.google.c om...
I am writing a VS .NET Addin and I need to create menu items
(CommandBarButtons) at run-time. I can do that but the event handlers
do not work. They are registered using AddHandler (wich was created
for that, isn't it?), but I think I am using it wrongly, because only
the last registered button fires the event and only if it the button
variable is declared as a class field (sorry if the term is incorrect,
I am new to VB and .NET).

Any solution in C# or VB .NET is welcome. I actually started writing
the Addin in C# but changed to VB because the C# code couldn't fire
events. Events could be fired in VB using the Handles clause but now I
need to add handlers at runtime...

Nov 20 '05 #3
Found a solution to the problem.

You actually need to somehow keep a reference to the objects that you
registered the events for. I used an ArrayList.

Thank you,
Thiago Arrais
Nov 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by JPRoot | last post: by
2 posts views Thread by sara | last post: by
2 posts views Thread by Manuel | last post: by
15 posts views Thread by damiensawyer | last post: by
7 posts views Thread by SetonSoftware | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.