c#中如何避免Button,CheckBox等的点击状态时的焦点矩形框问题:

方法一:

    使用label控件替代;

方法二:

    转移焦点:

    利用一个label控件,将其BackColor设为透明,比如checkbox,在其enter事件中转移焦点即可,代码如下:

   

private void control_lose_focus()
        {
            lblHide.Focus();
        }

private void chkAutoLogin_Enter(object sender, EventArgs e)
        {
            control_lose_focus(); //在enter的情况下转移焦点最佳
        }

方法三:重绘控件:如button,代码如下:

1,第一种验证过:在重绘控件的构造函数中:

SetStyle(ControlStyles.Selectable, false);
2,没验证,但是也可以:

public class MyButton : System.Windows.Forms.Button
    {
        public bool Selectable
        {
            get { return this.GetStyle(System.Windows.Forms.ControlStyles.FixedHeight); }
            set { this.SetStyle(System.Windows.Forms.ControlStyles.Selectable, value); }
        }
    }   
使用时:
button1.Selectable=false;  //设置不让捕获焦点;
button1.Selectable=true;   //设置可以捕获焦点,就和.net的Button完全一样。
方法四:最绝:

private void SetButton(Button button)  
        {  
            MethodInfo methodinfo = button.GetType().GetMethod("SetStyle",BindingFlags.NonPublic 
                | BindingFlags.Instance | BindingFlags.InvokeMethod);  
            methodinfo.Invoke(button,BindingFlags.NonPublic 
                | BindingFlags.Instance | BindingFlags.InvokeMethod,null,new object[] 
                {ControlStyles.Selectable,false},Application.CurrentCulture);  
        }  

点击后不得到焦点且不影响当前焦点所在位置,类似系统计算器中按钮的效果。

可以实现无焦点的按钮、复选框、单选框等控件。





 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值