如何在TextBox中检查文本 [英] How Can Check Text in TextBox

查看:105
本文介绍了如何在TextBox中检查文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if (TXTSAVE.Text  == "False")
            {

                MessageBox.Show("You Do Not Have Permission");
                return;
            }







此代码未运行

和TXTSAVE有假值




This Code Is Not Running
And TXTSAVE Have "False" Value

推荐答案

这绝对不是检查权限的好方法。



要比较字符串,您可以使用
This is definitely not a good way to check for permissions.

To compare the strings, you can make use of
string.Compare(string1,string2,StringComparison.OrdinalIgnoreCase)


文本框是自由格式的用户输入字段 - 因此用户可以输入不符合预期的内容。我们作为人类,有时会看到我们所期望的,而不是真正存在的东西。所以...

1.也许有领先/尾随空格

2.也许你看到'假'实际上文字是'假的'(F vs f)



请尝试以下方法:



Text boxes are free-form user entry fields - so it is possible for a user to enter something not quite as you expected. And we, as humans, sometimes see what we expected, not what is really there. So...
1. Perhaps there are leading / trailing spaces
2. Perhaps you are seeing 'False' when in reality the text is 'false' (F vs f)

Try the following:

if (TXTSAVE.Text.Trim().Equals("False", StringComparison.InvariantCultureIgnoreCase))
    { 
    MessageBox.Show("You Do Not Have Permission");
    return;
    }


这里的问题是你使用了错误的Control来完成这项任务。



1.如果你想向用户显示文本通知,他们无法与之交互:使用标签。



2.使用TextBox当用户编辑内容表示时。



3.尽快执行验证并保留您不想要的控件用户与禁用进行交互,直到使用这些控件是合法的。为什么让用户首先犯了一个愚蠢的错误?



4.考虑使用像StatusBar这样的UI元素让用户了解当前的状态UI因为它影响了他们可能的选择
The problem here is you are using the wrong Control for this task.

1. if you want to show a text notice to the user, that they can't interact with: use a Label.

2. use a TextBox when having the user edit the contents means something.

3. perform validation as soon as possible and keep Controls that you do not want the user to interact with disabled until using those Controls is "legal." Why let the user make a stupid mistake in the first place ?

4. consider using a UI element like a StatusBar to keep the user informed about the current state of the UI as it effects their possible choices.


这篇关于如何在TextBox中检查文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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