Generalized click events for menu items using function
Generalized click events for menu items using function
Generalized click events for menu items using function
Generalized click events for menu items using function
Generalized click events for menu items using function
Generalized click events for menu items using function Generalized click events for menu items using function Generalized click events for menu items using function Generalized click events for menu items using function Generalized click events for menu items using function Generalized click events for menu items using function Generalized click events for menu items using function Generalized click events for menu items using function
Generalized click events for menu items using function Generalized click events for menu items using function
Generalized click events for menu items using function
Go Back  Xtreme Visual Basic Talk > > > Generalized click events for menu items using function


Reply
 
Thread Tools Display Modes
  #1  
Old 03-14-2013, 10:42 AM
RameshwarPrasad RameshwarPrasad is offline
Newcomer
 
Join Date: Mar 2013
Posts: 4
Default Generalized click events for menu items using function


Delegates for menu item click events

Hello All,

I am trying to create a general class which defines a function to create menus using code in vb.net 2010. Everything works fine till i have to assign a sub to respond to the user click event. i am trying to use delegates but to no avail. Can somebody please help me out. Sample use of my function:

'SYNTAX:construct_main_menu(formname,"&File","&New","&open",,,"text"," doc",,,"text","doc")
DESIRED:

'SYNTAX:construct_main_menu(formname,"&File","&New","&open",,,"text"," doc",,,"text","doc",File_click ,New_click)
File_click, New_click would be subs i would have defined in my own project responding to click events of 'File' and 'New'


Public MustInherit Class Menu_Builder

'This class builds the required menus and sub menus

'|mainitem
'|->|sub item->->sub-subitem->->sub-subitem->->sub-subitem

'|->|subitem->->sub-subitem->->sub-subitem->->sub-subitem

'|->|subitem->->sub-subitem->->sub-subitem->->sub-subitem

'|->|subitem->->sub-subitem->->sub-subitem->->sub-subitem

'For Eg you want to have
'File->New->-->txt or doc
'File->open->txt or doc
'SYNTAX:construct_main_menu(formname,"&File","&New","&open",,,"text"," doc",,,"text","doc")

Dim menutiem1clicksub As MenuClicksubs
Private Event myevent()
Dim menu As New MainMenu
Public Sub Construct_Main_Menu(ByRef formname As Form, Optional ByVal mainitem As String = "", Optional ByVal Subitem1 As String = "", Optional ByVal Subitem2 As String = "", Optional ByVal Subitem3 As String = "", Optional ByVal Subitem4 As String = "", _
Optional ByVal SubSubitem1 As String = "", Optional ByVal SubSubitem2 As String = "", Optional ByVal SubSubitem3 As String = "", Optional ByVal SubSubitem4 As String = "", Optional ByVal SubSubitem5 As String = "", Optional ByVal SubSubitem6 As String = "", _
Optional ByVal SubSubitem7 As String = "", Optional ByVal SubSubitem8 As String = "", Optional ByVal SubSubitem9 As String = "", Optional ByVal SubSubitem10 As String = "", Optional ByVal SubSubitem11 As String = "", Optional ByVal SubSubitem12 As String = "")
Dim mainitem1 As New MenuItem(mainitem)
Dim submainitem1 As New MenuItem(Subitem1)
Dim submainitem2 As New MenuItem(Subitem2)
Dim submainitem3 As New MenuItem(Subitem3)
Dim submainitem4 As New MenuItem(Subitem4)
Dim subsubmainitem1 As New MenuItem(SubSubitem1)
Dim subsubmainitem2 As New MenuItem(SubSubitem2)
Dim subsubmainitem3 As New MenuItem(SubSubitem3)
Dim subsubmainitem4 As New MenuItem(SubSubitem4)
Dim subsubmainitem5 As New MenuItem(SubSubitem5)
Dim subsubmainitem6 As New MenuItem(SubSubitem6)
Dim subsubmainitem7 As New MenuItem(SubSubitem7)
Dim subsubmainitem8 As New MenuItem(SubSubitem8)
Dim subsubmainitem9 As New MenuItem(SubSubitem9)
Dim subsubmainitem10 As New MenuItem(SubSubitem10)
Dim subsubmainitem11 As New MenuItem(SubSubitem11)
Dim subsubmainitem12 As New MenuItem(SubSubitem12)
menu.MenuItems.Add(mainitem1)
mainitem1.MenuItems.Add(submainitem1)
mainitem1.MenuItems.Add(submainitem2)
mainitem1.MenuItems.Add(submainitem3)
mainitem1.MenuItems.Add(submainitem4)
submainitem1.MenuItems.Add(subsubmainitem1)
submainitem1.MenuItems.Add(subsubmainitem2)
submainitem1.MenuItems.Add(subsubmainitem3)
submainitem2.MenuItems.Add(subsubmainitem4)
submainitem2.MenuItems.Add(subsubmainitem5)
submainitem2.MenuItems.Add(subsubmainitem6)
submainitem3.MenuItems.Add(subsubmainitem7)
submainitem3.MenuItems.Add(subsubmainitem8)
submainitem3.MenuItems.Add(subsubmainitem9)
submainitem4.MenuItems.Add(subsubmainitem10)
submainitem4.MenuItems.Add(subsubmainitem11)
submainitem4.MenuItems.Add(subsubmainitem12)
With formname
.Menu = menu
End With
'Code which invalidates the visiblity of a menuitem if it is empty

