【问题标题】:Passing names and scores to another form (C#)将名称和分数传递给另一种形式 (C#)
【发布时间】:2015-11-04 08:53:14
【问题描述】:

我正在尝试将用户输入从我的 AddNewStudent 表单传递到我的主 StudentScores 表单(我单击 StudentScores 表单上的 btnAddNew 按钮并弹出 AddNewStudent 表单。 .我输入我的值...然后按 btnOk 按钮...希望值显示在 StudentScores 列表框中)。然而,我还没有完全熟悉使用类和使用多表单程序,所以我不仅工作速度比平时慢,而且我一直很难弄清楚这一点。非常感谢任何帮助。

我已经有三个由 | 分隔的初始化名称和分数。管道字符。我正在尝试在 StudentScores 列表框中以相同格式显示任何用户输入。我想默认有这三个名字和分数。感谢您的宝贵时间。

我的学生班级

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace My_Program
{
public class Student
{
    public Student()
    {
        this.Scores = new List<int>();


    }

    public Student (string Name, List<int> Scores)
    { this.Name = Name;
        this.Scores = Scores;
    }

    public string Name
    { get;
        set;
    }

    public List<int> Scores
    { get;
      set;

    }

    public override string ToString()
    {
            string names= this.Name;

            foreach (int myScore in Scores)
            { names += "|" + myScore.ToString();


                }
        return names;
    }

    public int GetscoreTotal()
    { int sum = 0;

        foreach (int score in Scores)
        {
            sum += score;


        }
        return sum;
        }

    public int GetScoreCount()
    { return Scores.Count;
    }

    public void addScore(int Score)
    {
        Scores.Add(Score);
    }
}
}

我的主要表格又名 StudentScores 表格

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace My_Program
{

public partial class StudentScores : Form
{

    //list of students declared
    public List<Student> studentList = new List<Student>();
    List<Student> Students;

    public StudentScores()
    {
        InitializeComponent();


       // listBoxStudents.DataSource = Students;

    }

    private void StudentScore_Load(object sender, EventArgs e)
    {

        //initialized 3 students for list box upon loading program...
        Students = new List<Student>();

        Student student1 = new Student();

        student1.Name = "George Mendoza";
        student1.Scores.Add(97);
        student1.Scores.Add(71);
        student1.Scores.Add(83);
        Students.Add(student1);
        listBoxStudents.Items.Add(student1);

        Student student2 = new Student();

        student2.Name = "John Doe";
        student2.Scores.Add(99);
        student2.Scores.Add(93);
        student2.Scores.Add(97);
        Students.Add(student2);
        listBoxStudents.Items.Add(student2);

        Student student3 = new Student();

        student3.Name = "Bill Cruz";
        student3.Scores.Add(100);
        student3.Scores.Add(100);
        student3.Scores.Add(100);

        Students.Add(student3);
        listBoxStudents.Items.Add(student3);






    }

    private void btnAddNew_Click(object sender, EventArgs e)
    {
        //calls form2 aka AddNewStudent form.
        AddNewStudent frm = new AddNewStudent();
        frm.ShowDialog();

    }

    private void btnDelete_Click(object sender, EventArgs e)
    {
        //code something that will delete entries...message box is a start
      DialogResult dialog = MessageBox.Show("Are you sure you want to delete your entries?", "Message", MessageBoxButtons.YesNoCancel);

        if (dialog == DialogResult.Yes)
        {
            while (listBoxStudents.SelectedItems.Count > 0)
            {
                listBoxStudents.Items.Remove(listBoxStudents.SelectedItems[0]);

            }
        }
    }

    private void listBoxStudents_SelectedIndexChanged(object sender, EventArgs e)
    {    


    }

    private void btnExit_Click(object sender, EventArgs e)
    {
        this.Close();
    }
}
}

AddNewStudent 表格(我添加新姓名和分数并将其发送到 StudentScores 表格的表格):

using System;
using System.Collections.Generic;
using System.ComponentModel; 
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace My_Program
{
public partial class AddNewStudent : Form
{

    List<Student> Students;



    public AddNewStudent()
    {
        InitializeComponent();



    }

    public AddNewStudent (List <Student> Students)
    {
        this.Students = Students;

    }



    private void btnCancel_Click(object sender, EventArgs e)
    {
        this.Close(); 
    }

    private void btnAddScore_Click(object sender, EventArgs e)
    {
        int score = Convert.ToInt32(txtScore.Text);

        //...

    }

    private void btnOk_Click(object sender, EventArgs e)
    {
        string name = Convert.ToString(txtName.Text);

        if (name == string.Empty)
        {
            MessageBox.Show("Please enter a name before continuing.", "Entry Error.");

        }
        else

        {
           //stuck here...
        }

        }



    }

    private void btnClearScores_Click(object sender, EventArgs e)
    {
       //clears both the score and scores text box
        txtScore.Text = "";
        txtScores.Text = "";

        txtScore.Focus();

    }
}

}

