与其他禁用的TextBox相比,禁用C#TextBox边框时会发生变化 [英] c# TextBox border changes when it gets disabled compared to other disabled TextBox(es)

查看:76
本文介绍了与其他禁用的TextBox相比,禁用C#TextBox边框时会发生变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常奇怪的问题. 我有多个用于用户名/密码的TextBoxes,以及每个用户/密码组旁边的CheckBox.当用户单击CheckBox并选中该复选框时,其旁边的用户名和密码TextBox将变为启用状态,并将焦点设置为用户名TextBox.如果用户取消选中复选框,则旁边的文本框将被禁用.但是,一个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 txtBox4 4 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本身进行各种刷新,无效等操作.

I tried all kinds of Refresh, Invalidate, etc.. on the controls and Form itself to no avail.

更新

因此,我发现了一个似乎有效的黑客工具.如果在禁用之前将文本框的边框样式设置为无",然后再将其重置,则不会出现奇数轮廓效果.

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相比,禁用C#TextBox边框时会发生变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