重写 ToString() 并添加到 ListBox C#

发布于 2024-11-03 06:33:10 字数 485 浏览 1 评论 0原文

谁能解释一下:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

吹梦到西洲 2024-11-10 06:33:10

为此,您必须禁用格式设置:

listBox1.FormattingEnabled = false;

看起来如果启用格式设置,它会执行一些魔术,结果并不总是应该的......

For that to work you have to disable formatting:

listBox1.FormattingEnabled = false;

It looks like if formatting is enabled, its doing some magic tricks and the result is not always what it should be...

烟柳画桥 2024-11-10 06:33:10

将 ListBox 上的 DisplayMember 设置为 Test 类型的属性。

listBox1.DisplayMember = "Name";

要解决您的问题,请将名为“Name”的属性添加到 Type 中,并在 getter 中调用 ToString。

public class Test : List<Int32>
{
    public String Name { get { return this.ToString(); } }

    public override string ToString()
    {
        return "Test";
    }
}

Set the DisplayMember on the ListBox to the property of the Test type.

listBox1.DisplayMember = "Name";

To solve your problem, add a Property called "Name" to Type and in the getter call ToString.

public class Test : List<Int32>
{
    public String Name { get { return this.ToString(); } }

    public override string ToString()
    {
        return "Test";
    }
}
赠意 2024-11-10 06:33:10

是不是一定要这样:

listBox1.Items.Add(test.ToString());

我假设您希望列表框包含字符串类型?

但不确定这是否正确,我还没有测试过。

Does it not have to be like this:

listBox1.Items.Add(test.ToString());

I assume you want your listbox to contain a string type?

Not sure if that is correct though, I haven't tested it.

过期情话 2024-11-10 06:33:10

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();).

绳情 2024-11-10 06:33:10

我也遇到过这个(再次感谢曼吉!)。
我有这样的事情:

public override string ToString()
    {
        return  MessageText;
    }

其中 MessageText 是几个其他文本字段中的一个,并且工作正常。后来我把它改成了这个

public override string ToString()
    {
        return string.Concat("[", MessageTime.ToString("yyyy-MM-dd HH:mm:ss.fffff"), "] ", MessageText);
    }

,它仍然只会返回MessageText字段内容(拔毛时间)。有趣的是,我设置的 ListBox 上用于将所选项目复制到剪贴板的上下文菜单确实使用了完整的 ToString 覆盖。

我个人认为 FormattingEnabled 属性应该默认为 false 而不是 true,我发现我经常被试图变得聪明的 IDE(或控件设置)所困扰。

///编辑:打字错误(一定要记住不要用肘部打字!

I came across this too (and another thanks there Manji!).
I had something like this:

public override string ToString()
    {
        return  MessageText;
    }

Where MessageText was a text field amongst several others, and it worked fine. Later I changed it to this

public override string ToString()
    {
        return string.Concat("[", MessageTime.ToString("yyyy-MM-dd HH:mm:ss.fffff"), "] ", MessageText);
    }

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文