Skip to content

Instantly share code, notes, and snippets.

@akira345
Last active December 14, 2015 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akira345/5096451 to your computer and use it in GitHub Desktop.
Save akira345/5096451 to your computer and use it in GitHub Desktop.
VB.Netで複数のテキストボックスに対し、Nullチェックを掛けるサンプルをサクッと書いてみた。
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
@kiyokura
Copy link

kiyokura commented Mar 6, 2013

これとかどうでしょう。

Dim count = Me.Controls.Cast(Of Control)().Count(Function(c) (TypeOf c Is TextBox) AndAlso DirectCast(c, TextBox).Text = "")

@akira345
Copy link
Author

akira345 commented Mar 6, 2013

おお!ありがとうございます。ループが無くなりすっきりしたコードに成りますね。
条件をラムダ式で渡しているのですか。ふむふむ。

@kiyokura
Copy link

kiyokura commented Mar 6, 2013

ちなみに、上記は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