简体   繁体   English

与其他禁用的TextBox相比,禁用C#TextBox边框时会发生变化

[英]c# TextBox border changes when it gets disabled compared to other disabled TextBox(es)

I have a really strange problem. 我有一个很奇怪的问题。 I have multiple TextBoxes for username/passwords, and a CheckBox beside every user/pass group. 我有多个用于用户名/密码的TextBoxes,以及每个用户/密码组旁边的CheckBox。 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. 当用户单击CheckBox并选中该复选框时,旁边的用户名和密码TextBox将启用,并且焦点将设置为用户名TextBox。 If user unchecked the CheckBox the TextBoxes beside it become disabled. 如果用户取消选中复选框,则旁边的文本框将被禁用。 However the border of one TextBox stays different than other disabled TextBoxes. 但是,一个TextBox的边框与其他禁用的TextBox保持不同。

See: 看到:

http://img545.imageshack.us/img545/1944/textbox.png

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. 我以为这是一个焦点问题,所以我更改了代码-当用户取消选中CheckBox时,它首先将焦点放在表单上的其他元素上,然后将其禁用,但是它仍然做同样的事情。 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. 代码有点冗长,但我这样做是为了轻松理解逻辑流程。

I created a form, with : 4 textboxes named txtBox1, txtBox2, txtBox3 and txtBox4 4 checkboxes named chkBox1, chkBox2, chkBox3 and chkBox4 我创建了一个表单,其中包含:名为txtBox1,txtBox2,txtBox3和txtBox4的4个文本框名为chkBox1,chkBox2,chkBox3和chkBox4的4个复选框

Set the Enabled property of each textbox to False (I did this at design time) 将每个文本框的Enabled属性设置为False(我在设计时就这样做了)

    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;
            }
        }
    }

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). 现在,如果执行此代码,则可以选中复选框以启用具有相同名称前缀(1、2、3或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. 我尝试了各种刷新,Invalidate等。在控件和Form本身上都无济于事。

UPDATE UPDATE

So I found a hack that seems to work. 因此,我发现了一个似乎可行的hack。 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;

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

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