Solved

Execute a function for every ComboBox item on a form.

Posted on 2004-09-09
12
263 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 52

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
countHi challenge 25 98
base64 decode encode 12 119
recursion example 16 111
Hide vba in gp 7 80
This is an explanation of a simple data model to help parse a JSON feed
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
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 …
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 …

919 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now