I have an Experiment class. I created some instances of this class and populate combobox with these objects. I used DisplayMember and ValueMember properties. The population is OK, but when I read the selectedValue from the combobox, it gives me NullReferenceException.
我有一个实验课。我创建了这个类的一些实例,并用这些对象填充组合框。我使用了DisplayMember和ValueMember属性。人口还可以,但是当我从组合框中读取selectedValue时,它会给我NullReferenceException。
Here is my code:
这是我的代码:
public ref class ABC
{
ABC( Experiment^ exp )
{
this->exp = exp;
this->name = this->exp->getName();
}
property Experiment^ Exp
{
Experiment^ get()
{
return this->exp;
}
}
property String^ Name
{
String^ get()
{
return this->name;
}
}
Experiment^ exp;
String^ name;
}
Experiment^ e1;
this->combobox->Items(gcnew ABC(e1));
this->combobox->DisplayMember = "Name";
this->combobox->ValueMember = "Exp";
this->combobox->SelectedIndex = 0;
Experiment^ e2 = (Experiment^)(this->combobox->SelectedValue); // nullReferenceException
1 个解决方案
#1
1
I dont know why but when I exchange the following line
我不知道为什么,但当我交换以下行时
Experiment^ e2 = (Experiment^)(this->combobox->SelectedValue);
with this line
用这条线
Experiment^ e2 = ((ABC^)(this->combobox->SelectedItem))->Exp;
it is Ok, this fixed the problem.
没关系,这解决了这个问题。
#1
1
I dont know why but when I exchange the following line
我不知道为什么,但当我交换以下行时
Experiment^ e2 = (Experiment^)(this->combobox->SelectedValue);
with this line
用这条线
Experiment^ e2 = ((ABC^)(this->combobox->SelectedItem))->Exp;
it is Ok, this fixed the problem.
没关系,这解决了这个问题。