好的,在 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);
使用这段代码,我想在我的列表框中显示数组的填充值。但是我收到以下错误:
value can not be null.
非常感谢您的帮助。
最佳答案
首先,请注意大括号的放置位置,以确保可以编译代码。
public partial class Form1 : Form
{
string[] Brands = new string[10];
int brandNo;} //<- off-placed
其次,由于您首先将数组作为您的 DataSource
(而不是使用 Items.Add
或 Items.AddRange
到您的 listBoxMotoren
,如果你在 listBoxMotoren
中添加或删除你的项目,它可能是一致的。
private void buttonAddbrand_Click(object sender, EventArgs e) {
if (brandNo >= 10)
return; //cannot add more brand
Brands[brandNo++] = textBoxMerk.Text;
listBoxMotoren.DataSource = null; //the cheapest and dirtiest trick to do this
listBoxMotoren.DataSource = Brands; //Maintaining the style, use `DataSource` to accommodate new data
}
最后,如果您想随意删除品牌项目,您可能需要在 Form1
中添加另一个 Control
作为您要删除的品牌项目的输入有选择地删除。但请注意,这可能会“破坏”您的项目序列,因此您可能需要“重新排序”您的项目。
现在,假设您使用 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
}
总的来说,您需要将所有这些结合起来:
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;
}
}
关于c# - 将我填充的数组值显示到列表框中并可选择删除它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34741882/