0

我尝试检索 Form2 中的数据,但它在 Form2 中没有显示任何内容:当我单击更新按钮时,它只显示空白 Form2:

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;
    }

}

在书类:私人双评等级;私人双分配等级;

    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 中检索数据。你能告诉我该怎么做吗?

4

1 回答 1

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"));

                }    
            }
于 2012-08-18T09:33:58.660 回答