Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pass a Combobox in to a function?

Status
Not open for further replies.

vbSun

Programmer
Dec 9, 2002
504
US
Hi,

I am learning C# these days and trying to port logic from some VB code to C#.

Would like to know how can I pass a Combobox in to a function. The function then will add items to the combobox. Is this possible in C#? If yes, can someone please explain?

Thanks for all help.

------------------------------------------
The faulty interface lies between the chair and the keyboard.
 
You can pass a combobox into a function by

public void FillMyCombo(ComboBox box)
{
box.Items.Add("Item1");
}

However, it is unconventional to pass GUI Components into a function. Typically, you would have a place where the ComboBox data is stored (a list of data for example) and your ComboBox simply binds to the data. This way, when the data changes, you can refresh the combobox automatically. In other words, the ComboBox is simply a view of the data, not the actual data.
 
Thanks for the response.

Being a beginner to C#, I am still thinking in VB terms. Can you please explain how you would acheive this in C#?

------------------------------------------
The faulty interface lies between the chair and the keyboard.
 
Pertaining to JurkMonkey's explanation, look into the Combobox DataSource property.

--Brad
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top