Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
DotNet
acad.assignment.mngr
academic
adonet
aspnet
aspnet.announcements
aspnet.build.controls
aspnet.caching
aspnet.datagridcontrol
aspnet.mobile
aspnet.security
aspnet.webcontrols
aspnet.webservices
clr
compactframework
component_services
datatools
distributed_apps
drawing
faqs
framework
framework.wmi
general
internationalization
interop
languages.csharp
languages.jscript
languages.vb
languages.vb.controls
languages.vb.data
languages.vb.upgrade
languages.vc
languages.vc.libraries
myservices
odbcnet
performance
remoting
scripting
sdk
security
setup
vjsharp
vsa
webservi.enhancements
webservices
windowsforms
windowsforms.controls
winforms.databinding
winforms.designtime
xml
  
 
date: Tue, 1 Jul 2008 08:19:01 -0700,    group: microsoft.public.dotnet.framework.windowsforms.controls        back       


Combobox Data   
I have several Comboboxes that are all going to have the same data add to the 
items collection is there a way to add the same data to all of them at once???

Thanks in advance...

dotnetme
VB.net 2005
date: Tue, 1 Jul 2008 08:19:01 -0700   author:   dotnetme

RE: Combobox Data   
Hi,

That depends on what you mean by at once.  At the very least you need to 
loop through your ComboBoxes and set their DataSource or fill the Items list 
with a  common List.

string[] data = new string[] { "Morning", "Noon", "Afternoon", "Evening", 
"Night" };

foreach (ComboBox cb in ListOfComboBoxes)
{
    cb.Items.AddRange(data);
}

