为什么我不能使用选项卡离开文本框?
我有这样的代码:
public static void AddDefaultTextFromTag(params TextBox[] textBoxes)
{
foreach (TextBox oTextBox in textBoxes)
{
bool isPasswordChar = oTextBox.UseSystemPasswordChar;
oTextBox.Enter += (sndr, evnt) =>
{
if (((TextBox)sndr).Text == ((TextBox)sndr).Tag.ToString())
{
((TextBox)sndr).Text = "";
((TextBox)sndr).UseSystemPasswordChar = isPasswordChar;
((TextBox)sndr).ForeColor = SystemColors.WindowText;
}
};
oTextBox.Leave += (sndr, evnt) =>
{
if (((TextBox)sndr).Text.Trim().Count() == 0)
{
((TextBox)sndr).UseSystemPasswordChar = false;
((TextBox)sndr).CharacterCasing = CharacterCasing.Normal;
((TextBox)sndr).Text = ((TextBox)sndr).Tag.ToString();
((TextBox)sndr).ForeColor = SystemColors.GrayText;
}
};
if (oTextBox.Text.Trim().Count() == 0)
{
oTextBox.UseSystemPasswordChar = false;
oTextBox.CharacterCasing = CharacterCasing.Normal;
oTextBox.Text = oTextBox.Tag.ToString();
oTextBox.ForeColor = SystemColors.GrayText;
}
}
}
但是当我在此方法的参数中输入的 TextBox.UseSystemPasswordChar
为 true 并且它的 TextBox.Text
属性为空时,TextBox
无法使用键盘上的 Tab
按钮离开,只能使用 MouseClick
来失去该 TextBox
的焦点。
为什么会发生这种情况?
我的代码是C#,框架4,在VS2010 Pro中构建,项目在WinForms中。 我使用 VS 工具箱中的文本框。
请帮忙。提前致谢。
I have this code:
public static void AddDefaultTextFromTag(params TextBox[] textBoxes)
{
foreach (TextBox oTextBox in textBoxes)
{
bool isPasswordChar = oTextBox.UseSystemPasswordChar;
oTextBox.Enter += (sndr, evnt) =>
{
if (((TextBox)sndr).Text == ((TextBox)sndr).Tag.ToString())
{
((TextBox)sndr).Text = "";
((TextBox)sndr).UseSystemPasswordChar = isPasswordChar;
((TextBox)sndr).ForeColor = SystemColors.WindowText;
}
};
oTextBox.Leave += (sndr, evnt) =>
{
if (((TextBox)sndr).Text.Trim().Count() == 0)
{
((TextBox)sndr).UseSystemPasswordChar = false;
((TextBox)sndr).CharacterCasing = CharacterCasing.Normal;
((TextBox)sndr).Text = ((TextBox)sndr).Tag.ToString();
((TextBox)sndr).ForeColor = SystemColors.GrayText;
}
};
if (oTextBox.Text.Trim().Count() == 0)
{
oTextBox.UseSystemPasswordChar = false;
oTextBox.CharacterCasing = CharacterCasing.Normal;
oTextBox.Text = oTextBox.Tag.ToString();
oTextBox.ForeColor = SystemColors.GrayText;
}
}
}
But when the TextBox.UseSystemPasswordChar
I input in this method's parameter is true and it's TextBox.Text
property is empty, the TextBox
can't leave using a Tab
button on the keyboard, only a MouseClick
can be used to lose the focus of that TextBox
.
Why is this happening?
My code is in C#, framework 4, build in VS2010 Pro, project is in WinForms.
I use a TextBox from the VS ToolBox.
Please help. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法离开文本框的原因是您正在更改文本框中的CharacterCasing 属性。
不知道为什么它会这样工作,但它以前发生在我身上,我最终所做的是捕获按键事件,如果它是一个字母,我会将其切换为大写值。这不是最佳的,但它有效
我做了类似的事情(从头开始写它,但它应该有效):
您还应该使用 new 关键字来“覆盖”(我知道这不是正确的术语)字符大小写,所以它不做它自己的事情
代码基本上检查按下的键是否是字母,然后,如果它被标记为大写,并且字符较低,则将其替换为它的大写版本(在位置光标)然后将光标移动到下一部分,反之亦然 (toLower)
注意:
如果用户选择了多个字符(SelectionLenght > 0),此代码可能(应该)会遇到一些麻烦,如果您想保留正常的文本框功能,您应该删除所有选定的字符
The reason you can't leave the textbox is because you are changing the CharacterCasing property in the textbox.
Not sure why it works like this, but it has happened to me before, what I ended up doing was capture the keypress event, and if it was a letter I'd switch it to it's uppercase value. It's not optimal, but it works
I did something similar to this (writing it from the top of my head, but it should work):
You also should use the new keyword to "override" (I know that's not the right term here) the Character casing, so it doesn't do it's own thing
The code basically checks if the pressed key is a letter, then, if it's marked as Upper, and the char is lower, replaces it with it's upper version (in the position of the cursor) then moves the cursor to the next part, and Viceversa (toLower)
NOTE:
This code will have may (should) have some trouble if the user has more than one character selected (SelectionLenght > 0), if you want to keep the normal Textbox functionality, you should delete all the selected characters
因此,我设置了一个 WinForms 应用程序,绘制了两个文本框,将其中一个设置为 UseSystemPasswordChar=true,然后将其设置如下:
您的函数工作正常,并且无论文本框包含什么内容,我都可以在表单上的控件中进行 Tab 切换。 (还添加了一个按钮,该按钮对选项卡测试没有任何作用)所以......除非我的测试设置无效,否则不会重现
So I set up a WinForms app, drew two textboxes, set one to UseSystemPasswordChar=true then set it up like so:
Your function works fine and I have no problems tabbing through the controls on the form no matter what the textboxes contain. (added a button also that does nothing for tabbing test) so... no repro unless my test setup is not valid
我在这个帖子<的答案中发现了什么/a> 是我的解决方案。您可以将PasswordChar 设置为“●”,然后设置为“\0”以获得普通文本,而不是将UseSystemPasswordChar 设置为true,然后设置为false。您不应设置 UseSystemPasswordChar,因为它优先于 PasswordChar。
What I found in the answer of this post was the solution for me. Instead of setting UseSystemPasswordChar to true and then to false, you can set PasswordChar to '●' and then to '\0' to have normal text. You should not set the UseSystemPasswordChar because it has precedence over PasswordChar.