event on radContextMenu

7 posts, 0 answers
  1. Shirya
    Shirya avatar
    40 posts
    Member since:
    Sep 2008

    Posted 20 Oct 2008 Link to this post

    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
  2. John
    John avatar
    30 posts
    Member since:
    Aug 2008

    Posted 20 Oct 2008 Link to this post

    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
  3. UI for WinForms is Visual Studio 2017 Ready
  4. D
    D avatar
    7 posts
    Member since:
    Feb 2009

    Posted 26 Feb 2009 Link to this post

    Can someone please give me the "Manual create the handler" code that is given below in VB ?  Thanks 
  5. John
    John avatar
    30 posts
    Member since:
    Aug 2008

    Posted 26 Feb 2009 Link to this post

    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 

  6. D
    D avatar
    7 posts
    Member since:
    Feb 2009

    Posted 26 Feb 2009 Link to this post

    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.
  7. John
    John avatar
    30 posts
    Member since:
    Aug 2008

    Posted 26 Feb 2009 Link to this post

    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 
     
     

  8. D
    D avatar
    7 posts
    Member since:
    Feb 2009

    Posted 26 Feb 2009 Link to this post

    Thanks - I will try it.  I appreciate your help.
     
Back to Top
UI for WinForms is Visual Studio 2017 Ready