Disable RadButton just before OnClick event

3 posts, 0 answers
  1. Mara
    Mara avatar
    5 posts
    Member since:
    Aug 2012

    Posted 23 Sep 2014 Link to this post

    I need to disable the radbutton just before the click event is fired, and finally resume its enabled state after the click event.
    I tried to create a property like this in my CustomRadButton:
    /// <summary>
            /// On click event
            /// </summary>
            /// <param name="e"></param>
            protected override void OnClick(EventArgs e)
            {
                if (this.multiClickAccepted)
                    base.OnClick(e);
                else
                {
                    //if the multiclick is disabled (as default) disable the button before raise any event
                    this.temporaryEnabled = this.Enabled;
                    base.Enabled = false;
                    base.OnClick(e);
                    base.Enabled = (bool)this.temporaryEnabled;
                    this.temporaryEnabled = null;
                    //after the reactivation look for any binding to restore the correct button state
                    if (this.DataBindings != null)
                    {
                        foreach (System.Windows.Forms.Binding b in this.DataBindings)
                        {
                            if (b.PropertyName.Equals("Enabled"))
                                b.ReadValue();
                        }
                    }
                }
            }

    But in debug I found out that the base.OnClick(e); event is asynchronous.

    Can you suggest something to solve my problem?

    Thanks
  2. Mara
    Mara avatar
    5 posts
    Member since:
    Aug 2012

    Posted 24 Sep 2014 in reply to Mara Link to this post

    I think I solved replacing the base.OnClick(e); with this.ButtonElement.PerformClick();
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Dimitar
    Admin
    Dimitar avatar
    1409 posts

    Posted 25 Sep 2014 Link to this post

    Hi Mara,

    Thank you for writing.

    In this case you are correct, calling the base OnClick method will not fire the Click event. The proper way to achieve this functionality will be to disable the button and call the DoEvents method of the application in order to make sure that the button is painted. Then reset the button state and call the element PerformClick method which will trigger the Click event:
    public class MyButton : RadButton
    {
        protected override void OnClick(EventArgs e)
        {       
            this.Enabled = false;
             
            Application.DoEvents();
            for (int i = 0; i < 1500; i++)
            {
                Console.WriteLine("waiting" + i);//do some work
            }
     
            this.Enabled = true;
     
            this.ButtonElement.PerformClick();
        }
    }

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

    Regards,
    Dimitar
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
Back to Top