473,385 Members | 1,474 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,385 software developers and data experts.

Dynamic AddressOf value for AddHandler

I'm building a windows forms app in VB.net 2005 and would like to know
if there is any way of adding a handler using a dynamic AddressOf
value?

The application will have a "Windows" menu item and I want to add a
list open forms as other menu items to this list. I want to be able to
do this dynamically and create a handler for each one that I add.
However, instead of creating the handler everytime i open a new form,
I want to use a single method that takes a couple of parameters and
adds the menu item and builds the handler. an example of what Id like
is shown below...

Private Sub AddItem(ByVal strMenuItem As String, ByVal strMenuItemName
As String)

If IsNothing(Me.menuWindows.DropDownItems.Item(strMen uItem)) Then
Dim oMI = New System.Windows.Forms.ToolStripMenuItem
Me.menuWindows.DropDownItems.AddRange(New
System.Windows.Forms.ToolStripItem() {oMI})
oMI.Name = strMenuItem
oMI.Text = strMenuItemName
AddHandler Me.menuWindows.DropDownItems(strMenuItem).Click,
AddressOf strMenuItem & "_Click"
End If

End Sub

Obviously I'd like to be able to remove menu items and handlers when
the forms are closed using a similar method. If anyone knows how I can
evalute 'strMenuItem & "_Click"' into a proper method name then I'd be
most grateful.

I thought that there would some some sort of Eval method that would do
it, and I'm sure there is something like it, but cant remember what it
is!

Thanks again
Jun 27 '08 #1
1 3378
"Karl Rhodes" <go**********@cortexa.co.ukschrieb
I'm building a windows forms app in VB.net 2005 and would like to
know if there is any way of adding a handler using a dynamic
AddressOf value?

The application will have a "Windows" menu item and I want to add a
list open forms as other menu items to this list. I want to be able
to do this dynamically and create a handler for each one that I add.
However, instead of creating the handler everytime i open a new
form, I want to use a single method that takes a couple of
parameters and adds the menu item and builds the handler. an example
of what Id like is shown below...

Private Sub AddItem(ByVal strMenuItem As String, ByVal
strMenuItemName As String)

If IsNothing(Me.menuWindows.DropDownItems.Item(strMen uItem)) Then
Dim oMI = New System.Windows.Forms.ToolStripMenuItem
Me.menuWindows.DropDownItems.AddRange(New
System.Windows.Forms.ToolStripItem() {oMI})
oMI.Name = strMenuItem
oMI.Text = strMenuItemName
AddHandler Me.menuWindows.DropDownItems(strMenuItem).Click,
AddressOf strMenuItem & "_Click"
End If

End Sub

Obviously I'd like to be able to remove menu items and handlers when
the forms are closed using a similar method. If anyone knows how I
can evalute 'strMenuItem & "_Click"' into a proper method name then
I'd be most grateful.

I thought that there would some some sort of Eval method that would
do it, and I'm sure there is something like it, but cant remember
what it is!

Thanks again
Do you have to have one event handler per menu item? You could also
handle them all in one procedure. The 'sender' arg points to, guess
what, the sender of the event, so you can still distingiush and keep the
event hooking simple.

Otherwise, you could use Reflection (see Framework docs TOC) to resolve
the procedure name at runtime. However, I'm not a friend of this
approach as long as there are straighter ways to do the same. Reflection
creates the potential risk of finding an error at runtime, which means
not ASAP, for the compiler isn't able to do the check. In addition, it's
(a little) slower at runtime. Doing this just for writing fewer source
code chars, should be avoided, IMO.

So, I suggest:

Private Sub AddItem( _
ByVal strMenuItem As String, _
ByVal strMenuItemName As String, _
byval Handler as EventHandler)

'...
AddHandler Me.menuWindows.DropDownItems(strMenuItem).Click, Handler
'...

End Sub

Call:
AddItem "bla","blub", addressof bla_Click

Let me please add: How much worth is this Sub? Do you really have to
check for Nothing? BTW, the 'Is' operator (Is Nothing) is more
straightforward than calling a function that does the same. Why do you
create an array of ToolStripItems for one single item? Do you really
need to set the name property? You could use one of the overloaded
constructors of the ToolStripMenuItem class, and, starting with VB
2008(?), you can use the With clause to initialize properties. Just a
thought.
AZ

Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Sudhir | last post by:
I have created a dynamic datagrid and would like to sort the grid when I click the column name in the header. ----------------------------------------------- Private Sub btnSearch_Click(ByVal...
2
by: Henry | last post by:
Hi, I have to create a variable amount of controls with events for a windows form in my vb code. Private WithEvents T_Label As Label Private WithEvents T_ButtonDo As Button I create the...
0
by: Adam Pedder | last post by:
Hi, I'm using the 2.0 Formview control from inside Visual Studio 2005 (VB), and am having a problem setting a dynamic value. Inside my Insert template I have: <asp:TextBox...
0
by: richard | last post by:
OK, Im finished pulling my hair out and now I need help. I have created a VB class file for my custom paging, in it I create a table with 2 rows, in the rows I have linkbuttons for first page,...
0
by: mtsylvester | last post by:
Hi All, How do I add a List Box to a dynamic Details View. I want to replace the check boxes with list boxes. If I try to simply replace the check box with a list box it complains about not being...
1
by: djjohnst | last post by:
I am trying to fill out a form from a record set and have a few menu boxes that have a static list of values. I want to fill out the default value with the variable from the recordset. The one...
0
by: vmyerpady | last post by:
how to get a selected value of a dropdownlist which is created dynamically in a asp table
1
by: SunshineInTheRain | last post by:
The following code is dynamic create dropdownmenu which data within pulled from database However, the code work well on IE but not on Firefox. On Firefox, the whole mouseover and mouseout function...
1
by: nightwalker | last post by:
hi all, I am trying to retrieve dynamic text value, but I need some helps with my script below. it doesn't get executed. Please help. for (var i =1; i <=3; i++) { ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.