分類:[.NET]
2003/07/02(Wed) 11:35:16 編集(投稿者)自力で作成しました。サンプルを載せておきます。 GroupBoxやPanel等を使った階層が深くなるコントロールも探せます。 ---------------------------------------------------------------------- Private Const TextBoxCount As Integer = 13 '13個の配列を作成 Private Const TextBoxName As String = "TextBox" 'デザインで使用している名称 Private TextBox(TextBoxCount - 1) As System.Windows.Forms.TextBox Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim Ctrl As System.Windows.Forms.Control Dim intIdx As Integer For intIdx = 0 To TextBox.Length - 1 Ctrl = FindControl(Me, TextBoxName & intIdx + 1) If IsNothing(Ctrl) = False Then '配列に対し設定 TextBox(intIdx) = CType(Ctrl, TextBox) TextBox(intIdx).Text = "" 'イベントハンドラに関連付け AddHandler TextBox(intIdx).GotFocus, AddressOf Me.TextBox_GotFocus AddHandler TextBox(intIdx).LostFocus, AddressOf Me.TextBox_LostFocus End If Next End Sub Private Sub TextBox_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Dim index As Integer = System.Array.IndexOf(TextBox, sender) TextBox(index).BackColor = Color.LightCyan End Sub Private Sub TextBox_LostFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Dim index As Integer = System.Array.IndexOf(TextBox, sender) TextBox(index).BackColor = Color.White End Sub --- 以下モジュールにしても可能 --- Public Function FindControl(ByVal ParentsCtrl As System.Windows.Forms.Control, ByVal FindCtrlName As String) As System.Windows.Forms.Control Dim FindCtrl As System.Windows.Forms.Control If SearchCtrl(ParentsCtrl, FindCtrlName, FindCtrl) = True Then Return FindCtrl End If End Function Private Function SearchCtrl(ByVal ParentsCtrl As System.Windows.Forms.Control, ByVal FindCtrlName As String, ByRef FindCtrl As System.Windows.Forms.Control) As Boolean Dim Ctrl As System.Windows.Forms.Control Dim CtrlCount As Integer = ParentsCtrl.Controls.Count Dim intIdx As Integer SearchCtrl = False For intIdx = 0 To CtrlCount - 1 '子コントロール検査 Ctrl = ParentsCtrl.Controls.Item(intIdx) If FindCtrlName = Ctrl.Name Then SearchCtrl = True FindCtrl = Ctrl Exit Function Else If Ctrl.Controls.Count > 0 Then '再帰処理 If SearchCtrl(Ctrl, FindCtrlName, FindCtrl) = True Then SearchCtrl = True Exit Function End If End If End If Next End Function