以一种形式创建列表,以第二种形式在列表框中显示列表值

时间:2015-12-15 21:25:06

标签: c# winforms listbox

enter image description here

以下是我此刻想要制作的图像。

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

我的代码:
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();
        }
    }

窗体2:

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

2 个答案:

答案 0 :(得分:1)

而不是传递List传递BindingList。通过这种方式,您可以在将新元素添加到BindingList

时利用自动通知
window.location = " http://10.0.2.2/external/index.html ";

在Form2中:

.controller('MyCtrl', function($scope) {

//example mock http call
function getHttpData() {
  return [{
    "id": "1",
    "body": "sample text",
    "read": "false",
    "checked": "true"
  }, {
    "id": "2",
    "body": "sample text",
    "read": "true",
    "checked": "false"
  }];
}

function getData() {
  var data = getHttpData();

  //process array as required
  data.forEach(function(value) {
    value.id = parseInt(value.id);
    value.read = JSON.parse(value.read);
  });

  return data;
}

$scope.data = getData();
});

在form3中

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

答案 1 :(得分:0)

您可以在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);
}