Search code examples
c#arrayscombobox

How can I add array values into a combobox?


I have defined variables as string arrays. I also have a form called form4 and on this form I have 1 textbox and 1 combobox.

I have this code in a class:

public class food
{
    public string[] name1 = new string [20];
    public string[] name2 = new string [50];
    public string[] name3 = new string [40] ;
    public string[] name = new string [20];
    public string[] type = new string [15];
    //private const int total = 11;
    public int[] calories_ref;
    public int i, i2;
    public Form4 form = new Form4();


    public food(string[] Name1, string[] Name, string[] Type1, int[] kcal)
    {
        name1 = Name1;
        name = Name;
        type = Type1;
        calories_ref = kcal;
    }
    public food()
    {

    }
    private void settypes()
    {

        type[0] = "Bebidas não alcoólicas";
        type[1] = "Bebidas Alcóolicas";
        type[2] = "Fruta";
        type[3] = "Hidratos de Carbono";
        type[4] = "Peixe";
        type[5] = "Carne";
        type[6] = "Cereais";
        type[7] = "Lacticínios";
        type[8] = "Óleos/Gorduras";
        type[9] = "Leguminosas";
        type[10] = "Legumes";

        for (int i = 0; i < type.Length; i++)
        {
            form.comboBox1.Items.Add(type[i]);
        }
    }

In the settypes() method I define the various types of food, more concretly the food wheel. How can I can use these values as items in the combobox that is in form4?


Solution

  • You shouldn't be storing a Form4 object in your food class. Your code as it is creates a brand new Form4 every time the food object is created. As shown in the line:

    public Form4 form = new Form4();
    

    This won't actually be shown on screen though as you do nothing else with the form except add items to the ComboBox, which also wouldn't show on the screen.

    Even if you were to get it shown on the screen, you will still get an error similar to:

    Form4.comboBox1 is inaccessible due to its protection level
    

    This is due to the fact that the ComboBox is created internally with private access modifier. (See http://msdn.microsoft.com/en-us/library/ms173121.aspx for more details).

    What you need to do is get your existing Form4 to ask the food object to populate it's ComboBox by passing the ComboBox to a method on the food object similar to this example (in your Form4 code not your food code):

    private void Form4_Load(object sender, EventArgs e)
    {
        food f = new food(); //or however you wanted to create the object
        f.AddTypesToComboBox(this.comboBox1);
    }
    

    The AddTypesToComboBox method would be defined like this in your food object:

    public void AddTypesToComboBox(ComboBox box)
    {
        for (int i = 0; i < type.Length; i++)
        {
            box.Items.Add(type[i]);
        }
    }
    

    Also, at the moment the function won't actually add anything to the ComboBox as your type array is not being filled with data. You need to call settypes(); in the food object's constructors like this:

    public food(string[] Name1, string[] Name, string[] Type1, int[] kcal)
    {
        settypes();
        name1 = Name1;
        name = Name;
        type = Type1;
        calories_ref = kcal;
    }
    public food()
    {
        settypes();
    }
    

    You will need to remove public Form4 form = new Form4(); from your variable declaration section as well as removing the following from your settypes() method:

    for (int i = 0; i < type.Length; i++)
    {
        form.comboBox1.Items.Add(type[i]);
    }
    

    Your settypes() should only fill the data into the array, and not try and add it to the ComboBox.