How do I get at the listbox item's “key” in c# winforms app?

前端 未结 6 998
南旧
南旧 2020-12-20 20:49

I am writing a winforms app in which a user selects an item from a listbox and edits some data that forms part of an associated object. The edits are then applied from the o

6条回答
  •  甜味超标
    2020-12-20 21:23

    Okay so the answer came as a result of Andy's answer, hence my upvoting that answer.

    But when I created a little class and tried to cast the listitem into that class the program threw an exception.

    Revealingly the exception told me that the program could not cast a DictionaryEntry into a class of the type I had defined.

    So I deleted the proxy class and reframed the request thus:

    DictionaryEntry de = (DictionaryEntry)listbox.SelectedItem;
    string htKey = de.Key.ToString();

    And it's all good.

    Bizarrely simple answer in the end. Thanks for the hint Andy.

提交回复
热议问题