-- 
Happy Coding!
Morten Wennevik [C# MVP]


"dotnetme" wrote:

> I have several Comboboxes that are all going to have the same data add to the 
> items collection is there a way to add the same data to all of them at once???
> 
> Thanks in advance...
> 
> dotnetme
> VB.net 2005
date: Tue, 1 Jul 2008 23:10:00 -0700   author:   Morten Wennevik [C# MVP]

RE: Combobox Data   
Basically i read in an excel sheet into a untype(simple) Dataset and then i 
what to load all the Column names of the dataset into the item list of the 
comboboxes...   Well i want to do this the most efficent way and not sure how 
to...  I can definitly do it by calling each combobox and load the info in 
but i don't want to hard code that... incase i change the number of Combo 
boxes..  so should i loop through the form and add all comboboxes to a list 
then loop through that list and then set the item list???

--
GetExcelData.ReadExcelFile("Clive", DS)

ReDim sColList(DS.Tables(0).Columns.Count)

For x = 0 To DS.Tables(0).Columns.Count - 1
    sColList(x) = (DS.Tables(0).Columns(x).ColumnName)
Next

Dim Cbox As ComboBox
Dim ComboCtrl As Control
For Each Cbox In Me.TabPage1.Controls  
            'If ComboCtrl.GetType().ToString = 
"System.Windows.Forms.ComboBox" Then  'tried something like this but did't 
work.

            Cbox.Items.AddRange(sColList)
          
Next Cbox

--
Thanks
dotnetme


"Morten Wennevik [C# MVP]" wrote:

> Hi,
> 
> That depends on what you mean by at once.  At the very least you need to 
> loop through your ComboBoxes and set their DataSource or fill the Items list 
> with a  common List.
> 
> string[] data = new string[] { "Morning", "Noon", "Afternoon", "Evening", 
> "Night" };
> 
> foreach (ComboBox cb in ListOfComboBoxes)
> {
>     cb.Items.AddRange(data);
> }
> 
> -- 
> Happy Coding!
> Morten Wennevik [C# MVP]
> 
> 
> "dotnetme" wrote:
> 
> > I have several Comboboxes that are all going to have the same data add to the 
> > items collection is there a way to add the same data to all of them at once???
> > 
> > Thanks in advance...
> > 
> > dotnetme
> > VB.net 2005
date: Wed, 2 Jul 2008 07:50:01 -0700   author:   dotnetme

RE: Combobox Data   
"dotnetme" wrote:

> Basically i read in an excel sheet into a untype(simple) Dataset and then i 
> what to load all the Column names of the dataset into the item list of the 
> comboboxes...   Well i want to do this the most efficent way and not sure how 
> to...  I can definitly do it by calling each combobox and load the info in 
> but i don't want to hard code that... incase i change the number of Combo 
> boxes..  so should i loop through the form and add all comboboxes to a list 
> then loop through that list and then set the item list???
> 

You could loop through all the Controls in a TabPage and set the Items 
property, but the code wouldn't work if you later decide to put the 
ComboBoxes inside a GroupBox on that TabPage, or any other container control. 
 You could recursively loop through all controls on the form and set all 
ComboBoxes no matter where they were located, but that would prevent you from 
using a ComboBox with a separate list of Items.  Your best bet is to keep a 
reference to all the necessary ComboBoxes in a separate list, or add a line 
of code for each ComboBox where you set the Items property (this is probably 
the easiest to understand if someone else reads your code).

If you prefer the 'loop through tabpage1' option, the code below would 
probably be the most efficient way.

        Dim sColList As New List(Of String)()

        GetExcelData.ReadExcelFile("Clive", DS)

        For Each column As DataColumn In DS.Tables(0).Columns
            sColList.Add(column.ColumnName)
        Next

        For Each control As Control In Me.TabControl1.Controls
            If TypeOf control Is ComboBox Then
                CType(control, ComboBox).Items.AddRange(sColList.ToArray())
            End If
        Next

-- 
Happy Coding!
Morten Wennevik [C# MVP]
date: Wed, 2 Jul 2008 22:29:00 -0700   author:   Morten Wennevik [C# MVP]

RE: Combobox Data   
Thanks for the info... thinking about what you said about not wanting to load 
a future comobox with all of the data the other ones are getting... so I 
decided to tag all the comboboxes with the word "CliveList" to the Combobox 
name and then I added an "if" statement to check if the phrase is in the the 
name and if it is it loads the list.....  all works well...

        Dim sColList As New List(Of String)()

        GetExcelData.ReadExcelFile("Clive", DS)

        For Each column As DataColumn In DS.Tables(0).Columns
            sColList.Add(column.ColumnName)
        Next

        For Each control As Control In Me.TabPage1.Controls
            If TypeOf control Is ComboBox Then
                If InStr(control.Name, "CliveList") Then
                    CType(control, 
ComboBox).Items.AddRange(sColList.ToArray())
                End If
            End If
        Next

Thanks for help...

dotnetme 

"Morten Wennevik [C# MVP]" wrote:

> 
> "dotnetme" wrote:
> 
> > Basically i read in an excel sheet into a untype(simple) Dataset and then i 
> > what to load all the Column names of the dataset into the item list of the 
> > comboboxes...   Well i want to do this the most efficent way and not sure how 
> > to...  I can definitly do it by calling each combobox and load the info in 
> > but i don't want to hard code that... incase i change the number of Combo 
> > boxes..  so should i loop through the form and add all comboboxes to a list 
> > then loop through that list and then set the item list???
> > 
> 
> You could loop through all the Controls in a TabPage and set the Items 
> property, but the code wouldn't work if you later decide to put the 
> ComboBoxes inside a GroupBox on that TabPage, or any other container control. 
>  You could recursively loop through all controls on the form and set all 
> ComboBoxes no matter where they were located, but that would prevent you from 
> using a ComboBox with a separate list of Items.  Your best bet is to keep a 
> reference to all the necessary ComboBoxes in a separate list, or add a line 
> of code for each ComboBox where you set the Items property (this is probably 
> the easiest to understand if someone else reads your code).
> 
> If you prefer the 'loop through tabpage1' option, the code below would 
> probably be the most efficient way.
> 
>         Dim sColList As New List(Of String)()
> 
>         GetExcelData.ReadExcelFile("Clive", DS)
> 
>         For Each column As DataColumn In DS.Tables(0).Columns
>             sColList.Add(column.ColumnName)
>         Next
> 
>         For Each control As Control In Me.TabControl1.Controls
>             If TypeOf control Is ComboBox Then
>                 CType(control, ComboBox).Items.AddRange(sColList.ToArray())
>             End If
>         Next
> 
> -- 
> Happy Coding!
> Morten Wennevik [C# MVP]
> 
> 
>
date: Thu, 3 Jul 2008 07:07:01 -0700   author:   dotnetme

RE: Combobox Data   
"dotnetme" wrote:

> Thanks for the info... thinking about what you said about not wanting to load 
> a future comobox with all of the data the other ones are getting... so I 
> decided to tag all the comboboxes with the word "CliveList" to the Combobox 
> name and then I added an "if" statement to check if the phrase is in the the 
> name and if it is it loads the list.....  all works well...
> 

Remember that changing the Name property of a Control may confuse the Forms 
Designer as the Designer often assumes the Name is equal to the reference 
name.  

There is a better way to tag controls using the Tag property :)

foreach(Control c in Controls)
{
   if(c.Tag == someTagValue)
      ...
}

-- 
Happy Coding!
Morten Wennevik [C# MVP]
date: Thu, 3 Jul 2008 22:02:00 -0700   author:   Morten Wennevik [C# MVP]

RE: Combobox Data   
Thanks...  I did notice the tag property after the fact I will use it as you 
advised..

Thanks again 

dotnetme

"Morten Wennevik [C# MVP]" wrote:

> 
> "dotnetme" wrote:
> 
> > Thanks for the info... thinking about what you said about not wanting to load 
> > a future comobox with all of the data the other ones are getting... so I 
> > decided to tag all the comboboxes with the word "CliveList" to the Combobox 
> > name and then I added an "if" statement to check if the phrase is in the the 
> > name and if it is it loads the list.....  all works well...
> > 
> 
> Remember that changing the Name property of a Control may confuse the Forms 
> Designer as the Designer often assumes the Name is equal to the reference 
> name.  
> 
> There is a better way to tag controls using the Tag property :)
> 
> foreach(Control c in Controls)
> {
>    if(c.Tag == someTagValue)
>       ...
> }
> 
> -- 
> Happy Coding!
> Morten Wennevik [C# MVP]
> 
>
date: Mon, 7 Jul 2008 07:56:01 -0700   author:   dotnetme

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us