简体   繁体   English

如何将文本框值添加到数组并仅显示填充的数组位置?

[英]How to add textbox value to array and display only the filled array places?

I have a array, its size is 100. So it is like this: 我有一个数组,它的大小是100。所以像这样:

string[] Brands = new string[100];

I also have a TextBox on my Form . 我的Form上也有一个TextBox Everytime I click the Button , I want the TextBox to store its data in the array. 每次单击Button ,我都希望TextBox将其数据存储在数组中。 After that I want the array to display the values into a ListBox . 之后,我希望数组将值显示到ListBox But, I only want the ListBox to display the actual used memory of the array. 但是,我只希望ListBox显示数组实际使用的内存。 So I have 100 places in the array, but I only want to show the used ones. 所以我在数组中有100个位置,但是我只想显示使用的位置。

Can someone please help me? 有人可以帮帮我吗?

    int ArrayPos = 0;
    string[] Brands = new string[100];

            private void Button_Click(object sender, RoutedEventArgs e)
            {

             if(arrayPos < 100)
             {
             Brands[ArrayPos] = textBox.Text;
             listbox.add(Brands[ArrayPos]);
             ArrayPos++;

             }


            };

This will allow you to add items to the array at the next position as well as checking that the array is not full. 这将使您可以在下一个位置向阵列添加项目,并检查阵列是否已满。 Also It will then add the contents of the newly filled array position into the listbox. 并且它将随后将新填充的数组位置的内容添加到列表框中。

What you're trying to do will be much easier if you use a List<string> instead. 如果您使用List<string>代替,则您尝试执行的操作会容易得多。

List<string> Brands = new List<string>();

When you click the button, you can add your element to the list like this: 单击按钮时,可以将元素添加到列表中,如下所示:

Brands.Add(textBox1.Text);

And when you want to show the number of elements (I assume that's what you mean by "actual used memory"), it's as easy as referencing the Count property: 当您要显示元素的数量时(我假设这就是您所说的“实际使用的内存”),它就像引用Count属性一样简单:

int totalBrands = Brands.Count;

If for some reason you have a constraint that forces you to use a string[100] , this won't work. 如果由于某种原因您有一个强制您使用string[100]的约束,那么它将不起作用。

According to the restriction, given that you can only use array, what you would probably need is something like this 根据限制,鉴于您只能使用数组,可能需要的是这样的内容

int index = 0; //to tell the current position of the array "pointer"
string[] Brands = new string[100];

//And on your Button click event
private void button1_Click(object sender, EventArgs e) {
    if (index < Brands.Length) {
        Brands[index] = textBox1.Text;
        listBox1.Items.Add(Brands[index++]);                
    }
}

This way you add whatever is in the textBox1 at the event button1 is clicked to the position in the array which is pointed by your index value. 这样,您将在事件button1上单击textBox1 ,将其添加到数组中由index值指向的位置。 And as long as your index is not greater than the Brands.Length you can always add item to your listBox1 by using listBox1.Items.Add . 而只要你的index是不大于大Brands.Length你可以到你随时添加项目listBox1使用listBox1.Items.Add

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM