Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to determine the field a textbox is bound to
Message
De
30/10/2010 11:23:55
 
 
À
30/10/2010 11:15:22
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:
01487605
Vues:
32
Hi,
I had to fire up VB to be sure I'd remembered the syntax correctly (and to remind myself how much I prefer C# (g)). But I don't really know whether it will work properly with the DataTable/DataView bits....
BTW what's your 'favorite online C#-to-VB converter' ?

>I was going to suggest to Marc some of what you just posted, Viv ... but, since it's VB and I didn't quite know how to translate what my C# suggestion would be: if (Ctrl is IControlMaskit), I figured I'd leave it alone, since it worked. Hmmm, well, checking my favorite online C#-to-VB converter, I see that it would have translated that just fine ... well, there goes my excuse for being a slacker! <g>



>
>>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)
>>
>>>
>>>    Private Sub frmXXX_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
>>>        Me.MasKit(Me.XXXDataset.XXX.Columns)
>>>    End Sub
>>>
>>>
>>>so, cols is really Me.XXXDataset.XXX.Columns is a DataColumnCollection, belonging to a datatable ... I think.
>>>
>>>in my baseform I have :
>>>
>>>    Friend Sub MasKit(ByVal cols As DataColumnCollection)
>>>        Dim ctrl2 As IControlMaskit = Nothing
>>>        For Each Ctrl As Control In Me.Controls
>>>            Dim lApplicable As Boolean = True
>>>            Try
>>>                ctrl2 = Ctrl
>>>            Catch 
>>>                lApplicable = False
>>>            End Try
>>>            If lApplicable Then
>>>                ctrl2.MaskIt(cols)
>>>            End If
>>>        Next
>>>    End Sub
>>>
>>>
>>>and at the txtBase level (inferring from your wise advice ...)
>>>
>>>
>>>    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 t As DataTable = CType(B.DataSource, DataTable)
>>>        Dim c As DataColumn = Nothing
>>>
>>>        If t Is Nothing Then
>>>            Dim t1 As DataView = B.DataSource
>>>            c = t1.Columns(B.BindingMemberInfo.BindingField)
>>>        Else
>>>            c = t.Columns(Me.DataBindings.Item(0).BindingMemberInfo.BindingField)
>>>        End If
>>>
>>>        ' Dim o As DataColumn = cols(Me.DataBindings.Item(0).BindingMemberInfo.BindingField)
>>>        Dim cT As String = c.DataType.ToString
>>>        If cT = "System.String" Then
>>>            Me.Mask = ">" & New String("A", c.MaxLength)
>>>            Exit Sub
>>>        End If
>>>        If cT = "System.DateTime" Then
>>>            Me.Mask = ">##/AAA/##"
>>>            Exit Sub
>>>        End If
>>>        MsgBox(Me.Name & " cannot be masked yet (" & cT & ")")
>>>    End Sub
>>>
>>>
>>>Now this won't compile because "columns is not a member of 'system.data.dataview', and leaving out the dataview section does not work. (I don't think I need the dataview section). When I debug, B.Datasource does not seem to be set (in fact it returns "nothing", your "null" I guess?)
>>>
>>>I get the feeling from what I read in my frmXXX.designer.vb that the datasource of the "binding" is a dataset, whereas you seemed to assume that it should be a datatable or a dataview.
>>>
>>>That is precisely the problem I guess, because the form generates a xxxdataset that and binds the controls to a datatable that belongs the xxxDataset and is called XXX and referred to as xxxDataset.xxx. And it's the latter that I would like get a hold of from my base class textbox.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform