473,386 Members | 1,830 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,386 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 14718
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.