通过当前选择的ListBox项目声明另一个Formbox的数据源



是否已经询问,

我想知道如何通过当前选择的列表框对象声明文本框或列表框的数据源。

so box 1具有项目列表,每个选择都在下一个框中打印出有关它们的更多信息(ItemInfo(

这是我正在使用的代码:

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
           if (listBox1.SelectedIndex != null)
            {
                listBox2.DataSource = ItemStorage._?_?_?_?_(listBox1.SelectedItem);
            }
        }
public class ItemObject
        {
            public string ItemName { get; set; }
            public string ItemInfo { get; set; }
            public ItemObject(string itemName, string itemInfo)
            {
                ItemName = itemName;
                ItemInfo = itemInfo;
            }

             public override string ToString()
            {
                return ItemName;
            }
        }

列表名称为 ItemStorage,我要打印到另一个框的元素是 ItemInfo

我找到了我要寻找的答案:

我选择了一个RichTextbox,而不是使用ListBox,这是我使用的代码:

if (listBox1.SelectedIndex != null)
        {
            try { var Information = ((ItemObject)listBox1.SelectedItem).ItemInfo;
                richTextBox2.Text = Information;
            }
            catch( Exception ex)
            {
            }
        }

这只为我提供了我想拥有的所选项目的信息,因此我的列表框中的所有项目总计都有完整的信息,而选择仅向我显示了来自选择的一个信息。

最新更新