简体   繁体   English

访问第二个表单中的列表并将用户输入添加到列表中

[英]Accessing a list from a second form and adding user input to the list

I have a list called "studentInfo" and I am trying to allow the user to go to a form I created called "addRecord" and type in a student code and a student mark and once they hit submit the data is then added to the list in form1 and then also the data gets added to the listbox in form1. 我有一个名为“studentInfo”的列表,我试图允许用户转到我创建的名为“addRecord”的表单并键入学生代码和学生标记,一旦他们点击提交,数据就会被添加到列表中在form1中,然后数据也被添加到form1的列表框中。

I don't know how to go about this since I am new to C# so any guidance will be appreciated. 我不知道如何解决这个问题,因为我是C#的新手,所以任何指导都将不胜感激。

My list is created like this 我的列表是这样创建的

public static List<string> studentInfo = new List<string>();

Then once my form1 loads I read in from a CSV file to populate the listbox 然后,一旦我的form1加载,我从CSV文件读入以填充列表框

private void Form1_Load(object sender, EventArgs e)
        {
            string lines, studentNumber, studentMark;  

            StreamReader inputFile; 

            inputFile = File.OpenText("COM122.csv");  

            while (!inputFile.EndOfStream)  
            {
                lines = inputFile.ReadLine(); 
                studentInfo = lines.Split(',').ToList(); 
                studentNumber = studentInfo[0];  
                studentMark = studentInfo[1];  
                lstMarks.Items.Add(studentNumber + " : " + studentMark);  
            }
            inputFile.Close();
        }

I want the user to go to the addRecord form, enter in a student code and student mark, then hit submit and then the program asks the user to confirm that the data they have entered is correct and then the user is directed back to form1 and the data will be in the listbox. 我希望用户转到addRecord表单,输入学生代码和学生标记,然后点击提交,然后程序要求用户确认他们输入的数据是正确的,然后用户被定向回到form1和数据将在列表框中。

Alrighty, in Form addRecord, we create two properties to hold the data to be retrieved from the main Form. 好吧,在Form addRecord中,我们创建了两个属性来保存从主Form中检索的数据。 This should only be done if addRecord returns DialogResult.OK. 只有在addRecord返回DialogResult.OK时才应该这样做。 I've added in two buttons, one for cancel and one for OK. 我添加了两个按钮,一个用于取消,一个用于OK。 These simply set DialogResult to their respective results. 这些只是将DialogResult设置为各自的结果。 The property values for the two values to be returned are also set in the OK button handler: 要返回的两个值的属性值也在OK按钮处理程序中设置:

public partial class addRecord : Form
{

    public addRecord()
    {
        InitializeComponent();
    }

    private string _Student;
    public string Student
    {
        get
        {
            return this._Student;
        }
    }

    private string _Score;
    public string Score
    {
        get
        {
            return this._Score;
        }
    }

    private void btnOK_Click(object sender, EventArgs e)
    {
        this._Student = this.tbStudent.Text;
        this._Score = this.tbScore.Text;
        this.DialogResult = DialogResult.OK;
    }

    private void btnCancel_Click(object sender, EventArgs e)
    {
        this.DialogResult = DialogResult.Cancel;
    }

}

Now, back in Form1, here is an example of showing addRecord with ShowDialog(), then retrieving the stored values when OK is returned: 现在,回到Form1,这是一个使用ShowDialog()显示addRecord的示例,然后在返回OK时检索存储的值:

public partial class Form1 : Form
{

    public static List<string> studentInfo = new List<string>();

    // ... other code ...

    private void btnAddRecord_Click(object sender, EventArgs e)
    {
        addRecord frmAddRecord = new addRecord();
        if (frmAddRecord.ShowDialog() == DialogResult.OK)
        {
            studentInfo.Add(frmAddRecord.Student);
            studentInfo.Add(frmAddRecord.Score);
            lstMarks.Items.Add(frmAddRecord.Student + " : " + frmAddRecord.Score);
        }
    }

}

--- EDIT --- ---编辑---

Here's a different version of addRecord that doesn't allow you to hit OK until a valid name and a valid score have been entered. 这是addRecord的另一个版本,在输入有效名称和有效分数之前不允许您点击OK。 Note that the property has been changed to int, and the TextChanged() events have been wired up to ValidateFields(). 请注意,该属性已更改为int,并且TextChanged()事件已连接到ValidateFields()。 The OK button is also initially disabled: 确定按钮最初也被禁用:

public partial class addRecord : Form
{

    public addRecord()
    {
        InitializeComponent();

        this.tbStudent.TextChanged += ValidateFields;
        this.tbScore.TextChanged += ValidateFields;
        btnOK.Enabled = false;
    }

    private string _Student;
    public string Student
    {
        get
        {
            return this._Student;
        }
    }

    private int _Score;
    public int Score
    {
        get
        {
            return this._Score;
        }
    }

    private void ValidateFields(object sender, EventArgs e)
    {
        bool valid = false; // assume invalid until proven otherwise
        // Make sure we have a non-blank name, and a valid mark between 0 and 100:
        if (this.tbStudent.Text.Trim().Length > 0)
        {
            int mark;
            if (int.TryParse(this.tbScore.Text, out mark))
            {
                if (mark >= 0 && mark <= 100)
                {
                    this._Student = this.tbStudent.Text.Trim();
                    this._Score = mark;
                    valid = true; // it's all good!
                }
            }
        }
        this.btnOK.Enabled = valid;
    }

    private void btnOK_Click(object sender, EventArgs e)
    {
        this.DialogResult = DialogResult.OK;
    }

    private void btnCancel_Click(object sender, EventArgs e)
    {
        this.DialogResult = DialogResult.Cancel;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM