RadButton base.OnClick problem

5 posts, 1 answers
  1. Hans
    Hans avatar
    14 posts
    Member since:
    Nov 2010

    Posted 08 Oct 2014 Link to this post

    When I override Onclick on RadButton, the code first finishes the whole onclick method before firing base.OnClick.
    For a standard Button, this works fine.
    In the example below, the OnClick will first finish completely before the base.OnClick will be executed, but is should not.
    How to fix this ?

    public class QButton : RadButton
    {
    protected override void OnClick(EventArgs e)
    {
    var form = FindForm();
    try
    {
    // set wait cursor 
    form.Cursor = Cursors.WaitCursor;

    // execute base click code
    base.OnClick(e);
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    finally
    {
    // set arrow cursor
    form.Cursor = Cursors.Default;
    }
    }
    }

  2. Answer
    Dimitar
    Admin
    Dimitar avatar
    1415 posts

    Posted 08 Oct 2014 Link to this post

    Hello Hans,

    Thank you for writing.

    The Click event won't be fired when you are calling the base OnClick method because this is actually handled by the button's elements. So to properly fire the event you should call the element's PerformClick method:
    protected override void OnClick(EventArgs e)
    {
        var form = FindForm();
        try
        {
            // set wait cursor
            form.Cursor = Cursors.WaitCursor;
     
            // execute base click code
            this.ButtonElement.PerformClick();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            // set arrow cursor
            form.Cursor = Cursors.Default;
        }
    }

    I hope this helps. Should you have any other questions do not hesitate to ask.

    Regards,
    Dimitar
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Hans
    Hans avatar
    14 posts
    Member since:
    Nov 2010

    Posted 08 Oct 2014 in reply to Dimitar Link to this post

    Yes, perfect.  Thank you.

    Hans
  5. Hans
    Hans avatar
    14 posts
    Member since:
    Nov 2010

    Posted 17 Oct 2014 Link to this post

    Oops.  It turns out that now the click-event-code gets fired twice !!
  6. Dimitar
    Admin
    Dimitar avatar
    1415 posts

    Posted 17 Oct 2014 Link to this post

    Hello Hans,

    Thank you writing back.

    This can be avoided by creating a custom element as well. This will allow you to override its OnClick method and use your logic there:
    public class MyButton : RadButton
    {
        protected override RadButtonElement CreateButtonElement()
        {
            return new MyButtonElement(this);
        }
    }
     
    public class MyButtonElement : RadButtonElement
    {
        RadButton owner;
         
        public MyButtonElement(RadButton owner)
        {
            this.owner = owner;
        }
         
        protected override void OnClick(EventArgs e)
        {
            var form = owner.FindForm();
            try
            {
                // set wait cursor
                form.Cursor = Cursors.WaitCursor;
                 
                // execute base click code
                base.OnClick(e);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                // set arrow cursor
                form.Cursor = Cursors.Default;
            }
        }
         
        protected override Type ThemeEffectiveType
        {
            get
            {
                return typeof(RadButtonElement);
            }
        }
    }

    I hope this helps.

    Regards,
    Dimitar
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top
UI for WinForms is Visual Studio 2017 Ready