This is a migrated thread and some comments may be shown as answers.

How to add check box to Context Menu

5 Answers 1159 Views
ContextMenu
This is a migrated thread and some comments may be shown as answers.
Ekin
Top achievements
Rank 1
Ekin asked on 21 Nov 2010, 02:45 AM

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.

5 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 21 Nov 2010, 05:10 PM
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
0
Ekin
Top achievements
Rank 1
answered on 22 Nov 2010, 07:40 AM

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..

0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 22 Nov 2010, 08:36 AM
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
0
Ekin
Top achievements
Rank 1
answered on 22 Nov 2010, 12:04 PM
Thank you! You're the man!!
0
Themos
Top achievements
Rank 1
answered on 06 Sep 2012, 07:56 AM
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!
Tags
ContextMenu
Asked by
Ekin
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Ekin
Top achievements
Rank 1
Themos
Top achievements
Rank 1
Share this question
or