问题描述
我有一个非常奇怪的问题.我有多个用于用户名/密码的文本框,以及每个用户/密码组旁边的复选框.当用户单击 CheckBox 时,如果他选中它,它旁边的用户名和密码 TextBox 将变为启用状态,并且焦点设置为用户名 TextBox.如果用户取消选中 CheckBox,它旁边的 TextBoxes 将被禁用.但是,一个 TextBox 的边框与其他禁用的 TextBox 不同.
I have a really strange problem.I have multiple TextBoxes for username/passwords, and a CheckBox beside every user/pass group. When user clicks on the CheckBox, and if he checked it, the username and password TextBox beside it become enabled and focus is set to username TextBox. If user unchecked the CheckBox the TextBoxes beside it become disabled. However the border of one TextBox stays different than other disabled TextBoxes.
见:
我认为这是焦点问题,所以我更改了代码 - 当用户取消选中 CheckBox 时,它首先关注表单上的其他一些元素,然后禁用它,但它仍然做同样的事情.关于什么可能导致问题的任何想法?
I've thought it was a focus problem, so I've changed the code - when user unchecks the CheckBox it first focuses on some other element on the form and then disables it, but it still does the same thing.Any ideas on what could cause the problem?
推荐答案
据我所知,这是系统呈现控件禁用状态的方式中的一个错误.我创建了以下代码来模拟此问题.代码有点冗长,但我这样做是为了便于理解逻辑流程.
So far as I can tell, this is a bug in the way the system is rendering the control's disabled state. I've created the following code to simulate this issue. The code is a bit verbose but I made it so in order easily understand the logic flow.
我创建了一个表单,其中包含:4 个名为 txtBox1、txtBox2、txtBox3 和 txtBox4 的文本框4 个复选框,分别命名为 chkBox1、chkBox2、chkBox3 和 chkBox4
I created a form, with :4 textboxes named txtBox1, txtBox2, txtBox3 and txtBox44 checkboxes named chkBox1, chkBox2, chkBox3 and chkBox4
将每个文本框的 Enabled 属性设置为 False(我在设计时这样做了)
Set the Enabled property of each textbox to False (I did this at design time)
private void Form1_Load(object sender, EventArgs e) {
chkBox1.CheckedChanged += chkBox_CheckedChanged;
chkBox2.CheckedChanged += chkBox_CheckedChanged;
chkBox3.CheckedChanged += chkBox_CheckedChanged;
chkBox4.CheckedChanged += chkBox_CheckedChanged;
}
private void chkBox_CheckedChanged(object sender, EventArgs e) {
var chkBox = ((CheckBox)sender);
var controlSet = chkBox.Name.Substring(6,1);
var txtName = "txtBox" + controlSet;
foreach (var txtBox in Controls.Cast<object>().Where(ctl => ((Control)ctl).Name == txtName).Select(ctl => ((TextBox)ctl))) {
if (chkBox.Checked) {
txtBox.Enabled = true;
txtBox.Focus();
}
else {
//The checkbox stole the focuse when it was clicked, so no need to change.
txtBox.Enabled = false;
}
}
}
现在,如果您执行此代码,您可以选中复选框以启用具有相同名称前缀(1、2、3 或 4)的文本框.这也将焦点设置到文本框.现在,如果您禁用具有焦点的文本框,它的显示方式将不同于其他禁用的文本框.
Now if you execute this code, you can check the checkboxes to enable the textbox with the same name prefix (1, 2, 3 or 4). This also sets the focus to the Textbox. Now, if you disable a Textbox that has the focus, it shows up differently than other disabled Textboxes.
我在控件和Form本身上尝试了各种Refresh、Invalidate等,无济于事.
I tried all kinds of Refresh, Invalidate, etc.. on the controls and Form itself to no avail.
更新
所以我发现了一个似乎有效的 hack.如果在禁用之前将文本框的边框样式设置为无",然后再将其重置,则不会发生奇怪的轮廓效果.
So I found a hack that seems to work. If you set the borderstyle of the textbox to 'None' before disabling, and then reset it afterwards, the odd outline effect doesn't happen.
var borderStyle = txtBox.BorderStyle;
txtBox.BorderStyle = BorderStyle.None;
txtBox.Enabled = false;
txtBox.BorderStyle = borderStyle;
这篇关于与其他禁用的TextBox(es)相比,c#TextBox边框在被禁用时会发生变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!