Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Execute a function for every ComboBox item on a form.

Posted on 2004-09-09
12
Medium Priority
?
271 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 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 900 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 600 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 60

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

971 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