Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to determine the field a textbox is bound to
Message
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
VB 8.0
OS:
Vista
Network:
Windows XP
Database:
Jet/Access Engine
Application:
Desktop
Divers
Thread ID:
01487443
Message ID:
01487740
Vues:
44
Hi,
TBH, I'm rusty on DataTable/DataView stuff but it looks as if you and Bonnie have sorted it. It did occur to me that if the MasKit method was the only item in the interface you could keep the txtBase class lighter by handling this is the form method:
 Friend Sub MasKit(ByVal cols As DataColumnCollection)
        For Each Ctrl In Me.Controls
            If (TypeOf Ctrl Is IControlMaskit) Then
                Dim icm As IControlMaskit = CType(Ctrl, IControlMaskit)
                 If icm.Mask <> "" Then
                    Exit Sub
                End If

                Dim B As Binding = Ctrl.DataBindings.Item(0)

                Dim O As Object = B.DataSource
                If Not (TypeOf O Is DataTable) Then
                    O = CType(O, DataView).Table
                End If

                Dim c As DataColumn = CType(O, DataTable).Columns(B.BindingMemberInfo.BindingField)
                Select Case c.DataType
                    Case GetType(System.String)
                        ' Possible error c.MaxLength can be negative.....
                        icm.Mask = ">" & New String("A", c.MaxLength)
                    Case GetType(System.DateTime)
                        icm.Mask = ">##/AAA/##"
                End Select
            End If
        Next
    End Sub
End Class

Public Interface IControlMaskit
    Property Mask() As String
End Interface
This would also allow you to use this with controls other than just TextBox...

>It's shorter, it's more elegant, and it works.
>
>Thanks a lot.
>
>Marc
>
>PS : it does not solve my conundrum that B.Datasource turns out to be a BindingSource that steadfastly refuses to divulge to which table the field belongs to which the control is bound.
>
>
>>Will this do what you need:
Friend Sub MasKit(ByVal cols As DataColumnCollection)
>>        For Each Ctrl In Me.Controls
>>            If (TypeOf Ctrl Is IControlMaskit) Then
>>                CType(Ctrl, IControlMaskit).MaskIt(cols)
>>            End If
>>        Next
>>    End Sub
   Friend Sub MaskIt(ByVal cols As DataColumnCollection) Implements IControlMaskit.MaskIt
>>        If Me.Mask <> "" Then
>>            Exit Sub
>>        End If
>>
>>        Dim B As Binding = Me.DataBindings.Item(0)
>>
>>        Dim O As Object = B.DataSource
>>        If Not (TypeOf O Is DataTable) Then
>>            O = CType(O, DataView).Table
>>        End If
>>
>>        Dim c As DataColumn = CType(O, DataTable).Columns(B.BindingMemberInfo.BindingField)
>>
>>        Select Case c.DataType
>>            Case GetType(System.String)
>>                ' Possible error since MaxLength can be negative.....
>>                Me.Mask = ">" & New String("A", c.MaxLength)
>>            Case GetType(System.DateTime)
>>                Me.Mask = ">##/AAA/##"
>>        End Select
>>    End Sub
If not then at least it's a bit shorter (s)
>>
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform