【发布时间】:2014-11-28 05:38:59
【问题描述】:
所以基本上我有两个课程,汽车和秘密汽车。
class Cars
{
public string First;
public string Last;
public string Number;
public Cars(string first, string last , string number)
{
First = first;
Last = last;
Number = number;
}
public Cars()
{
}
public override string ToString()
{
return this.First + ", " + this.Last + ", " + this.Number;
}
}
class SecretCars : Contact
{
public SecretCars (string first, string last , string number) : base(first, last , number)
{
First = first;
Last = last;
Number = number;
}
public SecretCars() : base()
{}
public override string ToString()
{
return this.First + ", " + this.Last + ", " + "Secret cars";
}
}
在 Form1.cs 中,我有一个 listbox1、一个 textBox1、一个 button1 和另外三个 textBox(textbox2、textbox3、textbox4)。
无论您在 textBox1 中输入什么内容都会在 listBox1 中打印,接下来我要做的是在我留下的三个文本框(textbox2、textbox3、textbox4)中打印我拥有的三个变量(First、Last、Number),每一个值。
我应该使用以下代码:
Cars selectCar =(Cars)listBox1.SelectedItem;
textBox2.Text = selectCar.First;
textBox3.Text = selectCar.Last;
textBox4.Text = selectCar.Number;
选择项目时出现此错误
WindowsFormsApplication1.exe 中出现“System.InvalidCastException”类型的未处理异常
附加信息:无法将“System.String”类型的对象转换为“WindowsFormsApplication1.Contact”类型。
所以我的问题是我怎样才能做到这一点? 谢谢。
【问题讨论】:
-
不,它没有用,但是谢谢。
-
您在使用时遇到了什么错误?