C# 如何将数组值添加到组合框中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18543299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:32:19  来源:igfitidea点击:

How can I add array values into a combobox?

c#arrayscombobox

提问by Vitor Ferreira

I have defined variables as stringarrays. I also have a form called form4and on this form I have 1 textbox and 1 combobox.

我已将变量定义为string数组。我还有一个名为的表单form4,在这个表单上我有 1 个文本框和 1 个组合框。

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?

settypes()方法中,我定义了各种类型的食物,更具体地说是食物轮。如何将这些值用作组合框中的项目form4

采纳答案by Josh

You shouldn't be storing a Form4object in your foodclass. Your code as it is creates a brand new Form4every time the foodobject is created. As shown in the line:

您不应该Form4food类中存储对象。Form4每次food创建对象时,您的代码都会创建一个全新的代码。如行所示:

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.

这实际上不会显示在屏幕上,因为除了将项目添加到 之外,您对表单没有做任何其他事情ComboBox,这也不会显示在屏幕上。

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 ComboBoxis created internally with private access modifier. (See http://msdn.microsoft.com/en-us/library/ms173121.aspxfor more details).

这是因为ComboBox是使用私有访问修饰符在内部创建的。(有关更多详细信息,请参阅http://msdn.microsoft.com/en-us/library/ms173121.aspx)。

What you need to do is get your existing Form4to ask the foodobject to populate it's ComboBoxby passing the ComboBoxto a method on the foodobject similar to this example (in your Form4code not your foodcode):

您需要做的是通过将 传递给类似于此示例的对象上的方法(在您的代码中而不是您的代码中),让您现有Form4food对象来填充它:ComboBoxComboBoxfoodForm4food

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 AddTypesToComboBoxmethod would be defined like this in your foodobject:

AddTypesToComboBox方法将在您的food对象中像这样定义:

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 ComboBoxas your typearray is not being filled with data. You need to call settypes();in the foodobject's constructors like this:

此外,目前该函数实际上不会向 中添加任何内容,ComboBox因为您的type数组未填充数据。你需要调用settypes();food对象的构造是这样的:

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:

您需要public Form4 form = new Form4();从变量声明部分中删除,并从settypes()方法中删除以下内容:

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.

settypes()应该只将数据填充到数组中,而不要尝试将其添加到ComboBox.

回答by Patrick Geyer

Not at my computer at the moment, but this should do it:

目前不在我的电脑上,但应该这样做:

    foreach(var type in type[])
    {
        form.comboBox1.Items.Add(type);
    }

回答by ismellike

Here is a void you can use if you want to go beyond just one array.

如果您想超越一个数组,可以使用这里的 void。

public void AddToCombo(Array array, ComboBox c)
{
  foreach(var a in array)
  {
    c.Items.Add(a);
  }
}

回答by Brett Sanderson

If you want the items to be in the ComboBox just set the array as its datasource. You don't need to loop through the array and add the items one by one. It's a lot less code than the other solutions here.

如果您希望项目位于 ComboBox 中,只需将数组设置为其数据源。您不需要遍历数组并一项一项地添加项目。与这里的其他解决方案相比,它的代码要少得多。

    public void SetTypes()
    {
        comboBox1.DataSource = new[]{
            "Bebidas n?o alcoólicas",
            "Bebidas Alcóolicas",
            "Fruta",
            "Hidratos de Carbono",
            "Peixe",
            "Carne",
            "Cereais",
            "Lacticínios",
            "óleos/Gorduras",
            "Leguminosas",
            "Legumes"
        };
    }

回答by JO53JUL10

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

您可以使用AddRange(array[])from 的方法添加字符串数组comboBox

form.comboBox1.Items.AddRange(type);

回答by Dave Beauchesne

As simple as this:

就这么简单:

public void yourMethodName() {        
    yourComboBoxName.removeAllItems();
    for (YourObjectName object: yourArrayListName) {
        yourComboBoxName.addItem(object.getWhatYouWanaGet());
    }        
}

Your remove the actual item from the list than you add the item you want to add :)

您从列表中删除实际项目而不是添加要添加的项目:)