【问题标题】:c# using multiple listboxc# 使用多个列表框
【发布时间】:2015-12-23 00:03:38
【问题描述】:

我有 2 个列表框和 2 个文本框,我需要它做的是,如果我单击列表框 1 中的一个项目,它会在列表框 2 中显示 2 个或更多项目,如果我单击列表框 2 中的一个项目,则会出现一个操作,它将出现在文本框 1 和文本框 2 中, 前任。圆珠笔=10,笔记本=20

“listBox1”中包含的项目是“笔”和“笔记本”。如果我点击笔记本一个项目将显示在列表框 2:1,2 然后如果我点击“1”,文本框 1.text=20 因为笔记本是 20*1=20

【问题讨论】:

  • 您要查找的是名为 SelectedIndexChanged 的​​事件
  • 你能给我一些使用 selectedindexchanged 的​​例子吗?天呐! :D
  • Google 是您的朋友。有数百万个例子......

标签: c# listbox listboxitem operation


【解决方案1】:

所以,我只是要为您的项目创建一个枚举。

public enum ListBoxItemThing
{
    Pen = 10, Notebook = 20
}

然后我会将这些添加到表单构造函数中的“listBox1”中。

public Form1()
{
    InitializeComponent();
    foreach(ListBoxItemThing item in Enum.GetValues(typeof(ListBoxItemThing)))
    {
        listBox1.Items.Add(item);
    }
}

然后使用此程序对 textBox1 进行计算:

private void Calculate()
{
    int a = (int)(listBox1.SelectedItem as ListBoxItemThing);
    int b = int.Parse(listBox2.SelectedItem.ToString());
    textBox1.Text = (a * b).ToString();
}

【讨论】:

    【解决方案2】:

    我认为您可以使用字典存储listBox1 和listBox2 的信息。 key 是 listBox1 中的一个项目,value 是 listBox2 中的列表项目。当您单击 listBox1 中的一个项目 => 调用 listBox1 的 SelectedIndexChanged 事件 => 获取您选择的项目(假设 = value1)=> 在字典中查找 key = value1 => 您将在 listBox2 中获得列表项目(假设 = listItems)= > 将 listItems 添加到 listBox2 => 当您单击 listBox2 中的项目时,调用 listBox2 的 SelectedIndexChanged 事件 => 更新 textbox1 和 textbox2 的值 = 在 listBox1 和 listBox2 中选择了项目。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-18
      • 2013-12-26
      • 2014-10-18
      • 1970-01-01
      • 2019-04-15
      • 2011-09-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多