有关文本框内标签的问题 [英] A question about labels inside textboxes

查看:59
本文介绍了有关文本框内标签的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的登录表单中有2个文本框,显然是用户名和密码.

现在,我不想在文本框旁边放置名为 User Name Password 的标签,而是将其放置在内部,类似于CP中的搜索"文本框.

所以我要做的是为GotFocus和LostFocus事件编写代码.
这适用于UserName,但是密码有问题.

我尝试在代码上设置密码文本框的UseSystemPasswordChar属性,但是得到了Error creating window handle.
看来您只能在设计时设置该属性.

我的问题是,你们知道其他方法吗?感谢您的帮助.

更新:

下面是代码.看来这里的问题是当我放入txtPassword.Text = "";时.当我删除此行时没有错误,但密码没有从文本框中删除.

I have 2 textboxes in my login form, obviously for the user name and password.

Now instead of putting a label called User Name and Password beside the textboxes, I want to put it inside, similar to the Search textbox in CP.

So what I did was to write the code for the GotFocus and LostFocus events.
This worked on the UserName, however I am having problem with the Password.

I have tried setting the UseSystemPasswordChar property of the password textbox on the code but I got a Error creating window handle.
It seems you can only set that property on the design time.

My question is, do you guys know of any other way of doing this? Thanks for the help.

UPDATE:

Below is the code. It seems that the issue here is when I put txtPassword.Text = "";. It doesn''t have an error when I remove this line but the Password is not removed from the textbox.

private void txtPassword_GotFocus(object sender, EventArgs e)
        {
            if (txtPassword.Text.Trim().ToUpper() == "PASSWORD")
            {
                txtPassword.ForeColor = Color.Black;
                txtPassword.Text = "";
                txtPassword.UseSystemPasswordChar = true;
            }
        }

推荐答案

使用蒙版属性 [
Use MaskedTextBox[^].
Set its Mask Property[^] to A when not on focus to show alphanumeric and change it into * or -whatever character you likes- when it got focus.

Hope this helps


我认为您在这里尝试做的非常非常错误的事情与UseSystemPasswordChar无关:
I think you are trying to do something very, very wrong here, that has nothing to do with UseSystemPasswordChar :
private void butShowPWChars_Click(object sender, EventArgs e)
    {
    tbPassword.UseSystemPasswordChar = false;
    }
private void butHidePWChars_Click(object sender, EventArgs e)
    {
    tbPassword.UseSystemPasswordChar = true;
    }

完全按我的预期工作:隐藏,文本显示为斑点,如图所示,它再次可读.
如果我键入"Hello",然后隐藏数据,则会得到6个Blob.如果我再添加那里".我看到十二个斑点.
再次显示该文本,然后看到"Hello There.".
我没有时间遇到任何问题.

发布您尝试使用的代码片段:我认为您的问题可能更根本.


[edit]已更新为回答Musefan-OriginalGriff [/edit]

也许是在控件具有焦点时更改时引起的错误?这听起来像是OP正在尝试的操作.我看到您的按钮单击将成为焦点"

我也尝试过:

Works exactly as I expect: Hidden, text shows as blobs, shown, it is readable again.
If I type "Hello " then hide the data, I get 6 blobs. If I then add "There." I see twelve blobs.
Show the text again, and I see "Hello There.".
At no time to I get a handle problem.

Post the code fragment you are trying to use to do this: I think your problem may be more fundamental.


[edit]Updated to answer Musefan - OriginalGriff[/edit]

"Perhaps the error is caused when changing while the control has focus? Which sounds like is what the OP is trying. I see your button clicks will take the focus"

I tried this as well:

private void tbPassword_Enter(object sender, EventArgs e)
    {
    tbPassword.UseSystemPasswordChar = false;
    }
private void tbPassword_Leave(object sender, EventArgs e)
    {
    tbPassword.UseSystemPasswordChar = true;
    }

仍然没有处理问题.


一个有趣的问题.我以前没有做过,但是想到的一件事情是创建第三个标准文本框,该文本框与密码框的位置相同

可以说...

TextBoxUsername
TextBoxPassword
TextBoxPlainPassword

最初,您从显示TextBoxPlainPassword和隐藏TextBoxPassword开始.然后,将GotFocus事件添加到TextBoxPlainPassword,将LostFocus事件添加到TextBoxPassword.

现在,在您的GotFocus事件中,请执行以下操作...

1.隐藏TextBoxPlainPassword
2.显示TextBoxPassword
3.将焦点设置为TextBoxPassword

...并在LostFocus事件中执行...

1.检测TextBoxPassword是否为空
2.如果为空,则:
-隐藏TextBoxPassword
-显示TextBoxPlainPassword
3.其他:
-什么都不做


我知道这可能有点破解,但是如果您找不到更好的方法,这听起来应该可以解决
An interesting question. I have not done before but one thing that comes to mind would be to create a third standard text box which is in the same position as the password box

Lets say...

TextBoxUsername
TextBoxPassword
TextBoxPlainPassword

Initially, you start with TextBoxPlainPassword visible and TextBoxPassword hidden. Then you add a GotFocus event to TextBoxPlainPassword and a LostFocus event to TextBoxPassword.

Now, in your GotFocus event you do the following...

1. Hide TextBoxPlainPassword
2. Show TextBoxPassword
3. Set Focus to TextBoxPassword

...and on the LostFocus event you do...

1. Detect if TextBoxPassword is empty
2. if Empty then:
- Hide TextBoxPassword
- Show TextBoxPlainPassword
3. Else:
- Do nothing


I know it may be a bit of a hack but it sounds like it should work if you don''t find a better way


这篇关于有关文本框内标签的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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