Solved

Execute a function for every ComboBox item on a form.

Posted on 2004-09-09
12
264 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
  • 4
  • 3
  • 2
  • +3
12 Comments
 
LVL 85

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 85

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 54

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This is about my first experience with programming Arduino.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

777 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