如何在C#Visual 2010中使用Button_Click事件处理程序填充数组



我正试图从一个文本框接收用户输入,并将其发送到我的类中名为Employee的数组中。

我不确定下面的代码是否正确,但看起来是正确的,因为我没有编译错误
但是,当我按下按钮时,员工的姓名和号码仍在文本框中。我想让这个应用程序做以下事情:

  1. 接收员工的姓名和身份证号码
  2. 将它们发送到我的"Employee"类中的数组中,并将其作为名称发送给我的数组
  3. 我希望清除文本框,以便输入新名称

这可能吗?还是我要求太多了?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
    namespace Company_Employees
    {
        class Employee
        {
            private const int NAME = 100;
            private static string[] employee = new string[NAME];
            private const int NUMBER = 100;
            private static int[] iD = new int[NUMBER];
            public Employee()  n// This is a null Constructor
            {
                employee[NAME] = null;
                iD[NUMBER] = 0;
            }

            public Employee(string name, int number) //  This is my overloaded constructor that receive these arguments from my main form.
            {
                for (int index = 0; index < employee.Length; index++)
                {
                    name = employee[index];
                }

                for (int index = 0; index < iD.Length; index++)
                {
                     number = iD[index];    
                }
           }

            public static int getemployeeNumber ()
            {
                return iD[NUMBER];
            }
            public static string getemployeeName()
            {
                return employee[NAME];
            }
        }
    }

这是我的主窗体,它有button_click事件处理程序。我希望用户输入员工姓名和身份证号码。每次用户单击按钮将信息发送到我的"EmployeeClass"时,文本框都会被清除,以便输入新的输入。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Company_Employees
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string employeeNames;
            int eNumbers;
            employeeNames = eName.Text;
            eNumbers = int.Parse( eNum.Text );
            Employee chart = new Employee(employeeNames, eNumbers);
            for ( int index = 0; index < 100; index++)
            {
                index = eNumbers;
            }
        }
    }
}            

Per@Palatiner的评论,让你知道我为什么要把你的东西改成这个,是因为你把非常简单的东西过于复杂了。更不用说,你拥有的代码永远不会工作。根据您的代码,您将始终更新同一个数组项,因为您显式地分配给位于常量位置100的数组项。

您所需要做的就是创建一个跟踪要保存的两个属性NameId的对象,以及一个保存这些对象集合的列表。数组不会动态调整大小,而列表会动态调整大小。有了它,您就不需要自己管理收藏的大小。它还提供了对许多LINQ扩展方法的访问。



我会创建一个员工类,如下所示:

public class Employee
{
    public string Name { get; set; }
    public int Id { get; set; }
    public Employee(string name, int id)
    {
        this.Name = name;
        this.Id = id;
    }
}


然后我会创建一个CreateEmployee方法,如下所示:

private void CreateEmployee()
{
    // Get textbox values.
    string name = eName.Text;
    int id;
    int.TryParse(eNum.Text, out id);
    // Validate the values to make sure they are acceptable before storing.
    if (this.EmployeeValuesAreValid(name, id))
    {
        // Values are valid, create and store Employee.
        this.Employees.Add(new Employee(name, id));
        // Clear textboxes.
        eName.Text = string.Empty;
        eNum.Text = string.Empty;
    }
}


EmployeeValuesAreValid方法可以简单如下:

private bool EmployeeValuesAreValid(string name, int id)
{
    return !String.IsNullOrEmpty(name) && id > 0;
}


按钮内单击,您只需调用CreateEmployee:

private void button1_Click(object sender, EventArgs e)
{
    this.CreateEmployee();
}



把所有东西放在一起,你会得到:

public partial class Form1 : Form
{
    public class Employee
    {
        public string Name { get; set; }
        public int Id { get; set; }
        public Employee(string name, int id)
        {
            this.Name = name;
            this.Id = id;
        }
    }
    public List<Employee> Employees { get; set; }
    public Form1()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        this.CreateEmployee();
    }
    private bool EmployeeValuesAreValid(string name, int id)
    {
        return !String.IsNullOrEmpty(name) && id > 0;
    }
    private void CreateEmployee()
    {
        // Get textbox values.
        string name = eName.Text;
        int id;
        int.TryParse(eNum.Text, out id);
        // Validate the values to make sure they are acceptable before storing.
        if (this.EmployeeValuesAreValid(name, id))
        {
            // Values are valid, create and store Employee.
            this.Employees.Add(new Employee(name, id));
            // Clear textboxes.
            eName.Text = string.Empty;
            eNum.Text = string.Empty;
        }
    }
}

最新更新