Last active
December 14, 2015 13:58
-
-
Save akira345/5096951 to your computer and use it in GitHub Desktop.
VB.Netでパネル上に配置されたテキストボックスのNullチェック
This file contains hidden or 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 Requisite() As String = {"Txt_name", "Txt_jyusyo", "Txt_tel"} | |
| ' | |
| 'カレントコントロールから | |
| 'OfTypeでまずはパネルコントロールをフィルタ | |
| 'これをfromとし、 | |
| 'さらにテキストボックスをフィルタ Function(c) | |
| 'これに対し、Function(c1)で、配列Requisiteに入っている要素から、TextがNullのものを選択 Function(cc) | |
| ' | |
| 'Dim count = Me.Controls.Cast(Of Control)().OfType(Of Panel).Count(Function(c) c.Controls.Cast(Of Control).OfType(Of TextBox).Where(Function(c1) Requisite.Contains(c1.Name)).Any(Function(cc) cc.Text = "")) | |
| Dim count = _ | |
| Me.Controls.Cast(Of Control)(). _ | |
| OfType(Of Panel). _ | |
| Count( _ | |
| Function(c) c.Controls.Cast(Of Control). _ | |
| OfType(Of TextBox). _ | |
| Where(Function(c1) Requisite.Contains(c1.Name)). _ | |
| Any(Function(cc) cc.Text = "") _ | |
| ) | |
| If count > 0 Then | |
| MessageBox.Show("エラー") | |
| End If | |
| End Sub | |
| End Class |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
なるほど。結果としてはT/Fしかないので、条件に合致するパネルの数でも確かに問題ないですね。
(この発想転換が中々出来ない)
OfTypeメソッド、知りませんでした。これだけでいつくかのループが削除できそうです。
勉強に成りました。
(未入力項目が3つなのにcountの値が1なのでバグかと思ったのは内緒だw)