重写 ToString() 并添加到 ListBox C#
谁能解释一下:
public class Test : List<int>
{
public override string ToString()
{
return "My ToString";
}
}
如果我实例化它并将其添加到 Windows Form
上的 ListBox
控件,它会显示“Collection”而不是“My ToString”。
Test test = new Test();
listBox1.Items.Add(test);
我认为添加到 Items
只会调用我的类的 ToString()
。当然,以下工作按预期进行
MessageBox.Show(test.ToString());
Can anyone explain this:
public class Test : List<int>
{
public override string ToString()
{
return "My ToString";
}
}
If I instantiate this and add it to a ListBox
control on a Windows Form
, it displays "Collection" rather than "My ToString".
Test test = new Test();
listBox1.Items.Add(test);
I thought the add to Items
would just call my class's ToString()
. The following works as expected of course
MessageBox.Show(test.ToString());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
为此,您必须禁用格式设置:
看起来如果启用格式设置,它会执行一些魔术,结果并不总是应该的......
For that to work you have to disable formatting:
It looks like if formatting is enabled, its doing some magic tricks and the result is not always what it should be...
将 ListBox 上的 DisplayMember 设置为 Test 类型的属性。
要解决您的问题,请将名为“Name”的属性添加到 Type 中,并在 getter 中调用 ToString。
Set the DisplayMember on the ListBox to the property of the Test type.
To solve your problem, add a Property called "Name" to Type and in the getter call ToString.
是不是一定要这样:
我假设您希望列表框包含字符串类型?
但不确定这是否正确,我还没有测试过。
Does it not have to be like this:
I assume you want your listbox to contain a string type?
Not sure if that is correct though, I haven't tested it.
ListBox 中的项目是对象的集合,而不是字符串。
请参阅 MSDN:ListBox.ObjectCollection.Add 方法
因此,您必须在前端或后端添加实例作为字符串(例如:
listBox1.Items.Add(test.ToString());
)在列表框中,您必须调用 ToString(例如:listBox1.Items[0].ToString();
)。The items in a ListBox are a collection of objects, not strings.
See MSDN: ListBox.ObjectCollection.Add Method
Therefore you either have to add the the instance as a string (ex:
listBox1.Items.Add(test.ToString());
) on the front end or on the backend when looking at the listbox you have to call ToString (ex:listBox1.Items[0].ToString();
).我也遇到过这个(再次感谢曼吉!)。
我有这样的事情:
其中 MessageText 是几个其他文本字段中的一个,并且工作正常。后来我把它改成了这个
,它仍然只会返回MessageText字段内容(拔毛时间)。有趣的是,我设置的 ListBox 上用于将所选项目复制到剪贴板的上下文菜单确实使用了完整的 ToString 覆盖。
我个人认为 FormattingEnabled 属性应该默认为 false 而不是 true,我发现我经常被试图变得聪明的 IDE(或控件设置)所困扰。
///编辑:打字错误(一定要记住不要用肘部打字!
I came across this too (and another thanks there Manji!).
I had something like this:
Where MessageText was a text field amongst several others, and it worked fine. Later I changed it to this
And it would still return just MessageText field contents (hair pulling time). Interesting thing was that a context menu on the ListBox I had set up to copy selected items to the Clipboard, did use the full ToString override.
Personally I think the FormattingEnabled property should default to false rather than true, I find I often get caught out by the IDE (or control settings) trying to be smart.
///Edit: Typo (must remember not to type with elbows!