Assigning ContextMenu to RadButtonElement

8 posts, 1 answers
  1. Jaroslav Bucek
    Jaroslav Bucek avatar
    9 posts
    Member since:
    Jul 2008

    Posted 18 Aug 2008 Link to this post

    Hi,

    I am using RadPanelBar with RadButtonElement controls on it. I whould like to assign a context menu to every single RadButtonElement but I have some problems regarding this.

    1. RadButtonElement dos not contain ContextMenu property, so I cannot assign the menu to it.

    2. When I right click on RadButtonElement, it fires a click event, what I do not want. Is it possible to disable such a behavior?

    TIA
    Jaroslav
  2. Jack
    Admin
    Jack avatar
    2333 posts

    Posted 18 Aug 2008 Link to this post

    Hi Jaroslav Bucek,

    Thank you for this question.

    In this case, you could use a descendant class of RadButtonElement that processes the OnMouseDown event. Consider the following code snippet:

    public class MyButton : RadToggleButtonElement 
        protected override void OnMouseDown(MouseEventArgs e) 
        { 
            if (e.Button == MouseButtons.Right) 
            { 
                RadContextMenu menu = new RadContextMenu(); 
                menu.Items.Add(new RadMenuItem("Item 1")); 
                menu.Items.Add(new RadMenuItem("Item 2")); 
                menu.Items.Add(new RadMenuItem("Item 3")); 
                menu.Show(Control.MousePosition); 
            } 
            else 
            { 
                base.OnMouseDown(e); 
            }            
        }    
      
        protected override Type ThemeEffectiveType 
        { 
            get { return typeof(RadToggleButtonElement); } 
        } 

    I hope this helps. Do not hesitate to contact us if you have further questions.

    Kind regards,
    Jack
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Jaroslav Bucek
    Jaroslav Bucek avatar
    9 posts
    Member since:
    Jul 2008

    Posted 19 Aug 2008 Link to this post

    Interesting idea. I would prefer not to create as many context menus as buttons. When I will create about 50 buttons, it seems very inefficient to me to create 50 context menus, moreover when all should has same items.

    Second problem I see is, that I should create menu items inside of button descendant class. I could create some kind of event (OnContextMenuBind or whatever) but it doesn't look clear to me.

    The question is: could you begin support context menu at least with RadButtonElement?

    The second question becomes from your example: How can I determine the button that was the source control of context menu pop up? (e.g. class ContextMenuStrip has property SourceControl). I could put the button into Tag property, but is it really the best and the one way to do it?

    Thanks in advance,
    Jaroslav Bucek
  5. Answer
    Jack
    Admin
    Jack avatar
    2333 posts

    Posted 20 Aug 2008 Link to this post

    Hi Jaroslav Bucek,

    Thank you for getting back to me.

    You could add a property of type RadContextMenu in the descendant class and set all buttons to point to one context menu.

    As an alternative solution, you could process the OnMouseDown method of the RadPanelBar and check the coordinates to find the correct button. This can be done by using the GetElementAtPoint method. Consider the code below:

    RadElement element = mycontrol.ElementTree.GetElementAtPoint(e.Location); 
    if (element is RadToggleButtonElement) 
     


    We will consider adding support for context menus in RadButtonElement in one of our upcoming releases.

    Currently there is no SourceControl property in RadContextMenu. This is a good feature and I will log it in our feature requests. I agree, that using the Tag property is not the best way but it is a working option.

    I hope this helps. Do not hesitate to write me if you have other questions.

    Best wishes,
    Jack
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  6. Jaroslav Bucek
    Jaroslav Bucek avatar
    9 posts
    Member since:
    Jul 2008

    Posted 20 Aug 2008 Link to this post

    Hi Jack,

    thank you for your time. I did it as you mentioned already ;-)

    Anyhow, I recommend you - Telerik - to consider if it is good idea to fire a Click event with right mouse button. Such a behavior is unexpected in classic controls. Or you should change the delegate of the Click event and pass MouseEventArgs instead EventArgs parameter.

    Regards,
    Jaroslav
  7. Jack
    Admin
    Jack avatar
    2333 posts

    Posted 21 Aug 2008 Link to this post

    Hi Jaroslav,

    I am glad to see you have resolved this issue. We appreciate your feedback and will consider these suggestions for implementation in our upcoming releases.

    Please let me know if you have further questions.

    Greetings,
    Jack
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  8. Mohammed
    Mohammed avatar
    1 posts
    Member since:
    Feb 2009

    Posted 12 Apr 2009 Link to this post

    sorry for disturbing in ur thread .........i want a solution for this problem its urgent.....


    RadButtonElement

    carouselItem = new RadButtonElement();

    .....
    ....
    .....


     

     

    this.radCarousel2.Items.Add(carouselItem);


    HOW TO WRITE CLICK EVENT in this carouselitem that has been generated programatically......

    thx and regards,
    Muzzammil

     

  9. Deyan
    Admin
    Deyan avatar
    2041 posts

    Posted 14 Apr 2009 Link to this post

    Hi Mohammed,

    I am not quite sure that I correctly understand your goal.

    However, if you want to provide a handler for the Click event of the RadButtonElement object, you could use the following code snippet:

    RadButtonElement element = new RadButtonElement(); 
     
    element.Click += new EventHandler(OnRadButtonElement_Click); 
     
     
    ... 
    ... 
     
    private void OnRadButtonElement_Click(object sender, EventArgs args) 
     
     
    I hope this will help.

    Greetings,
    Deyan
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
Back to Top
UI for WinForms is Visual Studio 2017 Ready