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

Disable RadButton just before OnClick event

2 Answers 305 Views
Buttons, RadioButton, CheckBox, etc
This is a migrated thread and some comments may be shown as answers.
Ufficio Acquisti
Top achievements
Rank 1
Ufficio Acquisti asked on 23 Sep 2014, 01:52 PM
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 Answers, 1 is accepted

Sort by
0
Ufficio Acquisti
Top achievements
Rank 1
answered on 24 Sep 2014, 07:58 AM
I think I solved replacing the base.OnClick(e); with this.ButtonElement.PerformClick();
0
Dimitar
Telerik team
answered on 25 Sep 2014, 12:54 PM
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.
 
Tags
Buttons, RadioButton, CheckBox, etc
Asked by
Ufficio Acquisti
Top achievements
Rank 1
Answers by
Ufficio Acquisti
Top achievements
Rank 1
Dimitar
Telerik team
Share this question
or