Reputation: 3618
I am working on a maintenance windows forms application with Tabbed Interface. In the form there are 3 binding sources (lets call it BindingSource1, BindingSource2, BindingSource3). I am trying to optimize the code and want to dynamically access the BindingSource something like this:
objBindingSource = Ctype(Me.Controls("BindingSource" + SelectedBindingSourceID),BindingSource)
I know that it cannot be accomplished using CType since a Control cannot be cast into BindingSource.
Any ideas on how to accomplish this would be great.
Thanks,
Raja
Upvotes: 1
Views: 2916
Reputation: 292375
A BindingSource
is a Component
, not a Control
, so it is not in the Controls
collection. However, the designer creates a private IContainer
field called components
to hold all components created on the form, so you can access the components through that field :
For Each c In components.Components
MessageBox.Show(c.ToString())
Next
Unfortunately, components don't have a name, so you will have to find another way to identify your BindingSource
... For instance, if you know that each BindingSource
is bound to a DataTable
, you can check the name of the table.
Private Function GetBindingSource(ByVal tableName As String) As BindingSource
For Each c In components.Components
Dim bs As BindingSource = TryCast(c, BindingSource)
' If the component is a BindingSource
If bs IsNot Nothing Then
Dim dt As DataTable = TryCast(bs.DataSource, DataTable)
' If the DataSource is a DataTable
If dt IsNot Nothing Then
' Check the table name against the parameter
If dt.TableName = tableName Then
' Found it !
Return bs
End If
End If
End If
Next
' Oops, BindingSource not found
Return Nothing
End Function
EDIT: the SO syntax highlighter seems to have trouble with VB...
Upvotes: 3
Reputation: 1880
Personally, if there are only three BindingSources, why not just access them directly instead of via a collection? If it's just to pretty up the code to be able to run it through a loop, I don't see much benefit. However, if that's what you want to do, one way to do this would be to initialize the BindingSources within the Form or UserControl's constructor (outside the InitializeComponents method) and add them to the Components collection manually. Doing it this way will allow you to assign a name as a key to the BindingSource within the Components collection. You can then access them like so: (forgive my C#, I'm not that fluent in VB, but you'll get the jist)
BindingSource bs1 = new BindingSource();
BindingSource bs2 = new BindingSource();
BindingSource bs3 = new BindingSource();
// set properties on BindingSources....
// add BindingSources to componenents collection manually.
// add a name key
components.Add(bs1, "BindingSource1");
components.Add(bs2, "BindingSource2");
components.Add(bs3, "BindingSource3");
// access the BindingSource
BindingSource bsSelected = components.Components["BindingSource" + SelectedBindingSourceID] as BindingSource;
if (bsSelected == null)
{
throw new Exception("BindingSource" +
SelectedBindingSourceID + " doesn't exist");
}
It's not pretty, but it may help you.
Upvotes: 0