Databind to ToolStripDropDown?

G

Guest

Hi,

I have a ToolStripDropDownButton and would like to databind a list of
menuitems for its associated ToolStripDropDown object. Is this possible? The
ToolStripDropDown class doesn't have a datasource property. Do I need to
manually enumerate through my array and add the items one by one?

Thanks...

-Ben
 
J

Jared Parsons [MSFT]

Hello Ben R.,
I have a ToolStripDropDownButton and would like to databind a list of
menuitems for its associated ToolStripDropDown object. Is this
possible? The ToolStripDropDown class doesn't have a datasource
property. Do I need to manually enumerate through my array and add the
items one by one?

AFAIK, this is what you need to do. I have run into the same want a few
times and just ended up writing a couple of helper functions to get the job
done.
 
D

Dominique

Hi Jared,

is it possible that you post your "couple of helper functions" ?
(i have also the same need)

Thanks in advance

Dominique
 
J

Jared Parsons [MSFT]

Hello Dominique,
Hi Jared,

is it possible that you post your "couple of helper functions" ? (i
have also the same need)

Here's the basic class that I use. It accepts any kind of list as the data
source. ReloadFromDataSource needs to be called whenever the list changes
unless the collection is an IBindingList. In that case it will auto update.


Imports System.ComponentModel

Public Class DropDownBinding
Private m_dd As ToolStripDropDown
Private m_list As IEnumerable
Private m_bindingList As IBindingList

Public Sub New(ByVal dd As ToolStripDropDown)
m_dd = dd
m_list = New List(Of Object)()
End Sub

Public Sub SetDataSource(ByVal e As IEnumerable)
If Not m_bindingList Is Nothing Then
RemoveHandler m_bindingList.ListChanged, New ListChangedEventHandler(AddressOf
Me.OnListChanged)
End If

m_bindingList = TryCast(e, IBindingList)
m_list = e

If Not m_bindingList Is Nothing Then
AddHandler m_bindingList.ListChanged, New ListChangedEventHandler(AddressOf
Me.OnListChanged)
End If

RebuildList()
End Sub

Public Sub ReloadFromSource()
RebuildList()
End Sub

Private Sub OnListChanged(ByVal sender As Object, ByVal e As ListChangedEventArgs)
RebuildList()
End Sub

Private Sub RebuildList()
m_dd.Items.Clear()

For Each cur As Object In m_list
m_dd.Items.Add(cur.ToString())
Next
End Sub
End Class
 
G

Guest

Thanks Jared!

-Ben

Jared Parsons said:
Hello Dominique,
Hi Jared,

is it possible that you post your "couple of helper functions" ? (i
have also the same need)

Here's the basic class that I use. It accepts any kind of list as the data
source. ReloadFromDataSource needs to be called whenever the list changes
unless the collection is an IBindingList. In that case it will auto update.


Imports System.ComponentModel

Public Class DropDownBinding
Private m_dd As ToolStripDropDown
Private m_list As IEnumerable
Private m_bindingList As IBindingList

Public Sub New(ByVal dd As ToolStripDropDown)
m_dd = dd
m_list = New List(Of Object)()
End Sub

Public Sub SetDataSource(ByVal e As IEnumerable)
If Not m_bindingList Is Nothing Then
RemoveHandler m_bindingList.ListChanged, New ListChangedEventHandler(AddressOf
Me.OnListChanged)
End If

m_bindingList = TryCast(e, IBindingList)
m_list = e

If Not m_bindingList Is Nothing Then
AddHandler m_bindingList.ListChanged, New ListChangedEventHandler(AddressOf
Me.OnListChanged)
End If

RebuildList()
End Sub

Public Sub ReloadFromSource()
RebuildList()
End Sub

Private Sub OnListChanged(ByVal sender As Object, ByVal e As ListChangedEventArgs)
RebuildList()
End Sub

Private Sub RebuildList()
m_dd.Items.Clear()

For Each cur As Object In m_list
m_dd.Items.Add(cur.ToString())
Next
End Sub
End Class




--
Jared Parsons [MSFT]
(e-mail address removed)
All opinions are my own. All content is provided "AS IS" with no warranties,
and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top