Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何通过字符串数组的行填充组合框?_C#_Arrays_Winforms_Combobox - Fatal编程技术网

C# 如何通过字符串数组的行填充组合框?

C# 如何通过字符串数组的行填充组合框?,c#,arrays,winforms,combobox,C#,Arrays,Winforms,Combobox,拜托,这里怎么了 abc = labGuns.Text; // multiline label string[] arr = Regex.Split(abc, "\r\n"); x = 0; foreach (string line in arr) { MessageBox.Show(line); //works fine - shows each line of label x = x + 1; string abc = "cbGuns" + x.ToString(

拜托,这里怎么了

abc = labGuns.Text;  // multiline label
string[] arr = Regex.Split(abc, "\r\n");
x = 0; 
foreach (string line in arr)
{
    MessageBox.Show(line); //works fine - shows each line of label
    x = x + 1;
    string abc = "cbGuns" + x.ToString();
    MessageBox.Show(abc); //works fine - shows "cbGuns1", "cbGuns2"...
    foreach (Control c in panPrev.Controls)
    {
        if (c.Name == abc) // five combos named cbGuns1, cbGuns2...
        {
            c.Text = line; //doesn't work. No combo changes its text
        }
    }
}
如果我将最后一行更改为-
c.Text=“323”
,也不会发生任何事情。
所以,很明显,错误就在代码末尾附近

此代码也适用(作为测试):


尝试将c转换为DropDownList

DropDownList ddl =(DropDownList) c;<br/>
ddl.Text ="your text"
DropDownList ddl=(DropDownList)c
ddl.Text=“您的文本”
尝试将c转换为DropDownList

DropDownList ddl =(DropDownList) c;<br/>
ddl.Text ="your text"
DropDownList ddl=(DropDownList)c
ddl.Text=“您的文本”
如果我理解正确,您希望在组合框中添加一行,而不是选择其中当前的一行,对吗?为此,您不需要将Combobox的文本值设置为字符串,您需要向Combobox添加一个新的ComboboxItem,如下所示:

c.Items.Add(line);
而不是

c.Text = line;
让我知道这是否有效

编辑: 好的,因为您正试图更改组合框的选定项,所以我只需编写

c.SelectedItem = line;

如果我理解正确,您希望在组合框中添加一行,而不是选择其中当前的一行,对吗?为此,您不需要将Combobox的文本值设置为字符串,您需要向Combobox添加一个新的ComboboxItem,如下所示:

c.Items.Add(line);
而不是

c.Text = line;
让我知道这是否有效

编辑: 好的,因为您正试图更改组合框的选定项,所以我只需编写

c.SelectedItem = line;

尝试替换
c.Text=line带有
c.项目。添加(行)

添加多个项目时,最好使用
AddRange
,因此,请尝试以下方法,而不是使用
foreach
循环:

foreach (Control c in panPrev.Controls)
{
    if (c.Name.StartsWith("cbGuns"))
    {
        c.Items.AddRange(arr);
    }
}

尝试替换
c.Text=line带有
c.项目。添加(行)

添加多个项目时,最好使用
AddRange
,因此,请尝试以下方法,而不是使用
foreach
循环:

foreach (Control c in panPrev.Controls)
{
    if (c.Name.StartsWith("cbGuns"))
    {
        c.Items.AddRange(arr);
    }
}

如果您的控件是combo,并且您希望在没有项目列表的情况下设置“Text”属性,那么您的combo应该具有
DropDownStyle=DropDown

如果您的控件是combo,并且您希望在没有项目列表的情况下设置“Text”属性,那么您的combo应该具有
DropDownStyle=下拉列表

每一行实际上都是一个组合项。我想换衣服SelectedItem@Buena:那么您根本不填充组合框,因为它们已经填充。哦,在这种情况下,您将需要编辑SelectedItem属性。如果改为使用C.SelectedItem=line,应该可以。我将相应地编辑我的答案。但我有另一个代码(在BTN单击上)可以更改组合框。文本通常BTW System.Windows.Forms.Cotrol没有SelectedItemeach的DAFINTION。每一行实际上是一个组合项。我想换衣服SelectedItem@Buena:那么您根本不填充组合框,因为它们已经填充。哦,在这种情况下,您将需要编辑SelectedItem属性。如果改为使用C.SelectedItem=line,应该可以。我将相应地编辑我的答案。但我有另一个代码(在BTN单击上)可以更改组合框。文本正常BTW System.Windows.Forms.Cotrol没有SelectedItem的dafinition