【发布时间】:2013-04-08 18:00:21
【问题描述】:
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);
}
}
加载表单时,我看到组合框有两个名称为空的项目,而不是“asd”:/
但是,如果我在公共类中覆盖 ToString(),而不是从任何东西派生,则此方法有效:
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);
}
}
之后我在组合框中看到“bla bla bla”
【问题讨论】:
-
你在哪里打电话
ToString()看这个网站的例子/解释msdn.microsoft.com/en-us/library/ms173154%28VS.80%29.aspx -
添加项目时应该由组合框调用,就像它适用于普通类一样
-
你可以做一个
Protected override on one of the comboboxes properties, accessing it from the e.argument(s) params? -
对不起,我不明白你在说什么:(
-
我看到了同样的行为。看起来
System.Windows.Forms.ComboBox的内部代码仅对不是System.Windows.Forms.UserControl的对象使用ToString覆盖。
标签: c# combobox overriding tostring