简体   繁体   English

按Tab键并单击按钮时按钮周围的恼人边框

[英]Annoying Border around button when pressing tab and clicking a button

I get this very annoying border when pressing tab and the clicking the button 按Tab键并单击按钮时,我会看到这个非常烦人的边框

I've tried 我试过了

 foreach (Control x in this.Controls)
 {
   if (x is Button)
            {
                Button newbut = (Button)x;
                newbut.FlatStyle = FlatStyle.Flat;
                newbut.FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 255);
                newbut.FlatAppearance.BorderSize = 0;
                newbut.TabStop = false;
            }
        }

And I've also tried doing adding an event on Keydown and not allowing to press the tab key but nothing has worked so far. 而且我也试过在Keydown上添加一个事件而不允许按Tab键但到目前为止没有任何工作。

在此输入图像描述

this is example of the border that keeps popping up 这是不断弹出边框的例子

通过生成自己的按钮类和编辑designer.cs文件来修复

If you do not want custom buttons, you can shift focus to another control once finished, this prevents the rectangle from showing. 如果您不想要自定义按钮,则可以在完成后将焦点移动到另一个控件,这样可以防止显示矩形。 You could place a dummy control out of view and shift fucus to this. 您可以将虚拟控件放在视图之外并将fucus移动到此处。

    private void btnDoSomething_Click(object sender, EventArgs e)
    {
        myDummyControl.Focus();

        MessageBox.Show("This still executes even though we have lost focus");

        // If you are decoupling your code from the UI, this becomes second nature quickly as it becomes part of your control handling.
    }

Also you can stop the control from getting Tab focus my setting it's TabStop property tp false, do this in the buttons Paint event. 此外,您可以停止控件获取Tab焦点我的设置它的TabStop属性tp false,在按钮Paint事件中执行此操作。

If you have many buttons, you can just point them to the same event handler (On_Paint). 如果你有很多按钮,你可以将它们指向同一个事件处理程序(On_Paint)。

    private void On_Paint(object sender, PaintEventArgs e)
    {
        Button thisButton = sender as Button;
        thisButton.TabStop = false;
    }

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

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