Solved

Execute a function for every ComboBox item on a form.

Posted on 2004-09-09
12
268 Views
Last Modified: 2011-10-03
Hi there Experts ,

I have a form in C# ,  when I open it, I fill the combo items using some SQL statements.
I would like to do something like " For every ComboBox items, call function FillComboBox "

Can this be done in C#?

Thanks for your answers!

Francis
0
Comment
Question by:Francis_B
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +3
12 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12022141
It looks like this in VB.Net:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim cbItem As String

        For Each cbItem In ComboBox1.Items
            ' Do something with cbItem...
            Debug.WriteLine(cbItem)
        Next
    End Sub

Regards,

Idle_Mind
0
 

Author Comment

by:Francis_B
ID: 12022819
Maybe my question is not clear;

in plain english, what I am looking for is code in C# that would do simething like:

for all System.Windows.Form.ComboBox , Set the Fore color to Black.

And this code would basically change all ComboCox fileds on the form so their Fore color is black.

Francis.
0
 
LVL 5

Assisted Solution

by:tzxie2000
tzxie2000 earned 300 total points
ID: 12023004
code below

for (i=0;i<this.Controls.Count;i++)
{
   if(this.Controls[i] is ComboBox)
      {
        MessageBox.Show((Controls[i] as Combobox).Name);//replace this to what you want to do to the combobox
      }
}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 200 total points
ID: 12023142
Private Sub foo()
    Dim c As Control

    For Each c In Me.Controls
        If TypeOf c Is ComboBox Then
            c.ForeColor = Color.Black
        End If
    Next
End Sub
0
 

Author Comment

by:Francis_B
ID: 12023206
Idle Mind and tzxie2000;

Both solution will work, but there is one thing that I am trying to avoid: Evaluate all controls.
Both solutions will go trough all controls on the form, and check if they are ComboBoxes.
Isn't there a way to refer to the ComboBox collection directly?

Sorry to be so pointy !!! ;-)
0
 
LVL 5

Expert Comment

by:tzxie2000
ID: 12023236
I think there are no method to do this if you want to make it more scalable
or you can add all combobox to a list when form loading if you have special thing to do and do not want to always go through the whole controls list
0
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 12023646
Also you can add all Comboboxes on one Panel then For each Control on Panel.Controls will iterate only the ComboBoxes.
0
 
LVL 5

Expert Comment

by:tzxie2000
ID: 12023716
yes
this will let you go through more fewer controls
and it also be an visual method to organize all the combobox
0
 
LVL 5

Expert Comment

by:KarcOrigin
ID: 12024410
Give me a try:

    Dim i As Control
    For Each i In Controls
        If TypeName(i) = "ComboBox" Then
            i.ForeColor = vbRed
        End If
    Next

This open also goes through all the controls.

REgArDs,
0
 
LVL 58

Expert Comment

by:Julian Hansen
ID: 12024694
There are a number of things you could do.

1. I create the combo boxes dynamically and store their indexes in an array (or some pointer to the combo boxes)

2. If you are going to need to do the combo refresh a number of times then when the form is initialised loop through all controls once and save the indexs of the combo boxes you are interested.

3. Create the combo boxes in order on the form and then loop from first combobox index to last combobox index (sort of a variation on the dynamic option in 1)

Failing this you pretty much have to loop through all controls.
0
 
LVL 5

Expert Comment

by:tzxie2000
ID: 12025827
you can add code after windows design code part to set all combo box to an array
dynamically create may have a little difficult in place the combo box
all the methods will be ok depend on the program you're doing
0
 

Author Comment

by:Francis_B
ID: 12026051
Great comments and thanks to all of you guys.

The main goal of this was that I have created a function that fills  the comboboxes by getting the values of my SQL server. So for every ComboBox on a form, I had to call the function in the FormLoad event. And since there is about 35 comboBox on the form, and that there is a possibility that more will be added, I wanted to automate the ComboBox item filling process... So I guess I will use Idle Mind and tzxie2000 concept of looping trough all controls and call the function if the type is ComboBox.

I will give out the points today, if nobody else have any other solution.

Thanks for your great assistance everyone!

Francis
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question