2014-03-03 23 views
0

我正在致力於一個簡單的大學項目,該項目必須註冊一個新成員。現在我忙於表單驗證。VB.Net循環遍歷文本框以檢查它們是否僅包含數字

我的一些文本框只能包含文本和不信,我用它來檢查代碼,如果必須只接受文字文本框如下:

'check textboxes contains only characters' 
     'initial' 
     If Not System.Text.RegularExpressions.Regex.Match(vinital, "^[a-z]*$", System.Text.RegularExpressions.RegexOptions.IgnoreCase).Success Then 
      MsgBox("Initial can only contain text .") 
     End If 

我的問題

相反做一個如果一次爲每個文本框表述,我怎樣才能使用上面的類似代碼來循環它們來驗證文本只。

回答

1

你可以嘗試把所有的控件在Panel,然後遍歷它像這樣(未經測試,但你的想法):

Dim ctrl As Control 
For Each ctrl In Panel1.Controls 'Me.Controls should also work, I think. 
    If (ctrl.GetType() Is GetType(TextBox)) Then 
     Dim txt As TextBox = CType(ctrl, TextBox) 
     'check content of txt here 
    End If 
Next 
+0

Thanxs了它與幾個mondifications – Marilee

+0

@Marilee你的工作很受歡迎。 – seph

相關問題