简体   繁体   English

如何在winform c#中禁用按钮时将焦点设置在按钮上?

[英]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.当 windows 表单应用程序中的按钮被禁用时,我想在按钮中设置焦点。

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#.您可以尝试通过以下代码添加和删除点击事件,而不是启用和禁用 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) 如何订阅和取消订阅事件(C# 编程指南)

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.使用该属性检查OnClick是否需要触发。

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);
           }
       }
       
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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