473,242 Members | 1,350 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,242 software developers and data experts.

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 14706
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: JPRoot | last post by:
Hi I use the following syntax to have events inherited from base to child classes which works nicely (virtual and override keyword on events). But I am wondering if it is a "supported" way of using...
1
by: Rick | last post by:
Hello all, I hope all is well with you. I am having a seriously difficult time with this problem. Allow me to set up the problem. I have a System.Web.UI.Page with the following controls...
2
by: sara | last post by:
hi all i need to fire a click events of a button that i add at run time what can i do?? i need to get a property value of other controls that i add also at runtime i want to get this property...
7
by: Denise | last post by:
I just realized the DataTable_RowChanging events were firing when I called Fill method of the DataAdapter! It fires TWICE for each row loaded. I thought these were only supposed to be called when...
7
by: Scott Emick | last post by:
How can I disable events for the controls on a form? I tried setting the form's enable property to false, but that doesn't stop events from firing on its controls. I need to temporarily disable...
2
by: Manuel | last post by:
When a form loads it fires all modification events for its controls. For example, if I put a comboBox and give it some values at design time, it will fire the comboBox_Change event (or whatever it...
4
by: Joergen Bech | last post by:
I sometimes use delegates for broadcasting "StateChanged" events, i.e. if I have multiple forms and/or controls that need updating at the same time as the result of a change in a global/common...
15
by: damiensawyer | last post by:
Hi, I am creating a class (from a base class) that needs to trigger events (code at bottom). I am instatiating the classes and wiring up the events as follows. clsDetermineConnection oDC =...
7
by: SetonSoftware | last post by:
I have a class with shared properties and methods that manages the open forms in my WinForms application, VB.NET 2005. The data contained within must persist for the lifetime of the application....
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.