Enumerate bindings, set MaxLength of textbox to MaxLength of underlying data column?

cjard

Well-known member
Joined
Apr 25, 2006
Messages
7,081
Programming Experience
10+
I'm trying, at runtime form load, to whizz through all the controls that are bound and if they are a kind of textbox, to set the MaxLength equal to the Max Length of the column on the underlying datasource.

I pinched a bit of code from another post:

VB.NET:
        For Each binding As Binding In DDH_ADDRESSESBindingNavigator.BindingSource.CurrencyManager.Bindings
            If TypeOf binding.Control Is TextBoxBase Then
                 directcast(binding.Control , TextBoxBase).MaxLength = ___
            End If
        Next binding


and now i'm just wondering the most effective way to retrieve the datacolumn that the binding pertains to, so i can query its max length?
 
I did it this way in the end:

VB.NET:
        For Each binding As Binding In DDH_ADDRESSESBindingSource.CurrencyManager.Bindings
            If TypeOf binding.Control Is TextBoxBase Then
                DirectCast(binding.Control, TextBoxBase).MaxLength = HubDSInstance.DDH_ADDRESSES.Columns.Item(binding.BindingMemberInfo.BindingField).MaxLength 
            End If
        Next binding
        For Each binding As Binding In [B]DDH_CLIENTSBindingSource[/B].CurrencyManager.Bindings
            If TypeOf binding.Control Is TextBoxBase Then
                DirectCast(binding.Control, TextBoxBase).MaxLength = [B]HubDSInstance.DDH_CLIENTS[/B].Columns.Item(binding.BindingMemberInfo.BindingField).MaxLength 
            End If
        Next binding

really, what I was after was a way to get to the datatable without going through the name of it, so i could copy and paste the code more easily across multiple forms.. i dont mind editing the two bold items, however
 
Back
Top