Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.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_Visual Studio_Null_Listbox - Fatal编程技术网

C# 将填充的数组值显示到列表框中,并可以选择删除它们

C# 将填充的数组值显示到列表框中,并可以选择删除它们,c#,arrays,visual-studio,null,listbox,C#,Arrays,Visual Studio,Null,Listbox,好的,在Stackoverflow上一些很棒的家伙的帮助下,我创建了这样的数组 public partial class Form1 : Form { string[] Brands = new string[10]; int brandNo;} public Form1() { InitializeComponent(); Brands[0] = "Yamaha"; //ok Brands[1] = "Suzu

好的,在Stackoverflow上一些很棒的家伙的帮助下,我创建了这样的数组

public partial class Form1 : Form    
{
    string[] Brands = new string[10];
    int brandNo;}

    public Form1()
    {
       InitializeComponent();
       Brands[0] = "Yamaha"; //ok
       Brands[1] = "Suzuki"; //ok
       Brands[2] = "Harley"; //ok
       Brands[3] = "Kawasaki"; //ok
       brandNo = 4;
    }

    private void buttonAddbrand_Click(object sender, EventArgs e)
    {
       if (brandNo >= 10)
          return; //cannot add more brand

       Brands[brandNo++] = textBoxMerk.Text;
       var Merk = Brands
       listBoxMotoren.Items.Clear();

       listBoxMotoren.Items.AddRange(Merk);
有了这段代码,我想在我的列表框中显示数组的填充值。但我得到了以下错误:

值不能为空


非常感谢您的帮助。

我很确定这段代码甚至都不会编译

   int brandNo;} <- "}" ???

   var Merk = Brands <- ";" missing and (var Merk) not needed
和库尔斯

if (listBoxMotoren != null && Brands  != null)
   listBoxMotoren.Items.AddRange(Brands);

如果listboxMotoren为空,则表单尚未初始化,只要listboxMotoren是表单控件

