我使用以下get set
方法创建了一个windows窗体应用程序。name, email
和phone
是硬编码的,因此可以通过创建对象
public string Name { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public Dictionary<String, String> facilities { get; set; }
Rating r = new Rating(); //object creating
r.Name = txtName.Text; //get value from textbox and assing to property
r.Email = txtEmail.Text;
r.Phone = txtPhone.Text;
然而,对于工具,comboBox用于数据来自txt文件
int locationX = 143;
int locationY = 200;
string line;
System.IO.StreamReader file =
new System.IO.StreamReader(@"facilities.txt");
while ((line = file.ReadLine()) != null)
{
// Remove the extra ','
string comboName = line.Substring(0, line.Length - 1);
ComboBox comboBox = new ComboBox();
comboBox.Name = comboName;
comboBox.Items.AddRange(new object[] { 1, 2, 3, 4 });
comboBox.Location = new Point(locationX, locationY);
this.tabPage1.Controls.Add(comboBox);
Label label = new Label();
label.Text = comboName;
label.Location = new Point(0, locationY);
this.tabPage1.Controls.Add(label);
locationY += 50;
}
file.Close();
}
如何将其数据存储在字典中?我尝试使用相同的概念
ComboBox parking= (ComboBox)this.Controls.Find("parking facility", true)[0];
//MessageBox.Show(parking.Text);
r.facilities.Add("parking facility", parking.Text);
但是得到了System。NullReferenceException: '对象引用未设置为对象的实例。'的误差
可枚举对象必须初始化。
r.facilities = new Dictionary <String,String>();
r.facilities.Add ("parking facility", parking.Text);