Connecting Tech Pros Worldwide Help | Site Map

Events registered with AddHandler not firing

 
LinkBack Thread Tools Search this Thread
  #1  
Old November 20th, 2005, 12:06 PM
Thiago Arrais
Guest
 
Posts: n/a
Default 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
arrais.usenet@nnospamm.qualiti.com

  #2  
Old November 20th, 2005, 12:07 PM
PresterJohn
Guest
 
Posts: n/a
Default Re: Events registered with AddHandler not firing

Try the dim for 'button' WithEvents...

--Prester John


"Thiago Arrais" <thiago.arrais-usenet@qualiti.com> wrote in message
news:8ebe93e1.0401281346.25993149@posting.google.c om...[color=blue]
> 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
> arrais.usenet@nnospamm.qualiti.com[/color]


  #3  
Old November 20th, 2005, 12:10 PM
Thiago Arrais
Guest
 
Posts: n/a
Default Re: Events registered with AddHandler not firing

Tried that. And also declaring it Private.

Only the last created button works.

"PresterJohn" <presterjohn@comcast.net> wrote in message news:<_rZRb.172681$I06.1724156@attbi_s01>...[color=blue]
> Try the dim for 'button' WithEvents...
>
> --Prester John
>
>
> "Thiago Arrais" <thiago.arrais-usenet@qualiti.com> wrote in message
> news:8ebe93e1.0401281346.25993149@posting.google.c om...[color=green]
> > 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...[/color][/color]
  #4  
Old November 20th, 2005, 12:14 PM
Thiago Arrais
Guest
 
Posts: n/a
Default Re: Events registered with AddHandler not firing

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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.