检查所有文本框以确保不为空 [英] Check all textbox to make sure not empty

查看:85
本文介绍了检查所有文本框以确保不为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我遇到的一种情况是,在表单1中,我有10个文本框,在表单2中,我有15个文本框,在尝试保存之前必须填写.

如果我使用"If txt1.text<> string.empty OR txt2.text<> string.empty then",则我的检查语句必须相当长..

那么,这里有人教我检查表单中所有文本框的最简单方法吗?我想一定有一种方法可以循环检查system.windows.textbox,但我不知道该怎么做..

请给我看一下..tq

Hi all,

I have a situation here where in form 1, I have 10 textbox and in form 2, I have 15 textbox that must be filled before try to save.

If I use the "If txt1.text<>string.empty OR txt2.text<>string.empty then", my checking statement must be quite long..

So does anyone here teach me the simplest way to check all textbox in a form? I guess there must be a way to check by loop the system.windows.textbox but I don''t have any idea how..

Kindly please show me..tq

推荐答案

首先,如果您有10个文本框,那么避免使用Designer添加它们已经是一个很好的理由.设计人员通常意味着无聊的无用的手工工作,这些工作可以用代码更快地完成,质量也更高,尤其是在维护方面.如果这样做,您的问题将减少为:
First of all, if you have 10 text boxes, it''s already a good reason to avoid adding them using the Designer. Designer often means boring unproductive manual work which could be done in code much faster with much better quality, especially in terms of maintenance. If you do that, you problem would be reduced to:
TextBox[] textBoxes = new TextBox[textBoxCount];
// create, arrange them in its parent control using parent.Controls.Add(/* ... */);

//... and then use this repeatedly:

foreach (TextBox textBox in textBoxes) { /* ... */ }



即使文本框中没有数组或其他集合,也可以使用更多的聪明"和复杂的方法:您可以基于Control.Controls创建一个循环,迭代一些顶部控件(例如,Form )通过父级,并使用以下内容标识文本框:



There is more "clever" and complex way which would work even if you don''t have an array or other collections with your text boxes: you can create a loop based on Control.Controls, iterate some top control (Form, for example) through parent, and identify text boxes using:

void ProcessTextBoxes(Control parent) {
   foreach(Control child in parent.Controls) {
      TextBox textBox = child as TextBox;
      if (textBox != null) // type match
          ProcessTextBox(textBox); //define some method to work with individual text box
      else
          ProcessTextBoxes(parent); //recursive
   } //loop
}



您可以完成所有操作,但是为什么呢?如果不使用Designer创建它们,维护起来会容易得多,而且更好.

最后,我并不是根本不需要检查文本框是否为空文本.也许您需要处理事件TextBox.TextChanged以检测用户曾经更改过文本.这就是启用保存"之类的命令的方式.另外,您可以检查属性TextBox.Modified.这些方法可以帮助您处理用户输入有效的空文本时的情况.请参阅:
http://msdn.microsoft.com/en-us/library/system.windows.forms.textboxbase.modified.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.textchanged.aspx [ ^ ].

即使您确实需要检查文本值,在更一般的情况下,您也需要验证这些值;验证可能比检查空度要复杂得多. (顺便说一句,有可能,您需要在此检查之前使用string.Trim.验证是您应该熟悉的常规整体概念:
http://msdn.microsoft.com/en-us/library/ms229603.aspx [ ^ ].

祝你好运,

-SA



You can do it all, but why? Not creating them with Designer is much easier and better for maintenance.

Finally, I am not that you need to check the text boxes for empty text at all. Maybe you need to handle the event TextBox.TextChanged to detect that the user ever changed the text. This is how some command like "Save" gets enabled. Also, you can check up the property TextBox.Modified. These methods can help you to handle the situation when the user entered the valid empty text. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.textboxbase.modified.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.textchanged.aspx[^].

Even if you really need to check up the text values, in more general cases you need to validate the values; an validation can be more complex then a check for emptiness. (By the way, chances are, you need to use string.Trim before this check. The validation is the regular holistic concept you should better get familiar with:
http://msdn.microsoft.com/en-us/library/ms229603.aspx[^].

Good luck,

—SA


Dim EmptyTextBoxFound As Boolean = False 'Boolean flag for empty textbox 
Dim EmptyTextBoxName As String = ""
For Each ctl As Control in Me.Controls

If TypeOf ctl Is TextBox And ctl.Text.Length = 0 Then 
EmptyTextBoxName = ctl.Name
EmptyTextBoxFound = True
Exit For 
End For 
If EmptyTextBoxFound = True  Then 
'.. do whatever you have do
 End If 


这篇关于检查所有文本框以确保不为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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