我有一个程序,在其中使用组合框导航嵌套的数据结构。这些组合框应使用数据结构中的属性作为其显示成员。
我将表单构造函数中所有组合框的成员绑定
public frmWeatherManager()
{
InitializeComponent();
cmbLocationSelect.DisplayMember = "NameCmbBoxInterface";
cmbLocationSelect.ValueMember = "YearCmbBoxInterface";
cmbYearSelect.DisplayMember = "NumCmbBoxInterface";
cmbYearSelect.ValueMember = "this";
cmbMonthSelect.DisplayMember = "IdCmbBoxInterface";
cmbMonthSelect.ValueMember = "this";
}
“ NameCmbBoxInterface”和其他类似的属性只是在这里充当私有成员的代理。
private string Name;
private string StreetName;
private string County;
private string PostCode;
private double Latitude;
private double Longitude;
private Year[] Years;
public string NameCmbBoxInterface
{
get
{
return Name;
}
set
{
Name = value;
}
}
由于老师的限制,我不得不这样做。我不允许使用任何“内置”功能。这意味着我必须自己编写getter和setter,并且不能使用.contains()之类的列表或方法。
我通过两种方式填充数据结构:
第一种方法是生成随机数据的地方。
private void btnPopulate_Click(object sender, EventArgs e)
{
Locations = new Location[3];
Locations[0] = new Location("location1", "england", "street1", "postcode1", 0.55, 2.74, year_populate("Location1"));
Locations[1] = new Location("location2", "england", "street2", "postcode2", 1.45, -0.70, year_populate("Location2"));
Locations[2] = new Location("location3", "england", "street3", "postcode3", 7.37, -1.40, year_populate("Location3"));
cmbLocationSelect.DataSource = Locations;
}
这很好,并且位置选择器组合框正确显示了“ location1”,“ location2”,“ location3”。
我还有另一个功能可以从文件中读取数据:
private void btnReadFile_Click(object sender, EventArgs e)
{
System.IO.StreamReader file = new System.IO.StreamReader(txtFilePath.Text);
//establish the number of locations to be parsed.
int locationcount = int.Parse(file.ReadLine());
//initalize locations array to the required size
Locations = new Location[locationcount + 1];
for (int i=0; i < locationcount; i++)
{
//begin location parse
//i is used to control access to location array
//extract data and sort into containers ready for insertion into data structure.
string locationName = file.ReadLine();
string streetName = file.ReadLine();
string county = file.ReadLine();
string postCode = file.ReadLine();
double latitude = double.Parse(file.ReadLine());
double longitude = double.Parse(file.ReadLine());
//establish number of years in location to be parsed
//must be done first so that location object's year array knows how large it needs to be
int yearcount = int.Parse(file.ReadLine());
//create new location object inside array.
Locations[i] = new Location(locationName, streetName, county, postCode, latitude, longitude, yearcount);
for (int R=0; R < yearcount; R++)
{
//begin year parse
string desc = file.ReadLine();
int yearid = int.Parse(file.ReadLine());
//create current year object
Year thisyear = new Year(yearid, desc);
for(int M=0; M < 12; M++)
{
//begin month parse
int monthid = int.Parse(file.ReadLine());
double maxtemp = double.Parse(file.ReadLine());
double mintemp = double.Parse(file.ReadLine());
int frostdays = int.Parse(file.ReadLine());
double rainfall = double.Parse(file.ReadLine());
double sunshine = double.Parse(file.ReadLine());
//read out useless year id line unless we are on the last loop
if (M != 11)
file.ReadLine();
//create current month object
Month thismonth = new Month(monthid, frostdays, maxtemp, mintemp, rainfall, sunshine);
//add month in question to the correct place in the year object.
thisyear.SetMonth(thismonth, M);
}
//similarly add year to location.
Locations[i].SetYear(thisyear, R);
}
}
file.Close();
//now that we are all done. Bind the data source of the combo box
cmbLocationSelect.DataSource = Locations;
}
在这种情况下,它将失败。位置选择器组合框显示Location对象的每个实例的.tostring评估。我已经知道,当找不到您想要绑定到显示值的成员时会发生这种情况。
我已经花了好几个小时了,但我看不到什么可能引起问题。我希望有人能分享一些见识。