Home All Groups Group Topic Archive Search About

Databind to ToolStripDropDown?

Author
10 Jul 2006 8:12 PM
Ben R.
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

Author
10 Jul 2006 10:28 PM
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.

--
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.
Author
11 Jul 2006 9:47 PM
Dominique
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 ***
Author
11 Jul 2006 10:45 PM
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




--
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.
Author
12 Jul 2006 1:13 PM
Dominique
Thanks, Jared.

Best regards,

Dominique



*** Sent via Developersdex http://www.developersdex.com ***
Author
12 Jul 2006 8:35 PM
Ben R.
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.
>
>
>