用包含另一个表单上的值的类填充一个表单中的列表框

本文关键字:表单 一个 列表 填充 另一个 包含 | 更新日期: 2023-09-27 18:01:20

我对编程很陌生,我在窗体之间发送信息时遇到了很多麻烦。

我的现状:

我有两个窗体和一个类。我的第一个表单只包含一个列表框,而我的第二个表单包含几个带有OK按钮的文本框。我想做的是输入信息到form2的文本框,点击确定,并从每个文本框填充我的form1的列表框的所有信息。来自form2的信息应该全部放在form1的列表框的一行上。

现在我要做的是将form2的文本框中的信息添加到我的自定义类中,然后将该自定义类传递给form1中的列表框。我将非常感谢任何帮助。下面是我到目前为止的代码:

Form2:

public partial class frmProperties : Form
{
    private string empVar;
    public string EmployeeNew
    {
        get { return empVar; }
        set { empVar = value; }
    }
    public frmProperties()
    {
        InitializeComponent();
    }
    private void btnCancel_Click(object sender, EventArgs e)
    {
        Close();
    }
    private void btnOk_Click(object sender, EventArgs e)
    {
        string empVar;
        double salaryToDouble = Convert.ToDouble(txtSalary.Text);
        Employee testEmploy = new Employee(txtFirstName.Text, txtLastName.Text, txtEmpType.Text, salaryToDouble);
        empVar = Convert.ToString(testEmploy);
    }
}

Form1:

public partial class Form1 : Form
{
    //public vars for file access
    FileStream output;
    StreamReader fileReader;
    StreamWriter fileWriter;
    public Form1()
    {
        InitializeComponent();
    }
    private void openToolStripMenuItem_Click(object sender, EventArgs e)
    {
        OpenFileDialog fileChooser = new OpenFileDialog();
        fileChooser.Title = "Choose an employee file to open";
        fileChooser.Filter = "Text Files (*.txt)|*.txt";
        DialogResult result = fileChooser.ShowDialog();
        //if cancel
        if (result == DialogResult.Cancel)
        {
            return;
        }
        string strFileName = fileChooser.FileName;
        try
        {
            //open file for read access
            output = new FileStream(strFileName, FileMode.Open, FileAccess.Read);
            fileReader = new StreamReader(output);
            //var to hold the read record
            string strInputLine;
            string[] fields;
            //loop to get records and break into fields
            while (fileReader.EndOfStream != true)
            {
                //read record
                strInputLine = fileReader.ReadLine();
                //split the record
                fields = strInputLine.Split(',');
                //Check for empty space
                if (!string.IsNullOrEmpty(strInputLine))
                {
                    double salaryToDouble = Convert.ToDouble(fields[3]);
                    lstBoxEmployees.Items.Add(new Employee(fields[0], fields[1], fields[2], salaryToDouble));
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            //close resources
            fileReader.Close();
            output.Close();
        }
    }
    private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
    {
        //create dialog
        SaveFileDialog fileChooser = new SaveFileDialog();
        fileChooser.Title = "Choose a Save Location";
        fileChooser.Filter = "Text Files (*.txt)|*.txt";
        //open dialog, get result
        DialogResult result = fileChooser.ShowDialog();
        //if cancel
        if (result == DialogResult.Cancel)
        {
            return;
        }
        //get file name from dialog
        string strFileName = fileChooser.FileName;
        try
        {
            //save via filestream
            //open the new file for write access
            output = new FileStream(strFileName, FileMode.OpenOrCreate, FileAccess.Write);
            fileWriter = new StreamWriter(output);
            //take the text from listbox and write it to a file
            fileWriter.WriteLine(lstBoxEmployees);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            //close resources
            fileWriter.Close();
            output.Close();
        }
    }
    private void addNewToolStripMenuItem_Click(object sender, EventArgs e)
    {
        MenuOpen();
    }
    //Properties form method.
    private void MenuOpen()
    {
        frmProperties newEmployee = new frmProperties();
        DialogResult result = newEmployee.ShowDialog();
        if (newEmployee.ShowDialog() == DialogResult.OK)
        {
            lstBoxEmployees.Items.Add(newEmployee.EmployeeNew);
        }
    }
    private void toolStripButton1_Click(object sender, EventArgs e)
    {
        MenuOpen();
    }
    private void lstBoxEmployees_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        if (lstBoxEmployees.SelectedItem != null)
            if (lstBoxEmployees.SelectedItem.ToString().Length != 0)
                MenuOpen();
    }
    private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
    {
        object toRemove = lstBoxEmployees.SelectedItem;
        lstBoxEmployees.Items.Remove(toRemove);
    }
}

自定义类:

class Employee
{
    private string firstName;
    private string lastName;
    private string empType;
    private double salary;
    //Constructor
    public Employee(string FirstName, string LastName, string EmpType, double Salary)
    {
        firstName = FirstName;
        lastName = LastName;
        empType = EmpType;
        salary = Salary;
    }
    public string EmployeeFirstName
    {
        get { return firstName; }
        set { firstName = value; }
    }
    public string EmployeeLastName
    {
        get { return lastName; }
        set { lastName = value; }
    }
    public string EmploymentType
    {
        get { return empType; }
        set { empType = value; }
    }
    public double EmployeeSalary
    {
        get { return salary; }
        set { salary = value; }
    }
    public override string ToString()
    {
        return (firstName + ", " + lastName);
    }
}

用包含另一个表单上的值的类填充一个表单中的列表框

你的代码中有很多问题。

  1. 在Form2中,btnOk_Click方法将新员工的详细信息放入本地变量(也称为empVar),而不是类变量。要解决这个问题,请从btnOk_Click方法中删除string empVar;行。

  2. 您的lstBoxEmployees应该添加Employee类型的项目,而不是string。因此,您的EmployeeNewempVar应该更改为Employee类型而不是string。并且您的btnOk_Click方法应该更改为将新员工直接分配给empVar,如下所示:

    empVar = testEmploy;
    
  3. 您的Employee类需要是公共的,如:

    public class Employee
    
  4. 在表单设计器中确保你的OK和Cancel按钮的DialogResult分别设置为OK和Cancel。

  5. 如果你正确地完成了上面的步骤4,你不需要在Cancel按钮事件处理程序中使用Close();语句。