与其他禁用的 TextBox 相比,c# 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.
See:
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,这是系统呈现控件禁用状态的方式中的一个错误。我创建了以下代码来模拟此问题。代码有点冗长,但我这样做是为了轻松理解逻辑流程。
我创建了一个表单,其中:
4 个文本框,名为 txtBox1、txtBox2、txtBox3 和 txtBox4
4 个名为 chkBox1、chkBox2、chkBox3 和 chkBox4 的复选框
将每个文本框的 Enabled 属性设置为 False(我在设计时这样做)
现在,如果执行此代码,您可以选中复选框以启用具有相同名称前缀的文本框 (1 、2、3 或 4)。这还将焦点设置到文本框。现在,如果禁用具有焦点的文本框,它的显示方式将与其他禁用的文本框不同。
我在控件和表单本身上尝试了各种刷新、无效等,但无济于事。
更新
所以我发现了一个似乎有效的技巧。如果您在禁用之前将文本框的边框样式设置为“无”,然后在禁用之后重置它,则不会发生奇怪的轮廓效果。
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.
I created a form, with :
4 textboxes named txtBox1, txtBox2, txtBox3 and txtBox4
4 checkboxes named chkBox1, chkBox2, chkBox3 and chkBox4
Set the Enabled property of each textbox to False (I did this at design time)
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.
I tried all kinds of Refresh, Invalidate, etc.. on the controls and Form itself to no avail.
UPDATE
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.