,首先,请注意将花括号放在何处,以确保可以编译代码

  public partial class Form1 : Form
   {
   string[] Brands = new string[10];
     int brandNo;
    public Form1()
    {
        InitializeComponent();
        Brands[0] = "Yamaha"; //ok
        Brands[1] = "Suzuki"; //ok
        Brands[2] = "Harley"; //ok
        Brands[3] = "Kawasaki"; //ok
        brandNo = 4;
        listBoxMotoren.DataSource=Brands;//asssiign the current list to //listbox
    }

    private void buttonAddbrand_Click(object sender, EventArgs e)
    {
        if (brandNo >= 10)
      return; //cannot add more brand

   Brands[brandNo++] = textBoxMerk.Text;

   listBoxMotoren.DataSource = null; //make the list empty
   listBoxMotoren.DataSource = Brands;// assgin it new list
    }
public partial class Form1 : Form    
{
    string[] Brands = new string[10];
    int brandNo;} //<- off-placed
最后,如果您想随意删除品牌项目,您可能需要在
表单1
中使用另一个
控件作为您想要删除的品牌项目的输入。但请注意,这可能会“破坏”您的项目顺序,因此您可能需要“重新排序”您的项目

现在,假设您使用
NumericUpDown
来删除,并使用
buttonDeletebrand
触发删除,那么您应该这样做

private void buttonDeletebrand_Click(object sender, EventArgs e) {
    int indexToDelete = (int)numericUpDownMotorenNumberDeleted.Value; //note the casting to (int)
    if (indexToDelete < 0 || indexToDelete >= brandNo || brandNo <= 0) //can only delete index no [0] to [brandNo-1], and if the brand no > 0
        return; //invalid index
    for (int i = indexToDelete; i < brandNo - 1; ++i)
        Brands[indexToDelete] = Brands[indexToDelete + 1]; //resequencing
    Brands[brandNo - 1] = string.Empty; //removes the last element after resequencing
    listBoxMotoren.DataSource = null; //remember the cheapest and dirtiest trick?
    listBoxMotoren.DataSource = Brands;
    --brandNo; //reduce the brandNo by 1
}
private void按钮删除品牌\u单击(对象发送者,事件参数e){
int indexToDelete=(int)numericUpDownMotorenNumberDeleted.Value;//注意转换为(int)
如果(索引删除<0 | |索引删除>=brandNo | | brandNo 0
return;//索引无效
对于(int i=indexToDelete;i
总的来说,您需要将它们结合起来:

public partial class Form1 : Form {
    string[] Brands = new string[10];
    int brandNo;
    public Form1() {
        InitializeComponent();
        Brands[0] = "Yamaha";
        Brands[1] = "Suzuki";
        Brands[2] = "Harley";
        Brands[3] = "Kawasaki";
        brandNo = 4;
        listBoxMotoren.DataSource = Brands;
    }

    private void buttonAddbrand_Click(object sender, EventArgs e) {
        if (brandNo >= 10)
            return;

        Brands[brandNo++] = textBoxMerk.Text;
        listBoxMotoren.DataSource = null;
        listBoxMotoren.DataSource = Brands;
    }

    private void buttonDeletebrand_Click(object sender, EventArgs e) {
        int indexToDelete = (int)numericUpDownMotorenNumberDeleted.Value;
        if (indexToDelete < 0 || indexToDelete >= brandNo || brandNo <= 0)
            return;
        for (int i = indexToDelete; i < brandNo - 1; ++i)
            Brands[indexToDelete] = Brands[indexToDelete + 1];
        Brands[brandNo - 1] = string.Empty;
        listBoxMotoren.DataSource = null;
        listBoxMotoren.DataSource = Brands;
        --brandNo;
    }
}
公共部分类表单1:表单{
字符串[]品牌=新字符串[10];
因特布兰诺;
公共表格1(){
初始化组件();
品牌[0]=“雅马哈”;
品牌[1]=“铃木”;
品牌[2]=“哈雷”;
品牌[3]=“川崎”;
brandNo=4;
listBoxMotoren.DataSource=品牌;
}
私有无效按钮随机单击(对象发送者,事件参数e){
如果(品牌号>=10)
返回;
品牌[brandNo++]=textBoxMerk.Text;
listBoxMotoren.DataSource=null;
listBoxMotoren.DataSource=品牌;
}
私有无效按钮删除品牌\单击(对象发送者,事件参数e){
int indexToDelete=(int)numericUpDownMotorenNumberDeleted.Value;

如果(indexToDelete<0 | | indexToDelete>=brandNo | | brandNo为什么需要单独的数组来保存品牌?按钮处理程序可以像
listBoxMotoren.Items.Add(textBoxMerk.Text);
一样简单。问题是:给定代码中有语法错误(缺少
}
),请确保向我们显示包含错误的真实代码,提供完整的错误消息,并指定所有详细信息,如它发生在哪一行,是编译时还是运行时,等等。是的,很抱歉,没有正确复制。语法错误已消失。我收到此错误消息:System.Wi中发生“System.ArgumentNullException”类型的未处理异常ndows.Forms.dll您的问题将提供相关的详细信息。您忘记告诉发生错误的行(在此处添加注释
//error
)。再次感谢。但不幸的是,它不起作用。当我单击Addbrand按钮时,它没有崩溃或其他什么,只是没有在列表框中显示任何内容。奇怪的是,我的代码与您的代码完全相同。@Needsomehelp您是认真的吗?但是Addbrand与您以前的相同,否?请检查再次。如果可能,使用调试器
private void按钮adbrand\u单击(对象发送者,事件参数e){if(brandNo>=10)return;//无法添加更多品牌[brandNo++]=textBoxMerk.Text;listBoxMotoren.DataSource=null;//实现此目的最便宜、最肮脏的技巧listBoxMotoren.DataSource=Brands;//保持样式,使用
DataSource`容纳新数据}`如果你的brandNo不超过10,这一定会让我丢失一些东西。我复制了你的代码。程序运行正常。编译没有问题。但是当我单击add按钮时:列表框=(可能是因为你
复制了
我的代码…它将被
重新制作
(为了更好地理解!),而不是复制的
…您是否在设计器中处理您的
按钮和单击事件?
private void buttonDeletebrand_Click(object sender, EventArgs e) {
    int indexToDelete = (int)numericUpDownMotorenNumberDeleted.Value; //note the casting to (int)
    if (indexToDelete < 0 || indexToDelete >= brandNo || brandNo <= 0) //can only delete index no [0] to [brandNo-1], and if the brand no > 0
        return; //invalid index
    for (int i = indexToDelete; i < brandNo - 1; ++i)
        Brands[indexToDelete] = Brands[indexToDelete + 1]; //resequencing
    Brands[brandNo - 1] = string.Empty; //removes the last element after resequencing
    listBoxMotoren.DataSource = null; //remember the cheapest and dirtiest trick?
    listBoxMotoren.DataSource = Brands;
    --brandNo; //reduce the brandNo by 1
}
public partial class Form1 : Form {
    string[] Brands = new string[10];
    int brandNo;
    public Form1() {
        InitializeComponent();
        Brands[0] = "Yamaha";
        Brands[1] = "Suzuki";
        Brands[2] = "Harley";
        Brands[3] = "Kawasaki";
        brandNo = 4;
        listBoxMotoren.DataSource = Brands;
    }

    private void buttonAddbrand_Click(object sender, EventArgs e) {
        if (brandNo >= 10)
            return;

        Brands[brandNo++] = textBoxMerk.Text;
        listBoxMotoren.DataSource = null;
        listBoxMotoren.DataSource = Brands;
    }

    private void buttonDeletebrand_Click(object sender, EventArgs e) {
        int indexToDelete = (int)numericUpDownMotorenNumberDeleted.Value;
        if (indexToDelete < 0 || indexToDelete >= brandNo || brandNo <= 0)
            return;
        for (int i = indexToDelete; i < brandNo - 1; ++i)
            Brands[indexToDelete] = Brands[indexToDelete + 1];
        Brands[brandNo - 1] = string.Empty;
        listBoxMotoren.DataSource = null;
        listBoxMotoren.DataSource = Brands;
        --brandNo;
    }
}