简体   繁体   English

从禁用的文本框中删除矩形矩形

[英]Removing Focus Rectangle From Disabled Textbox

So the scenario that I have is that there is a Form with three text boxes and a button. 因此,我的情况是有一个带有三个文本框和一个按钮的窗体。 Clicking the button sets textBox1.Enabled = false, textBox2.Enabled = false, and textBox3.Focus(). 单击按钮将设置textBox1.Enabled = false,textBox2.Enabled = false和textBox3.Focus()。

在此处输入图片说明

The problem that I'm running into is that if either textBox1 or textBox2 has focus at the moment the user clicks the button, the text box becomes disabled but retains a greyed-out version of the focus rectangle. 我遇到的问题是,如果在用户单击按钮时textBox1或textBox2都具有焦点,则该文本框将被禁用,但会保留灰色的焦点矩形版本。 It's like the form isn't redrawing the disabled text box. 就像表单没有重新绘制禁用的文本框一样。 Please observe the attached screenshot and notice the difference between the first and second text box. 请观察所附的屏幕截图,并注意第一个和第二个文本框之间的区别。

How do I ensure that I move focus to textBox3 and get rid of the focus rectangle around textBox1? 如何确保将焦点移至textBox3并摆脱掉textBox1周围的焦点矩形?

I'm not sure if this behavior is a bug, but i found way to handle it. 我不确定这种行为是否是一个错误,但我找到了解决方法。 The trick is play around with BorderStyle property. 窍门是使用BorderStyle属性。

private void button1_Click(object sender, EventArgs e)
{
    textBox3.Focus();
    var borderStyle = textBox1.BorderStyle;
    textBox1.BorderStyle = BorderStyle.None;
    textBox2.BorderStyle = BorderStyle.None;
    textBox1.Enabled = false;
    textBox2.Enabled = false;
    textBox1.BorderStyle = borderStyle;
    textBox2.BorderStyle = borderStyle;
    textBox1.Refresh();
}

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

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