|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Databind to ToolStripDropDown?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 Hello Ben R.,
> I have a ToolStripDropDownButton and would like to databind a list of AFAIK, this is what you need to do. I have run into the same want a few > 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? times and just ended up writing a couple of helper functions to get the job done. -- Jared Parsons [MSFT] jared***@online.microsoft.com All opinions are my own. All content is provided "AS IS" with no warranties, and confers no rights. Hi Jared,
is it possible that you post your "couple of helper functions" ? (i have also the same need) Thanks in advance Dominique *** Sent via Developersdex http://www.developersdex.com *** Hello Dominique,
> Hi Jared, Here's the basic class that I use. It accepts any kind of list as the data > > is it possible that you post your "couple of helper functions" ? (i > have also the same need) > 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] jared***@online.microsoft.com All opinions are my own. All content is provided "AS IS" with no warranties, and confers no rights. Thanks Jared!
-Ben Show quoteHide quote "Jared Parsons [MSFT]" wrote: > 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] > jared***@online.microsoft.com > All opinions are my own. All content is provided "AS IS" with no warranties, > and confers no rights. > > > |
|||||||||||||||||||||||