Last active
December 14, 2015 13:49
-
-
Save akira345/5096451 to your computer and use it in GitHub Desktop.
VB.Netで複数のテキストボックスに対し、Nullチェックを掛けるサンプルをサクッと書いてみた。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Public Class Form1 | |
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click | |
Dim count = Me.Controls.Cast(Of Control)().Count(Function(c) (TypeOf c Is TextBox) AndAlso DirectCast(c, TextBox).Text = "") | |
If (count > 0) Then | |
MessageBox.Show("エラー") | |
End If | |
End Sub | |
End Class |
おお!ありがとうございます。ループが無くなりすっきりしたコードに成りますね。
条件をラムダ式で渡しているのですか。ふむふむ。
ちなみに、上記はCountメソッドにラムダで絞り込み条件与えてますが、池田さんが書かれたようにWhereで絞った結果に対してCountする場合は以下になるかと思います。
Dim count = Me.Controls.Cast(Of Control)().Where(Function(c) (TypeOf c Is TextBox) AndAlso DirectCast(c, TextBox).Text = "").Count()
その時のコンテキストに合わせて意味が分かりやすいほうで書けばよいかと思いますー。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
これとかどうでしょう。
Dim count = Me.Controls.Cast(Of Control)().Count(Function(c) (TypeOf c Is TextBox) AndAlso DirectCast(c, TextBox).Text = "")