投稿者 neptune  (社会人) 投稿日時 2009/4/26 04:56:31
続き


'//////////////////////////////Class.vb////////////////////// 
Imports System.Collections.Generic

Public Class TextControls

    Private txtAry As List(Of TextControl)
    Public Event TextControls_UpDated(ByVal pText As TextBox)

    Public Sub New()
        txtAry = New List(Of TextControl)
    End Sub

    ''' <summary> 
    ''' Itemの追加 
    ''' </summary> 
    ''' <param name="pObj">TextBox</param> 
    ''' <returns>成功時:tru</returns> 
    ''' <remarks></remarks> 
    Public Function AddItem(ByVal pObj As TextBox) As Boolean
        Dim clsTxt As New TextControl
        Dim ret As Boolean = False
        Dim newCount As Integer

        Try
            If txtAry.Count = 0 Then
                newCount = 0
            Else
                newCount = txtAry.Count
            End If

            clsTxt.Item = pObj
            AddHandler clsTxt.Text_UpDated, AddressOf Text_UpDated
            txtAry.Add(clsTxt)
            ret = True
        Catch ex As Exception

        End Try

        Return ret
    End Function

    ''' <summary> 
    ''' 全てのTextBoxのTextをClearする 
    ''' </summary> 
    ''' <returns>成功時:true</returns> 
    ''' <remarks></remarks> 
    Public Function ClearAllText() As Boolean
        Dim ret As Boolean = False
        Dim iCount As Integer = txtAry.Count()

        Try
            For i = 0 To iCount - 1
                txtAry(i).Item.Clear()
            Next
            ret = True
        Catch ex As Exception

        End Try
        Return ret
    End Function

    Public ReadOnly Property Items(ByVal pIndex As Integer) As System.Windows.Forms.TextBox
        Get
            Return txtAry(pIndex).Item
        End Get
    End Property

    'TextBoxで変更があった時のイベントを受け取る 
    Private Sub Text_UpDated(ByVal ptext As TextBox)
        RaiseEvent TextControls_UpDated(ptext)
        'MessageBox.Show(ptext.Name & "に変更がありました。") 
    End Sub

    '不要かも 
    Protected Overrides Sub Finalize()
        If IsArray(txtAry) Then
            txtAry.Clear()
        End If
        MyBase.Finalize()
    End Sub

End Class

'子クラス 
Public Class TextControl

    Private WithEvents m_Item As System.Windows.Forms.TextBox
    Private m_OldText As String
    Public Event Text_UpDated(ByVal pText As TextBox)


    'これはなくても良いと思う。 
    Protected Overrides Sub Finalize()
        m_Item = Nothing
        MyBase.Finalize()
    End Sub

    Public Property Item() As System.Windows.Forms.TextBox
        Get
            Return m_Item
        End Get
        Set(ByVal value As System.Windows.Forms.TextBox)
            m_Item = value
        End Set
    End Property

    'フォーカスを受け取った時のデータ保管 
    Private Sub m_Item_Enter(ByVal sender As TextBox, ByVal e As System.EventArgs) Handles m_Item.Enter
        m_OldText = sender.Text
    End Sub

    '変更後のデータを前のデータと比較、変更があれば Text_UpDated を発生させる 
    Private Sub m_Item_Validated(ByVal sender As TextBox, ByVal e As System.EventArgs) Handles m_Item.Validated
        If m_OldText.Length = 0 Then Exit Sub
        '全く同じでなければイベント発生 
        If String.Compare(sender.Text, m_OldText) <> 0 Then
            RaiseEvent Text_UpDated(sender)
        End If
    End Sub
End Class