Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WinForms > Menu, Application Menu, Context Menu > event on radContextMenu

Not answered event on radContextMenu

Feed from this thread
  • Shirya avatar

    Posted on Oct 20, 2008 (permalink)

    Hello,
    I can't find the event when i click on a radMenuItem on my radContextMenu.
    I added a RadContextMenu to my form with only one radMenuITem(for the moment).

    When there is a rigth click (mouse) on my form, the contextMenu is shown.
    But i would like to do something when the radMenuItem is chosen.... how?
    thanx
    Shirya

    Reply

  • John avatar

    Posted on Oct 20, 2008 (permalink)

    Shirya,
        You need to create the event handler for the RadMenuItem.  This is done easily through the Properties Window, but I have shown how to accomplish it manually as well.  I apologize if my answer is oversimplified, hope it helps.

    Properties Window (quick way)
    1. While on your form, open the Visual Studio Properties window.
    2. Select the RadMenuItem from the combobox.  
    2. Change to the Events section of the Properties Window.  The icon is located at the top of the properties window and looks like a lightning bolt.
    3. Double-click the "Click" event and your event code will be generated for you.
    4. Enter your logic to execute when the menu item is clicked

    Manually create the handler
    To create the event handler manually see the code provided below.
            public SampleForm()  
            {  
                InitializeComponent();  
                this.radMenuItem1.Click += new EventHandler(radMenuItem1_Click);  
            }  
     
            void radMenuItem1_Click(object sender, EventArgs e)  
            {  
                //Perform your logic here  
            }  
     
            private void SampleForm_FormClosing(object sender, FormClosingEventArgs e)  
            {  
                this.radMenuItem1.Click -= new EventHandler(radMenuItem1_Click);  
            } 

    John

    Reply

  • D avatar

    Posted on Feb 26, 2009 (permalink)

    Can someone please give me the "Manual create the handler" code that is given below in VB ?  Thanks 

    Reply

  • John avatar

    Posted on Feb 26, 2009 (permalink)

    Hi D,
         The equivalent code for VB is below, hope this helps.

        Private Sub Form1_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load 
            AddHandler RadMenuItem1.Click, AddressOf Me.RadMenuItem1_Click 
        End Sub 
     
        Private Sub RadMenuItem1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) 
            'Perform your logic here 
        End Sub 
     
        Private Sub Form1_FormClosing(ByVal sender As System.ObjectByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing 
            RemoveHandler RadMenuItem1.Click, AddressOf Me.RadMenuItem1_Click 
        End Sub 

    Reply

  • D avatar

    Posted on Feb 26, 2009 (permalink)

    Thanks for your quick response.

    I still need some help.

    I have a tree. When the user right clicks on a node of the tree, I create radDropDownMenu in code depending on the node that is clicked. I also need to have event handler for each of the possible items selected. If I created 4 options in code for user to click on in the drop down box then I would have to have 4 event handlers. This number would change depending on which node was selected in the tree. How can I code the event handlers as I create the drop down box? I could be way off in my thinking. First time doing anything like this.
    (I could easily drop 6-8 controls on my form (to cover all possibilites) and fill them all in and create the events from the properties window as you suggested but it seems my team wants me to create them dynamically. I already got it working the easy way but now have to redo it.)

    Your help is appreciated.

    Reply

  • John avatar

    Posted on Feb 26, 2009 (permalink)

    I have created a rudimentary example below demonstrating the way you might go about handling the situation.  You also don't need to have separate event handlers for each RadMenuItem click event, they can all use the same one.  Below you will see that on Form Load I create two RadMenuItems, setup their click event to call the same handler, add them to the RadDropDownMenu and assign that to the first RadTreeView node.   In the handler for the RadMenuItems I look at the Tag property which was set when they were created to determine which was clicked and perform my logic accordingly.  

        Private Sub Form1_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load 
            'create first menu item and add the handler 
            Dim option1 As New RadMenuItem("Option1""option1"
            AddHandler option1.Click, AddressOf Me.RadMenuItem_Click 
     
            'create second menu item and use the same handler 
            Dim option2 As New RadMenuItem("Option2""option2"
            AddHandler option2.Click, AddressOf Me.RadMenuItem_Click 
     
            'create you new context menu with both the options 
            Dim customDDMenu As New RadDropDownMenu() 
            customDDMenu.Items.Add(option1) 
            customDDMenu.Items.Add(option2) 
     
            'assign the menu to the RadTreeView 
            RadTreeView1.Nodes(0).ContextMenu = customDDMenu 
     
        End Sub 
     
        Private Sub RadMenuItem_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) 
            'Cast the sender which is an Object to RadMenuItem so you see the properties 
            Dim menuItem = CType(sender, RadMenuItem) 
     
            'Use the Tag property which was set when the menu item was created 
            'to determine which control was clicked.  Then perform your logic accordingly. 
            If String.Compare(menuItem.Tag, "Option1"True) = 0 Then 
                'Perform logic here 
            ElseIf String.Compare(menuItem.Tag, "Option2"True) = 0 Then 
                'Perform logic here 
            Else 
                'Perform logic here 
            End If 
        End Sub 
     
     

    Reply

  • D avatar

    Posted on Feb 26, 2009 (permalink)

    Thanks - I will try it.  I appreciate your help.
     

    Reply

Back to Top

Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WinForms > Menu, Application Menu, Context Menu > event on radContextMenu
Related resources for "event on radContextMenu"

[ Features | Demos | Documentation | Knowledge Base | Telerik TV | Code Library | Step-by-step Tutorial | Blogs | Self-Paced Trainer ]