Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 476
Vitor Ferreira Avatar asked Aug 23 '26 09:08

Vitor Ferreira


1 Answers

You can add an array of strings using method AddRange(array[]) from comboBox.

form.comboBox1.Items.AddRange(type);
like image 92
JO53JUL10 Avatar answered Aug 26 '26 00:08

JO53JUL10



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!