473,473 Members | 2,169 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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
Jul 10 '06 #1
5 3446
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]
ja******@online.microsoft.com
All opinions are my own. All content is provided "AS IS" with no warranties,
and confers no rights.
Jul 10 '06 #2

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 ***
Jul 11 '06 #3
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]
ja******@online.microsoft.com
All opinions are my own. All content is provided "AS IS" with no warranties,
and confers no rights.
Jul 11 '06 #4
Thanks, Jared.

Best regards,

Dominique

*** Sent via Developersdex http://www.developersdex.com ***
Jul 12 '06 #5
Thanks Jared!

-Ben

"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]
ja******@online.microsoft.com
All opinions are my own. All content is provided "AS IS" with no warranties,
and confers no rights.
Jul 12 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: autofill | last post by:
2 scenario inside a WebUserControl. I want to know why Microsoft and so many book doesn't suggest us to work like that. This control going to be in my page and I will call the DataBind of my...
3
by: Marty McDonald | last post by:
I have <asp:Table... </asp:Table> on my webform. In codebehind, I populate a DataTable whose data should appear in the asp:Table. I created my own code to populate the asp:Table with the...
1
by: Martin | last post by:
Hi, I've got a web label control which I set a databind expression on the Text attribute in the html for the page (aspx file). At runtime, I want to override this attribute, so before I do...
2
by: Jim Bancroft | last post by:
Hi everyone, I have a DropDownList I populate as outlined below. This is from my code-behind file: private void Page_Load(object sender, System.EventArgs e) { BindMyData(); DataBind(); }
1
by: Aayush Puri | last post by:
I was going through an article on datagrid control in ASP.NET and found the use of databind a bit confusing. So the default behavior in a editable datagrid row is to show a Update and a Cancel...
0
by: Matt Spinder | last post by:
Can I create a ToolStripDropDown that overflows to a second column instead of scrolling?
0
by: nicklang | last post by:
Say you are creating a page which contains a child control which uses a DataList or DataGrid control. In a DataList, the rows are dynamic controls and are data bound, so you have to first load the...
0
by: MP | last post by:
Hi all, I have: ToolStripDropDown control with Label, TextBox and two Button controls inside it. When TextBox has the focus and I press right Alt key then ToolStripDropDown is closed in the...
1
by: =?Utf-8?B?bGpsZXZlbmQy?= | last post by:
I've noticed that controls do not raise a Validating event if they are contained in a ToolStripDropDown via a ToolStripControlHost item. Please run the following sample and follow the instructions...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.