我正在尝试从列表框唯一的颜色中选择一个项目并将其添加到列表框“事物”中。我不想删除项目lbxUniqueColors。现在,列表框Things有9个项目,但是当我运行程序向lbxThings添加颜色时,计数器没有变化,它保持为9。到目前为止,这是我的代码
在这里,我将颜色添加到lbxThings并添加计数器,并将不同的颜色添加到lbxUniqueColors:
private void Form1_Load(object sender, EventArgs e)
{
var myList =
new List<string> { "red", "blue", "red", "green", "yellow", "yellow"
,"purple","violet","orange" };
lbxThings.Items.AddRange(myList.ToArray());
lblCounter.Text = $"Number of Things = {lbxThings.Items.Count}";
var UniqueItems =
(from item in myList select item).Distinct();
lbxUniqueColors.Items.AddRange(UniqueItems.ToArray());
lblUniqueColors.Text = $"Number of Unique Things = {lbxUniqueColors.Items.Count}";
}
在这里为lbxUniqueColors列表框添加标签:
private void lbxUniqueColors_SelectedIndexChanged(object sender, EventArgs e)
{
lblPickedColor.Text =
$"You picked {lbxUniqueColors.SelectedItem}";
}
在这里,我添加按钮以将颜色从唯一颜色添加到lbxThings:
private void bttnADD_Click(object sender, EventArgs e)
{
lbxThings.Items.Add(lbxUniqueColors.SelectedItem);
lblCounter.Text = $"Number of Things = {lbxThings.Items.Count}";
}
答案 0 :(得分:0)