0

我想在 vb.net 和 wpf 中对文本框进行身份验证,确保在我将它们的内容提供给数据库之前它不包含空值,我正在考虑使用 for...each 语句,但它在 wpf 中不起作用。这里是我的代码。

for Each txt as Control in window.controls
  if typeof is Textbox Then
    if txt.text = "" Then
      MsgBox("Complete the blank Properties")
      exit sub
    end if
  end if
next

此代码不起作用,我收到错误消息:“窗口是一种类型,不能用作表达式”

我怎样才能做到这一点?

4

4 回答 4

0
 if txt.text = "" Then

改成

 if txt.text is nothing Then

否则,如果您不想使用代码,您可以尝试使用错误提供程序工具。然后写这段代码

If (String.IsNullOrEmpty(textbox1.Text)) Or (String.IsNullOrEmpty(textbox2.Text)) Then
                ErrorProvider1.SetError(textbox1, "This is a require field")
                ErrorProvider1.SetError(textbox2, "This is a require field, fill in with 4 digits")
于 2013-03-18T06:38:36.863 回答
0

您的窗口未命名为窗口。将窗口的变量名而不是通用 Window 放在 foreach 循环中。也许这是您的窗口类的方法?尽管那会很不幸而且不是最好的设计,但您甚至可以省略名称而只写

for each txt as Control in yourwindowsvariablename.Controls

或者

for each txt as Control in Controls
于 2013-03-18T06:45:41.750 回答
0

如果代码与控件的格式相同(.net 窗口),请Me.Controls使用window.Controls. 如果不检查window实际是一个变量。

于 2013-03-18T06:47:20.590 回答
0

嘿伙计们,我终于解决了这个问题,非常感谢您的建议。

这只是一个函数,您调用它来循环遍历网格中的所有文本框并确保它们不为空,您调用该函数并将网格的名称作为参数传递。

干杯!

public function AuthenticateTextBoxes(Byref G as Grid)
For i as int32 = 0 to (G.Children.count - 1)
  if G.Children.Item(i).GetType = GetType(TextBox) Then
     Dim txt as Textbox = CType(G.Children.Item(i),TextBox)
       if string.IsnullOrWhitespace(txt.text) Then
         MsgBox("Fill In the required fields")
       End if
  End if
Next
于 2013-03-25T12:55:48.057 回答