.net - bug in displaying data selected in listbox in text box -
i using following code:
private void listbox1_mouseclick(object sender, mouseeventargs e) { txtfrom.clear(); txtsubject.clear(); txtbody.clear(); = this.listbox1.selectedindex.tostring(); int something1 = convert.toint32(something); foreach (mailmessage email in messages) { count++; if (count == something1) { txtfrom.text = email.from.tostring(); txtsubject.text = email.subject.tostring(); txtbody.text = email.body.tostring(); } }
the problem is, when select item, value of txtfrom.text,txtsubject.text, txtbody.text, not change according new value selected in list box.
the mouseclick
event not 1 want handle if you're interested in learning when selection changes. mouseclick
event going raised every time user clicks inside of control, regardless of whether make selection. , there several ways select item inside of listbox without using mouse—for example, change selection using arrow keys on keyboard. (and if that's not enough persuade you, draw attention documentation, says mouseclick
event "supports .net framework infrastructure , not intended used directly code".)
instead, you'll want switch handling selectedindexchanged
event. event raised each time selectedindex
property (or selectedindices
collection multi-select listboxes) changes, happens automatically whenever selection made/changed.
you don't have make other changes code:
private void listbox1_selectedindexchanged(object sender, eventargs e) { txtfrom.clear(); txtsubject.clear(); txtbody.clear(); = this.listbox1.selectedindex.tostring(); int something1 = convert.toint32(something); foreach (mailmessage email in messages) { count++; if (count == something1) { txtfrom.text = email.from.tostring(); txtsubject.text = email.subject.tostring(); txtbody.text = email.body.tostring(); } } }
Comments
Post a Comment