0

以前にこれを行って完全に機能させたことがありますが、方法を思い出せません

アイテム クラスの背後に 3 つのプロパティがあります

namespace Budgeting_Program
{    
    [Serializable]
    public class Item
    {
        public string Name { get; set; }
        public double Price { get; set; }
        public string @URL { get; set; }

        public Item(string Name, string Price, string @URL)
        {
            this.Name = Name;
            this.Price = Convert.ToDouble(Price);
            this.@URL = @URL;
        }

        public override string ToString()
        {
            return this.Name;
       }
    }
}

今私の編集ウィンドウに

public Edit(List<Item> i, int index)
{
    InitializeComponent();
    itemList = i;
    updateItemList();    
    itemListBox.SetSelected(index, true);                               
}

選択したインデックスの背後にある項目データをテキスト ボックスに反映させたい。これはどのように可能ですか。使用した方法を覚えていない前に、それを行ったことを覚えています。

4

3 に答える 3

2

selectedindexchanged イベントをリスト ボックスに追加すると、selectedItem を にキャストItemできます。これで、プロパティにアクセスして、テキスト ボックスのテキスト フィールドを設定できます。

private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
   Item item = (Item)listBox1.SelectedItem;
   txtName.Text = item.Name;
   txtPrice.Text = item.Price;
   txtUrl.Text = item.Url;
}

リストボックス内の項目を更新する必要がある場合は、INotifyPropertyChangedを実装することをお勧めしますListBox Item

このコードプロジェクトの記事を確認してください

于 2013-09-19T02:55:28.850 に答える