【问题标题】:Create list in one form, show list values in listbox at second form以一种形式创建列表,在第二种形式的列表框中显示列表值
【发布时间】:2015-12-15 21:25:05
【问题描述】:

这是我目前正在尝试创建的图像。

基本上我想写下姓名、姓氏、年龄并将新学生添加到列表中。 之后我希望学生显示在列表框中,但列表框是另一种形式。 我的问题是我无法进入另一种形式的列表框。我是否还必须以新形式再创建一个列表?

我的代码:
Form1:

 public class Students
    {
        public string Name { get; set; }
        public string Surname { get; set; }
        public int Age { get; set; }
        public override string ToString()
        {
            return string.Format("{0} \t\t {1} \t\t {2}", Name, Surname, Age);
        }
    }
    public partial class Form1 : Form
    {
        List<Students> stud = new List<Students>();
        public Form1()
        {
            this.IsMdiContainer = true;
            InitializeComponent();
        }

        private void addNewStudentToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2(stud);
            f2.MdiParent = this;
            f2.Show();
        }

        private void showAllStudentsInfoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form3 f3 = new Form3(stud);
            f3.MdiParent = this;
            f3.Show();
        }
    }

Form2:

public partial class Form2 : Form
    {
        public Form2(List<Students> stud)
        {
            InitializeComponent();
            studnew = stud;
        }
        List<Students> studnew;

        private void button1_Click(object sender, EventArgs e)
        {
            Students student = new Students();
            student.Name = textBox1.Text;
            student.Surname = textBox2.Text;
            student.Age = Convert.ToInt32(textBox3.Text);
            studnew.Add(student);
        }
    }

Form3:

public partial class Form3 : Form
{
    public Form3(List<Students> stud)
    {
        InitializeComponent();
        studnewf3 = stud;
    }
    List<Students> studnewf3;

    private void Form3_Load(object sender, EventArgs e)
    {
        listBox1.Items.Add("Name \t\t Surname \t\t Age");
    }

    private void button1_Click(object sender, EventArgs e)
    {
        listBox1.Items.Add(student); 
    }
}

【问题讨论】:

  • 有什么问题?只需将传递的列表存储到 Form3 构造函数中的表单字段中,就像您在 Form2 中所做的那样
  • 看看Application.Openforms
  • 这是作业吗?
  • 考前练习。不是真正的家庭作业。
  • 但据我了解,您只想显示Form3 列表中的所有学生,对吗?在Form3_Load 中添加foreach (var student in studnewf3) listBox1.Items.Add(student);

标签: c# winforms listbox


【解决方案1】:

而不是传递一个列表传递一个BindingList。通过这种方式,您可以在将新元素添加到 BindingList 时利用自动通知

public partial class Form1 : Form
{
    List<Students> stud = new List<Students>();
    BindingList<Students> bsStud;
    public Form1()
    {
        this.IsMdiContainer = true;
        InitializeComponent();
        bsStud = new BindingList<Students>(stud);
    }

    private void addNewStudentToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2(bsStud);
        f2.MdiParent = this;
        f2.Show();
    }

    private void showAllStudentsInfoToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Form3 f3 = new Form3(bsStud);
        f3.MdiParent = this;
        f3.Show();
    }

在 Form2 中:

public partial class Form2 : Form
{
    BindingList<Students> bsStudnew;

    public Form2(BindingList<Students> bs)
    {
        InitializeComponent();
        bsStudnew = bs;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Students student = new Students();
        student.Name = textBox1.Text;
        student.Surname = textBox2.Text;
        student.Age = Convert.ToInt32(textBox3.Text);
        bsStudnew.Add(student);
    }
}

在表格3中

public partial class Form3 : Form
{
    public Form3(BindingList<Students> bsStud)
    {
        InitializeComponent();
        listBox1.DataSource = bsStud;
    }
    ....

【讨论】:

    【解决方案2】:

    您可以在form1上声明form2并获取学生列表。 在 Form1 上:

    public Form2 f2;
    private void     showAllStudentsInfoToolStripMenuItem_Click(object       sender, EventArgs e)
        {
            Form3 f3 = new Form3(f2.ListOfStudent);
            f3.MdiParent = this;
            f3.Show();
        }
    

    在表格 2 上公开学生名单。

    Public List<Students> studnew;
    

    或者您可以在 form2 上创建一个在学生添加时发生的事件,并在 form1 上创建一个对 Form2 上该事件的响应:

    Public event EventHandler<Student> StudentAdded;
    public void SayToForm1ThatStudentHasAdded (Student s)
    {
    EventHandler <Student> sa;
    if (sa!=null)
    {sa (this,s);}
    }
    

    和其他事件一样使用这个事件:在 Form1:

    private void addNewStudentToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2(stud);
            f2.MdiParent = this;
            f2.StudentAdded+=ResponseToEvent;
            f2.Show();
        }
    
        void ResponseToEvent  (object sender,Student e)
     {
          List of Student. Add (  e);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-27
      • 1970-01-01
      • 2016-02-09
      • 1970-01-01
      • 2014-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多