How to add check box to Context Menu

6 posts, 1 answers
  1. Ekin
    Ekin avatar
    11 posts
    Member since:
    Nov 2010

    Posted 20 Nov 2010 Link to this post

    Hello,

    I've created a RadContextMenu and wanted to add a few check boxes for user to check and un-check when right click.  I see that the RadContextMenu can add combo box, but what about checkbox?  After a check box is check. I want it to stay check until the user right click and un-check it. Is this possible?

    Thanks in advance for the help.

  2. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 21 Nov 2010 Link to this post

    Hi Ekin,

    Yes, this is actually quite a simple thing to do. I have added a checkbox in a context menu to a RadGridView in my sample below.
    First, you need to define a context menu, and to this, you can add a RadMenuHostItem, which can host a RadCheckBox 

    1: Define your RadContextMenu
    Private m_ContextMenu As RadContextMenu


    2: In FormLoad for exmaple, add the RadCheckBox to the context menu using a RadMenuHostItem
    m_ContextMenu = New RadContextMenu()
    Dim checkbox As New RadCheckBox()
    checkbox.Text = "Check Me"
    Dim hostedCheckBox As New RadMenuHostItem(checkbox)
    m_ContextMenu.Items.Add(hostedCheckBox)

    3: In the ContextMenuOpening event of the RadGridView, assign your RadContextMenu
    Private Sub RadGridView1_ContextMenuOpening(ByVal sender As System.Object, _
        ByVal e As Telerik.WinControls.UI.ContextMenuOpeningEventArgs) Handles RadGridView1.ContextMenuOpening
        e.ContextMenu = m_ContextMenu.DropDown
    End Sub

    Because the context of the context menu is defined outside of the scope of the ContextMenuOpening, the checkbox will remain checked or unchecked each time the context menu is opened.

    Hope that helps, but let me know if you need more information

    Regards,
    Richard
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Ekin
    Ekin avatar
    11 posts
    Member since:
    Nov 2010

    Posted 22 Nov 2010 Link to this post

    quick question...how to I handle the checked and unchecked event of the check box in the context menu? And close the context menu after the check or unchecked is selected

    Thank you so much..

  5. Answer
    Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 22 Nov 2010 Link to this post

    Hi Ekin,

    No problem, Here you go..

    1: Same as before, but add an event handler
    m_ContextMenu = New RadContextMenu()
    Dim checkbox As New RadCheckBox()
    checkbox.Text = "Check Me"
    Dim hostedCheckBox As New RadMenuHostItem(checkbox)
    m_ContextMenu.Items.Add(hostedCheckBox)
    '// Add an  event handler
    AddHandler checkbox.ToggleStateChanged, AddressOf CheckBoxChanged

    2: Handle the event and close the context menu
    Private Sub CheckBoxChanged(ByVal sender As Object, ByVal e As StateChangedEventArgs)
        Me.m_ContextMenu.DropDown.ClosePopup(RadPopupCloseReason.CloseCalled)
        '// I have access to e.ToggleState here
    End Sub


    Hope that help but let me know if you need more information
    Richard
  6. Ekin
    Ekin avatar
    11 posts
    Member since:
    Nov 2010

    Posted 22 Nov 2010 Link to this post

    Thank you! You're the man!!
  7. Themos
    Themos avatar
    18 posts
    Member since:
    Jul 2011

    Posted 06 Sep 2012 Link to this post

    Thank you for the code, it's been very helpful!

    I'm having the following problem though:
    How can I access the radcheckbox after it has been created?
    I can't seem to get a reference to the radcheckbox, which is a child of radmenuhost item, which is a child of radcontextmenu.

    Your help would be greatly appreciated.
    Regards



    Nevermind, I figured it out: I had to call the HostedControl method of the RadMenuHostItem instead of trying to access it as a child item!
Back to Top
UI for WinForms is Visual Studio 2017 Ready