如何在列表框中显示拆分行到文本框
问题描述:
jkdshfkfslkjfhasfhadifusafjklsadhfipausdfhsadufhafwef
jkdshfkfslkjfhasfhadifusafjklsadhfipausdfhsadufhafwef
答
不要拆分任何东西。请注意,您可以将任何类型的对象存储为列表框项。但是在UI中显示了什么?无论方法ToString
返回。
因此,创建一个包含两部分数据的类或结构:显示列表项时在UI中显示的字符串(让我们称这个成员为UiText
),以及其他一些数据(让我们说,另一个字符串,但是在文本框中使用的那个,让我们称这个成员值
)。现在,要使此类型的实例正确显示为列表项,请覆盖object.ToString
以返回UiText
。
使用刚才定义的类/结构类型的实例填充列表框。处理它的事件SelectedIndexChanged
: http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.selectedindexchanged.aspx [ ^ ]。
在此事件的处理程序中,获取所选的项目对象,将其强制转换为您刚定义的类或结构类型,获取其值
(参见上文) ,并将其分配到您的文本框''文本
属性。
就是这样。-SA
Don''t split anything. Pay attention that you can store the object of any type as list box item. But that is displayed in the UI? Whatever the methodToString
returns.
Therefore, create a class or a structure having two parts of data: the string to show in UI when a list item is displayed (let''s call this memberUiText
), and some other data (let''s say, another string, but the one used in a text box, let''s call this memberValue
). Now, to make the instance of this type showing as the list item properly, overrideobject.ToString
to returnUiText
.
Populate the list box with the instances of the class/struct type you just defined. Handle it''s eventSelectedIndexChanged
: http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.selectedindexchanged.aspx[^].
In your handler of this event, take the selected item object, cast it to the class or structure type you just defined, get itsValue
(see above), and assign it to your text box''sText
property.
That''s it.—SA