C# method listbox or combobox as parameter

Saga 426 Reputation points
2020-12-03T21:07:44.897+00:00

Hi all,

First time at MS Q&A

I am using VS2015 and C#

I have code that loads data into a listbox. No problem, it work fine. Now I need a second method that loads the same data into a combobox. I recall that there was a way to pass the list control and use either a listbox or combibox in the method, but I can't locate it.

Is there a way I can have just one method that accepts either a combobox or listbox and loads the data into it? Thanks! Saga

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,897 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Viorel 117K Reputation points
    2020-12-03T21:24:33.83+00:00

    Probably the simplest approach is Data Binding. For example:

    List<string> items = new List<string> { "Item1", "Item2", "Item3" };
    listBox1.DataSource = items;
    comboBox1.DataSource = items;
    

    If you want to use an explicit loop, then try something like this:

    List<string> items = new List<string> { "Item1", "Item2", "Item3" };
    FillListBoxOrComboBox( listBox1, items );
    FillListBoxOrComboBox( comboBox1, items );
    

    where

    static void FillListBoxOrComboBox( dynamic control, IEnumerable<string> items )
    {
     foreach( var i in items )
     {
     control.Items.Add( i );
     }
    }
    

    Also note that both of ListBox and ComboBox are derived from ListControl.

    1 person found this answer helpful.
    0 comments No comments

  2. Karen Payne MVP 35,421 Reputation points
    2020-12-03T22:28:50.683+00:00

    Hello @Saga ,

    If you want both controls to be independent when traversing. You need to clone the list.

    Add this class to your project.

    static class Extensions  
    {  
        public static IList<T> Clone<T>(this IList<T> listToClone) where T : ICloneable =>   
            listToClone.Select(item => (T)item.Clone()).ToList();  
    }  
    

    Then to populate the controls.

    var firstNamesList = new List<string> { "Karen", "Jane", "Mike" };  
    listBox1.DataSource = firstNamesList;  
    comboBox1.DataSource = firstNamesList.Clone();  
    
    1 person found this answer helpful.
    0 comments No comments

  3. Saga 426 Reputation points
    2020-12-04T00:06:40.143+00:00

    Thank you both for your replies.

    After more research, it seems that what I want done is not possible, but after tweaking the routine, I was able to successfully load a combo box or list box.

    Here is the code:

    private void LoadCoData(ListControl MyList)
    {
        //Routine to load companies into list.
    
        //Clear data source.
        MyList.DataSource = null;
    
        //Clear list.  *****
        //MyList.Items.Clear();
    
        //Build the SQL to load the names.
        string sSQL = "select CompanyId,CompanyName from Companies where Co_Group = 17";
    
        //Get names.
        DataTable dtData = cDBFun.ExecSQLCommand(cDBFun.dbOptions.Query, sSQL).Tables[0];
    
        //Load the list.
        MyList.DisplayMember = "CompanyName";
        MyList.ValueMember = "CompanyId";
        MyList.DataSource = dtData;
    
    }
    

    My problem was with the Items.Clear() statement that's commented out. The Items collection was not found in the ListControl, so it was generating an exception. However, this statement is not necessary because the list clears whenever it gets a datasource.

    I am interested in the techniques that you both provided and will be looking into these for future use, Again, I appreciate your help. Saga

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.