RadContextMenu on RadTextBox

1 Answer 76 Views
ContextMenu TextBox
Todd
Top achievements
Rank 1
Todd asked on 14 Aug 2023, 03:47 PM

I have several RadTextBox controls on a form and want to use a single RadContextMenu to provide my own context menu. Depening on which control is selected the resulting action will be a little different. 

I have this code to assign my context menus to the 2 controls.

this.txtBreedersAccount.TextBoxElement.TextBoxItem.HostedControl.ContextMenu = new ContextMenu();
this.txtBreedersAccount.TextBoxElement.TextBoxItem.HostedControl.MouseDown += new MouseEventHandler(this.AccountContextMenu_MouseDown);
this.txtMemberAccount.TextBoxElement.TextBoxItem.HostedControl.ContextMenu = new ContextMenu();
this.txtMemberAccount.TextBoxElement.TextBoxItem.HostedControl.MouseClick += new MouseEventHandler(this.AccountContextMenu_MouseDown);

This is my MouseDown procedure.

   private void AccountContextMenu_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {                
                this.cmnuAccount.Show(MousePosition);
            }
        }

All of this works perfectly. My issue is the I can't seem to find a way to determine which of my 2 controls was actually used to instantiate the RadContextMenu. Searching the closest I could get was something like this, but it doesn't work.

        private void cmnuAccount_DropDownOpening(object sender, System.ComponentModel.CancelEventArgs e)
        {
            RadElement element = sender as RadElement;

            if (element == null)
            {
                return;
            }

            this._currentControl = element.ElementTree.Control as RadControl;
        }

Textboxes seem to be treated differently than other RadControls. If I setup the context menu on a checkbox for example it work without any issues.

I'd appreciate any help that anyone could give. Thanks.

1 Answer, 1 is accepted

Sort by
1
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 15 Aug 2023, 08:19 AM

Hi, Todd,

RadTextBox internally hosts the standard MS TextBox. That is why it may seem to be treated differently compared to other RadControls. If you want to detect which control is requesting the context menu in the RadContextMenu.DropDownOpening event, you can use the following approach:

        Control _currentControl;
        private void RadContextMenu1_DropDownOpening(object sender, System.ComponentModel.CancelEventArgs e)
        {
            HostedTextBoxBase hostedTextBox = this.ActiveControl as HostedTextBoxBase;
            if (hostedTextBox != null)
            {
                this._currentControl = hostedTextBox.Parent;

            }
          
            if (this._currentControl!=null)
            {
                this.Text = this._currentControl.Name;
            }
        }

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
ContextMenu TextBox
Asked by
Todd
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or