collection of controls that are bound to a BindingSource

eric13

Active member
Joined
May 25, 2006
Messages
30
Programming Experience
3-5
Does anyone know if you can or how u can get a collection of controls that are bound to your BindingSource. I want to be able to dynamically enable or disable the collection of controls based on whether my user is in edit mode or not.
 
There's no reason why you cant do it ahead of time.. You can scan all the controls on the form and ask them for their binding collection.. if they have any items in the dinding collection, iterate the collection and see if the datasource of any of the Binding objects (in the collection) is your binding source.. if it is then add the control to an arraylist called "controlsBoundToMyXXXDataSource"

and when the form mode changes, do as you will with those controls



Personally, i needed to do a similar thing and i found it faster and easier to just shove all the controls on the form in one arraylist (even though my forms used a few binding sources, all the controls needed enabling or disabling depending on add/browse/searhc mode etc)

and then i removed some of the controls to other arraylists if i wanted to do soemthing different with them..

i.e. im saying you might find it easier to just make an arraylist called "controlsThatMustBeDisabledInEditMode" and just manually put the controls in there that you want... etc
 
Thanks for your solution cjard,

Sorry, I am quite new to VB.NET, coming from the Foxpro world. Fox had a navigator control that would do this for us.

I was hoping to create a custom BindingNavigator control that could be used on all of our standard maintenance forms.

I got this far,
VB.NET:
[COLOR=yellowgreen]'added a edit toolstripbutton to the bindingnavigator[/COLOR]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] EditButton_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] btnCancel.Click
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] tsb [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] ToolStripButton
tsb = sender
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] bn [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] BindingNavigator = tsb.GetCurrentParent()
bn.BindingSource. [COLOR=yellowgreen]'At this point i have the binding source for the navigator and i want to set the enable/disable modes to each control binded to the source.[/COLOR]
 
[COLOR=yellowgreen]' i think i can use your solution here[/COLOR][SIZE=2][COLOR=#0000ff]
For[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] ctr [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Control [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=black]tsb.Owner.Parent.Controls [/COLOR][COLOR=yellowgreen]'me.controls[/COLOR]
[/SIZE][/COLOR][/SIZE][SIZE=2]MsgBox(ctr.Name)
[SIZE=2][COLOR=#0000ff][COLOR=yellowgreen]'determine here if bound to bn.BindingSource and set mode[/COLOR]
Next
[/COLOR][/SIZE][/SIZE]
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
Problem here is that i am using container controls such as the tabcontrol. The controls on the tabcontrol are not in the tsb.Owner.Parent.Controls collection. Is there a way to every control on a form regardless of whether it is in a container control.
 
Last edited by a moderator:
You don't actually need to check the controls manually.
VB.NET:
Private Sub BindingNavigatorEditItem_Click(ByVal sender As System.Object, _
                                           ByVal e As System.EventArgs) Handles BindingNavigatorEditItem.Click
    Dim button As ToolStripButton = DirectCast(sender, ToolStripButton)
    Dim navigator As BindingNavigator = DirectCast(button.GetCurrentParent(), BindingNavigator)

    'Enable all bound controls for editing.
    For Each binding As Binding In navigator.BindingSource.CurrencyManager.Bindings
        binding.Control.Enabled = True
    Next binding
End Sub
 
Good solve! - i looked at this very briefly before I left work, and i didnt notice that the Binding objects in the BindingCollection i discussed, had a .Control property. I may be able to use this in my project, so thanks!
 
Back
Top