[英]Declaring the datasource of another formBox, by the currently selected listBox item
抱歉,如果已經有人問過這個,
我想知道如何通過當前選擇的listBox對象聲明文本框或列表框的數據源。
因此,第一個框具有項目列表,每個選擇都將打印到具有有關它們的更多信息的下一個框( 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
。
我找到了我想要的答案:
我沒有使用listBox,而是選擇了richTextBox,這是我使用的代碼:
if (listBox1.SelectedIndex != null)
{
try { var Information = ((ItemObject)listBox1.SelectedItem).ItemInfo;
richTextBox2.Text = Information;
}
catch( Exception ex)
{
}
}
這使我只能從想要擁有的所選項目中獲取信息,因此我的列表框中包含所有項目的完整信息,而選擇項僅向我顯示了所選一個項目的信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.