Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当项目添加到ComboBox时,为什么重写的ToString()不返回我想要的内容?_C#_Combobox_Overriding_Tostring - Fatal编程技术网

C# 当项目添加到ComboBox时,为什么重写的ToString()不返回我想要的内容?

C# 当项目添加到ComboBox时,为什么重写的ToString()不返回我想要的内容?,c#,combobox,overriding,tostring,C#,Combobox,Overriding,Tostring,加载表单时,我看到combobox有两个名称为空的项,而不是“asd”:/ 但是,如果我在公共类中重写ToString(),而不是从任何东西派生,这将起作用: public partial class TestConrol : UserControl { public TestConrol() { InitializeComponent(); } public override string ToString() { re

加载表单时,我看到combobox有两个名称为空的项,而不是“asd”:/
但是,如果我在公共类中重写ToString(),而不是从任何东西派生,这将起作用:

public partial class TestConrol : UserControl
{
    public TestConrol()
    {
        InitializeComponent();
    }

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

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        TestConrol tc1 = new TestConrol();
        comboBox1.Items.Add(tc1);

        TestConrol tc2 = new TestConrol();
        comboBox1.Items.Add(tc2);
    }
}

之后,我在combobox中看到“bla bla bla”

在控件中创建一个属性,并将combobox的DisplayMember映射到该属性,它应该可以工作。

使用
comboBox1.Items.Add(tc1.ToString())而不是
组合框1.Items.Add(tcl)

我试图理解源代码(!)。这不是对
ToString()
的简单调用

有一个
internal
System.Windows.Forms.Formatter
在做一些事情。它最终创建了一个转换器。这大致相当于说:

public class TestClass
{
    public override string ToString()
    {
        return "bla bla bla";
    }
}

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        TestClass tcl = new TestClass();
        comboBox1.Items.Add(tcl);
    }
}
其中
tc1
是您问题中的
testcontrol
。现在,如果我们使用不实现任何接口的
TestClass
tcl
,这将为我们提供一个最终调用
ToString()
的转换器

但在本例中,我们使用了
tc1
,它是一个。因此,我们的
conv
成为
System.ComponentModel.ComponentConverter
。它使用
i组件的
站点
。当我们说:

var conv = System.ComponentModel.TypeDescriptor.GetConverter(tc1.GetType());
站点
为空,我们将得到您在组合框中看到的空字符串
”。如果有一个
站点
,它就会使用它的
名称

要演示这一点,请将以下内容放入
TestControl
实例构造函数中:

string result = conv.ConvertTo(tc1, typeof(string));
其中
DummySite
类似于:

public TestConrol()
{
    InitializeComponent();
    Site = new DummySite(); // note: Site is public, so you can also
                            // write to it from outside the class.
                            // It is also virtual, so you can override
                            // its getter and setter.
}
这对我很有用:

class DummySite : ISite
{
    public IComponent Component
    {
        get { throw new NotImplementedException(); }
    }

    public IContainer Container
    {
        get { throw new NotImplementedException(); }
    }

    public bool DesignMode
    {
        get { throw new NotImplementedException(); }
    }

    public string Name
    {
        get
        {
            return "asd";  // HERE'S YOUR TEXT
        }
        set
        {
            throw new NotImplementedException();
        }
    }

    public object GetService(Type serviceType)
    {
        return null;
    }
}

UserControl
中,添加一个属性,并将其称为
FriendlyName

comboBox1.FormattingEnabled = false
然后将
组合框的
DisplayMember
属性设置为“FriendlyName”

namespace project
{
   public partial class CustomUserControl : UserControl
   {
      public CustomUserControl()
      {
         InitializeComponent();
      }

      public String FriendlyName { get => "Custom name"; }
   }
}

对我来说,这是一个非常干净的解决方案,gut告诉我这是一种预期的解决方法。

你在哪里调用
ToString()
查看此站点,以获取示例/解释添加项目时combobox应该调用它,如果它适用于普通类,您是否可以对其中一个组合框属性执行
受保护的覆盖,从e.argument参数访问它?
对不起,我不明白您在说什么:(我看到了同样的行为。它看起来像是
System.Windows.Forms.ComboBox的内部代码
使用
ToString
覆盖的对象不是
System.Windows.Forms.UserControl
。我有点希望能够将comboBox1.SelectedItem强制转换回类实例…P.S.-1不是我的:P仍然感谢您的回答,如果newbieThanks问这个问题可能会有帮助,我正在尝试…但由于奇怪的原因,当我单击在属性列表中选择DisplayingMember时,它是空的,所以无需选择。哦,我只需要在那里键入,而不是选择。现在它可以工作了,谢谢!哦,哦,哦,但是如何处理CheckedListBox呢?:D没有DisplayingMember,但相同的问题有:嗯,很奇怪,我在PropertyList中看不到这一点。谢谢你解释这一点。你的答案值得接受,因为它确实回答了“为什么重写ToString()…”啊,我想这就是为什么他们在of
组件中说。ToString
不应该被覆盖。我感谢你的水平和理解。谢谢你富有洞察力的回答!
myComboBox.DisplayMember = "FriendlyName";