简体   繁体   中英

How to set focus on button while button is disabled in winform c#?

I want to set the focus in the button when a button is disabled in the windows form application.

Is there any way to achieve this goal?

Background:

I am making an application that can be used by a blind person so when the button is enabled then on focus screen reader will read of button content but when a button is disabled then not possible to read by screen reader because of the button can not be focused on disabled mode

You can try to add and remove the event of click by below code rather than enable and disable the button in C#.

For remove click event

button.Click -= button_Click;

For add click event

button.Click += button_Click;

For reference, you can refer to the below link.

How to subscribe to and unsubscribe from events (C# Programming Guide)

You can create a custom control and create one propriety into a custom control. using that property to check OnClick need to fire or not.

For Example:

   public class buttonReadonly: Button
   {
       private bool _isReadOnly = false;

       public bool isReadOnly
       {
           get
           {
               return _isReadOnly;
           }
           set
           {
               _isReadOnly = value;
           }
       }

       protected override void OnClick(EventArgs e)
       {
           if (!isReadOnly)
           {
               base.OnClick(e);
           }
       }
       
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM