• <i id='C6mg0'><tr id='C6mg0'><dt id='C6mg0'><q id='C6mg0'><span id='C6mg0'><b id='C6mg0'><form id='C6mg0'><ins id='C6mg0'></ins><ul id='C6mg0'></ul><sub id='C6mg0'></sub></form><legend id='C6mg0'></legend><bdo id='C6mg0'><pre id='C6mg0'><center id='C6mg0'></center></pre></bdo></b><th id='C6mg0'></th></span></q></dt></tr></i><div id='C6mg0'><tfoot id='C6mg0'></tfoot><dl id='C6mg0'><fieldset id='C6mg0'></fieldset></dl></div>

    <legend id='C6mg0'><style id='C6mg0'><dir id='C6mg0'><q id='C6mg0'></q></dir></style></legend>
    • <bdo id='C6mg0'></bdo><ul id='C6mg0'></ul>

    <small id='C6mg0'></small><noframes id='C6mg0'>

    1. <tfoot id='C6mg0'></tfoot>

        与其他禁用的TextBox(es)相比,c#TextBox边框在被禁用时会发生变化

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

            <tfoot id='lAlB9'></tfoot>

              <tbody id='lAlB9'></tbody>

            • <legend id='lAlB9'><style id='lAlB9'><dir id='lAlB9'><q id='lAlB9'></q></dir></style></legend>

              • <bdo id='lAlB9'></bdo><ul id='lAlB9'></ul>

                <small id='lAlB9'></small><noframes id='lAlB9'>

                <i id='lAlB9'><tr id='lAlB9'><dt id='lAlB9'><q id='lAlB9'><span id='lAlB9'><b id='lAlB9'><form id='lAlB9'><ins id='lAlB9'></ins><ul id='lAlB9'></ul><sub id='lAlB9'></sub></form><legend id='lAlB9'></legend><bdo id='lAlB9'><pre id='lAlB9'><center id='lAlB9'></center></pre></bdo></b><th id='lAlB9'></th></span></q></dt></tr></i><div id='lAlB9'><tfoot id='lAlB9'></tfoot><dl id='lAlB9'><fieldset id='lAlB9'></fieldset></dl></div>

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

                  问题描述

                  我有一个非常奇怪的问题.我有多个用于用户名/密码的文本框,以及每个用户/密码组旁边的复选框.当用户单击 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 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本身上尝试了各种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边框在被禁用时会发生变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to keep the Text of a Read only TextBox after PostBack()?(PostBack()之后如何保留只读文本框的文本?)
                  Winforms Textbox - Using Ctrl-Backspace to Delete Whole Word(Winforms 文本框 - 使用 Ctrl-Backspace 删除整个单词)
                  C# - Add button click events using code(C# - 使用代码添加按钮单击事件)
                  Multi-color TextBox C#(多色文本框 C#)
                  How can i set the caret position to a specific index in passwordbox in WPF(如何将插入符号位置设置为 WPF 密码框中的特定索引)
                  C# Numeric Only TextBox Control(C# 纯数字文本框控件)

                      <bdo id='oNkfC'></bdo><ul id='oNkfC'></ul>
                          <tbody id='oNkfC'></tbody>
                      • <tfoot id='oNkfC'></tfoot>

                      • <legend id='oNkfC'><style id='oNkfC'><dir id='oNkfC'><q id='oNkfC'></q></dir></style></legend>

                          <small id='oNkfC'></small><noframes id='oNkfC'>

                          1. <i id='oNkfC'><tr id='oNkfC'><dt id='oNkfC'><q id='oNkfC'><span id='oNkfC'><b id='oNkfC'><form id='oNkfC'><ins id='oNkfC'></ins><ul id='oNkfC'></ul><sub id='oNkfC'></sub></form><legend id='oNkfC'></legend><bdo id='oNkfC'><pre id='oNkfC'><center id='oNkfC'></center></pre></bdo></b><th id='oNkfC'></th></span></q></dt></tr></i><div id='oNkfC'><tfoot id='oNkfC'></tfoot><dl id='oNkfC'><fieldset id='oNkfC'></fieldset></dl></div>