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

}

Book クラスの場合: private double assessmentGrade; プライベート二重代入Grade;

    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 に答える