'Desired result is achieved but have to shorten the code
'For SubSubmainitems
If SubSubitem12 = "" Then
subsubmainitem12.Visible = False
End If

If SubSubitem11.ToString = "" Then
subsubmainitem11.Visible = False
End If

If SubSubitem10.ToString = "" Then
subsubmainitem10.Visible = False
End If

If SubSubitem9.ToString = "" Then
subsubmainitem9.Visible = False
End If

If SubSubitem8.ToString = "" Then
subsubmainitem8.Visible = False
End If

If SubSubitem7.ToString = "" Then
subsubmainitem7.Visible = False
End If

If SubSubitem6.ToString = "" Then
subsubmainitem6.Visible = False
End If

If SubSubitem5.ToString = "" Then
subsubmainitem5.Visible = False
End If

If SubSubitem4.ToString = "" Then
subsubmainitem4.Visible = False
End If

If SubSubitem3.ToString = "" Then
subsubmainitem3.Visible = False
End If

If SubSubitem2.ToString = "" Then
subsubmainitem2.Visible = False
End If

If SubSubitem1.ToString = "" Then
subsubmainitem1.Visible = False
End If
'For SUBMAINITEM now
If Subitem4.ToString = "" Then
submainitem4.Visible = False
End If

If Subitem3.ToString = "" Then
submainitem3.Visible = False
End If

If Subitem2.ToString = "" Then
submainitem2.Visible = False
End If

If Subitem1.ToString = "" Then
submainitem1.Visible = False
End If
'For mainitem
If mainitem = "" Then
mainitem1.Visible = False
End If
'
'
'
'
'Adding Event Handlers
AddHandler mainitem1.Click, AddressOf mainitem1_Click
AddHandler submainitem1.Click, AddressOf submainitem1_click
AddHandler submainitem2.Click, AddressOf submainitem2_click
AddHandler submainitem3.Click, AddressOf submainitem3_Click
AddHandler submainitem4.Click, AddressOf submainitem4_Click
AddHandler subsubmainitem1.Click, AddressOf subsubmainitem1_Click
AddHandler subsubmainitem2.Click, AddressOf subsubmainitem2_Click
AddHandler subsubmainitem3.Click, AddressOf subsubmainitem3_Click
AddHandler subsubmainitem4.Click, AddressOf subsubmainitem4_Click
AddHandler subsubmainitem5.Click, AddressOf subsubmainitem5_Click
AddHandler subsubmainitem6.Click, AddressOf subsubmainitem6_Click
AddHandler subsubmainitem7.Click, AddressOf subsubmainitem7_Click
AddHandler subsubmainitem8.Click, AddressOf subsubmainitem8_Click
AddHandler subsubmainitem9.Click, AddressOf subsubmainitem9_Click
AddHandler subsubmainitem10.Click, AddressOf subsubmainitem10_Click
AddHandler subsubmainitem11.Click, AddressOf subsubmainitem11_Click
AddHandler subsubmainitem12.Click, AddressOf subsubmainitem12_Click

AddHandler myevent, AddressOf mainitem1_Click

End Sub

Protected Overridable Sub mainitem1_Click(ByVal sender As Object, ByVal e As System.EventArgs)

End Sub
'For Sub menu items
'total 4 under one
Protected Overridable Sub submainitem1_Click(ByVal sender As Object, ByVal e As System.EventArgs)

End Sub

Protected Overridable Sub submainitem2_Click(ByVal sender As Object, ByVal e As System.EventArgs)