【问题讨论】:

    标签: c# forms winforms class


    【解决方案1】:

    你应该;

    1. StudentScoreslistBoxStudents的访问修饰符更改为public

    2. 实现AddNewStudent 表单的构造函数以接受StudentScores 实例。将 StudentScores 实例存储在字段中。

      private StudentScores _studentScoresForm;
      public AddNewStudent(StudentScores studentScoresForm) : this()
      {
        _studentScoresForm = studentScoresForm;
      }
      
      public AddNewStudent()
      {
          InitializeComponent();
      }
      
    3. 通过传递this创建AddNewStudent表单的实例

      private void btnAddNew_Click(object sender, EventArgs e)
      {
          //calls form2 aka AddNewStudent form.
          AddNewStudent frm = new AddNewStudent(this);
          frm.ShowDialog();
      }
      

    4.在AddNewStudent表单中,当发生变化(添加新学生)并点击确定按钮时:

    Student student1 = new Student();
    student1.Name = name;
    student1.Scores.Add(score);
    _studentScoresForm.listBoxStudents.Add(student1);
    

    【讨论】:

    • 我听从了您的建议,但是,在单击 btnAddNew 按钮后,AddNewStudent 表单在运行时现在是空白的。一旦我从第 3 步中删除“this”,对话框就会重新出现。
    • 编辑它以调用默认构造函数。
    • 抱歉给您添麻烦了。应该调用 InitializeComponent() 来初始化表单上的控件。
    • 不用担心,谢谢您的帮助。它已修复空白表单错误。对于第 4 步,您会在确定按钮方法中执行此操作吗?
    • 是的,当采取行动并单击确定时,列表的任何更新都应反映到学生列表框,然后可能会关闭添加学生表单
    【解决方案2】:

    正如@Oguz 已经解释过的,您希望将StudentScores 表单作为变量传递给AddNewStudent 表单。

    这将允许您从AddNewStudent 表单访问StudentScores 表单中的任何公共属性。换句话说,您可以访问原始学生列表并向其中添加新学生。仅传递StudentScores 表单的变量引用,因此所有更改都反映在原始学生列表中。

    【讨论】:

      【解决方案3】:

      在 AddNewStudent 表单中,这个构造函数永远不会被调用:

          public AddNewStudent (List <Student> Students)
      {
          this.Students = Students;
      }
      

      所以我认为你想要做的是改用它并改变它:

         private void btnAddNew_Click(object sender, EventArgs e)
      {
          //calls form2 aka AddNewStudent form.
          AddNewStudent frm = new AddNewStudent();
          frm.ShowDialog();
      }
      

      到这里:

         private void btnAddNew_Click(object sender, EventArgs e)
      {
          //calls form2 aka AddNewStudent form.
          AddNewStudent frm = new AddNewStudent(Students);
          frm.ShowDialog();
      }
      

      现在您将您的学生列表传递给您可以对其进行更改的表单。

      【讨论】:

        【解决方案4】:

        最专业的方法是使用委托

        在 AddNewstudent 表单中创建一个包含四个必需参数的委托

        public delegate void PassControl(string a,int b,int c,int d);
        public PassControl passControl;
        

        点击 AddNewStudent 表单中的 OK 按钮

         private void button1_Click(object sender, EventArgs e)
                {
                    if (passControl != null)
                    {
                        passControl(txtName.Text,int.Parse(txt1.Text),int.Parse(txt2.Text),int.Parse(txt3.Text));
                    }
                }
        

        在学生成绩表中点击添加新按钮

          private void btnAddNew_Click(object sender, EventArgs e)
                {
                    //calls form2 aka AddNewStudent form.
                    AddNewStudent frm = new AddNewStudent();
                    frm.passControl = new C2.PassControl(this.PassData);
                    frm.ShowDialog();
        
                }
        

        以学生分数形式编写它指向的委托函数。

          private void PassData(string a,int b,int c,int d)
                {
        
                    Student student1 = new Student();
        
                    student1.Name = a;
                    student1.Scores.Add(b);
                    student1.Scores.Add(c);
                    student1.Scores.Add(d);
                    Students.Add(student1);
                    listBoxStudents.Items.Add(student1);
                }
        

        【讨论】:

        • 如何使用我上面提供的代码来解决这个问题?我还没有使用/了解代表。
        • 所以先学习代表..然后你会发现它的优点......好吧,我已经展示了另一种方法......就是这样......
        猜你喜欢
        • 2012-08-17
        • 1970-01-01
        • 1970-01-01
        • 2015-06-06
        • 1970-01-01
        • 2017-06-17
        • 1970-01-01
        • 2021-08-06
        • 2015-06-23
        相关资源
        最近更新 更多