好的,所以在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);
使用此代码,我想在列表框中显示数组的填充值。但是我收到以下错误:
值不能为null。
非常感谢帮助。
答案 0 :(得分:1)
我很确定这段代码甚至无法编译....
int brandNo;} <- "}" ???
var Merk = Brands <- ";" missing and (var Merk) not needed
锁定此代码我猜问题是在 检查
if (listBoxMotoren != null && listBoxMotoren.Items.Any())
listBoxMotoren.Items.Clear();
和cource
if (listBoxMotoren != null && Brands != null)
listBoxMotoren.Items.AddRange(Brands);
如果listboxMotoren为null,则表单尚未初始化,因为listBoxMotoren是表单控件
答案 1 :(得分:1)
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
}
答案 2 :(得分:1)
首先,请注意放置花括号的位置,以确保可以编译代码。
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
}
最后,如果您想要随意删除品牌商品,则可能需要在Control
中另外Form1
作为输入,以便您可以选择删除品牌中的哪个商品。但请注意,这可能会“破坏”您的物品序列,因此您可能需要对物品进行“重新排序”。
现在,假设你使用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;
}
}