End Sub
Protected Overridable Sub submainitem3_Click(ByVal sender As Object, ByVal e As System.EventArgs)

End Sub
Protected Overridable Sub submainitem4_Click(ByVal sender As Object, ByVal e As System.EventArgs)

End Sub
'For sub sub menu items
Protected Overridable Sub subsubmainitem1_Click(ByVal sender As Object, ByVal e As System.EventArgs)

End Sub

Protected Overridable Sub subsubmainitem2_Click(ByVal sender As Object, ByVal e As System.EventArgs)

End Sub

Protected Overridable Sub subsubmainitem3_Click(ByVal sender As Object, ByVal e As System.EventArgs)

End Sub

Protected Overridable Sub subsubmainitem4_Click(ByVal sender As Object, ByVal e As System.EventArgs)

End Sub

Protected Overridable Sub subsubmainitem5_Click(ByVal sender As Object, ByVal e As System.EventArgs)

End Sub

Protected Overridable Sub subsubmainitem6_Click(ByVal sender As Object, ByVal e As System.EventArgs)

End Sub

Protected Overridable Sub subsubmainitem7_Click(ByVal sender As Object, ByVal e As System.EventArgs)

End Sub

Protected Overridable Sub subsubmainitem8_Click(ByVal sender As Object, ByVal e As System.EventArgs)

End Sub

Protected Overridable Sub subsubmainitem9_Click(ByVal sender As Object, ByVal e As System.EventArgs)

End Sub

Protected Overridable Sub subsubmainitem10_Click(ByVal sender As Object, ByVal e As System.EventArgs)

End Sub

Protected Overridable Sub subsubmainitem11_Click(ByVal sender As Object, ByVal e As System.EventArgs)

End Sub

Protected Overridable Sub subsubmainitem12_Click(ByVal sender As Object, ByVal e As System.EventArgs)

End Sub
End Class

Thank you guys if you make it till here!!!
Reply With Quote
  #2  
Old 03-14-2013, 11:48 AM
passel's Avatar
passelGeneralized click events for menu items using function passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,024
Default

I don't see where you're doing any WithEvents on the objects you are creating handlers for.
Perhap something like
Dim WithEvents mainitem1 As New MenuItem(mainitem)
Dim WithEvents submainitem1 As New MenuItem(Subitem1)
'etc...
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #3  
Old 03-14-2013, 06:30 PM
RameshwarPrasad RameshwarPrasad is offline
Newcomer
 
Join Date: Mar 2013
Posts: 4
Default

Hi,

Even if do put withevents, the thing which is making me more flummoxed is the question how to pass a sub as a parameter into another sub?

For eg Optional ByVal mainitem As String
so for sub ideally: "optional Byval sub1 as sub" which is not the case.

Regards
Rameshwar Prasad
Reply With Quote
  #4  
Old 03-14-2013, 08:21 PM
RameshwarPrasad RameshwarPrasad is offline
Newcomer
 
Join Date: Mar 2013
Posts: 4
Default

hello Passel

Can you please elaborate. Cause i am stuck here with no progress

Thanks
Rameshwar
Reply With Quote
  #5  
Old 03-15-2013, 04:39 AM
PlausiblyDamp's Avatar
PlausiblyDampGeneralized click events for menu items using function PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

If you wish to pass a function or sub as a parameter to another function or sub then you will need to use a Delegate
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #6  
Old 03-16-2013, 05:26 AM
RameshwarPrasad RameshwarPrasad is offline
Newcomer
 
Join Date: Mar 2013
Posts: 4
Default

Can you give me an example?
Reply With Quote
  #7  
Old 03-17-2013, 04:43 PM
PlausiblyDamp's Avatar
PlausiblyDampGeneralized click events for menu items using function PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

Using a SUB as an argument to a SUB has an example of using a delegate to pass a function as a parameter to a method.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Generalized click events for menu items using function
Generalized click events for menu items using function
Generalized click events for menu items using function Generalized click events for menu items using function
Generalized click events for menu items using function
Generalized click events for menu items using function
Generalized click events for menu items using function Generalized click events for menu items using function Generalized click events for menu items using function Generalized click events for menu items using function Generalized click events for menu items using function Generalized click events for menu items using function Generalized click events for menu items using function
Generalized click events for menu items using function
Generalized click events for menu items using function
 
Generalized click events for menu items using function
Generalized click events for menu items using function
 
-->