我尝试将Form2中的数据检索到,但它在Form2中没有显示任何内容: 当我点击更新按钮时,它只显示空白表格2:
Form1我在做:
ProductList products = new Prod
private void button2_Click(object sender, EventArgs e)
{
int i = this.lstPerson.SelectedIndex;
Product product = products[i];
Form2 frmUpdate = new Form2();
product = frmUpdate.GetNewProduct();
products[i] = product;
products.Add(product);
FillProductListBox();
}
private void FillProductListBox()
{
lstPerson.Items.Clear();
foreach (Product p in products)
lstPerson.Items.Add(p.GetDisplayText("\t"));
}
在产品类别中:
public class Product
{
private string firstname;
private string lastname;
private string email;
public Product()
{
}
public Product(string firstname, string lastname, string email)
{
this.Firstname = firstname;
this.Lastname = lastname;
this.Email = email;
}
public string Firstname
{
get
{
return firstname;
}
set
{
firstname = value;
}
}
public string Lastname
{
get
{
return lastname;
}
set
{
lastname = value;
}
}
public string Email
{
get
{
return email;
}
set
{
email = value;
}
}
public virtual string GetDisplayText(string sep)
{
return Firstname + sep + Lastname + sep + Email;
}
}
在书类中: 私人双重评估。 private double assignmentGrade;
public Book()
{
}
public Book(string firstname, string lastname, double assessmentGrade,double assignmentGrade,
string email) : base(firstname, lastname, email)
{
this.AssessmentGrade = assessmentGrade;
this.AssignmentGrade = assignmentGrade;
}
public double AssignmentGrade
{
get
{
return assignmentGrade;
}
set
{
assignmentGrade = value;
}
}
public double AssessmentGrade
{
get
{
return assessmentGrade;
}
set
{
assessmentGrade = value;
}
}
public override string GetDisplayText(string sep)
{
return base.GetDisplayText(sep) + " (" + AssessmentGrade + ")" + AssignmentGrade;
}
和软件类一样。
在Form2中
private Product product = null;
public Product GetNewProduct()
{
this.ShowDialog();
return product;
}
private void btnSave_Click(object sender, System.EventArgs e)
{
if (IsValidData())
{
if (rbBook.Checked)
product = new Book(txtName.Text, txtLName.Text,
Convert.ToDouble(txtAssessGrade.Text), Convert.ToDouble(txtAssigGrade.Text), txtEmail.Text);
else
product = new Software(txtName.Text, txtLName.Text,
Convert.ToInt32(txtAssessGrade.Text), txtEmail.Text);
this.Close();
}
}
我没有告诉我任何事情它只是在列表框中添加更多数据而不是更新数据而不是在Form2中检索数据。您能告诉我该怎么做吗?
答案 0 :(得分:0)
//in Form2
public partial class Form2 : Form
{
private Product product = null;
public Form2()
{
InitializeComponent();
}
public Product GetNewProduct()
{
return product;
}
private void btnSave_Click(object sender, System.EventArgs e)
{
if (IsValidData())
{
if (rbBook.Checked)
{
product = new Book(txtName.Text, txtLName.Text,
Convert.ToDouble(txtAssessGrade.Text), Convert.ToDouble(txtAssigGrade.Text), txtEmail.Text);
this.Close();
}
}
}
private bool IsValidData()
{
return true;
}
}
//Modify in Form1:
public partial class Form1 : Form
{
private ArrayList products = new ArrayList();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frmUpdate = new Form2();
frmUpdate.ShowDialog();
Product product = frmUpdate.GetNewProduct();
products.Add(product);
FillProductListBox();
}
private void FillProductListBox()
{
lstPerson.Items.Clear();
foreach (Product p in products)
lstPerson.Items.Add(p.GetDisplayText("\t"));
}
}