简体   繁体   English

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

[英]How can I fill in an Array Using a Button_Click Event Handler in C# Visual 2010

I am trying to receive user input from a textbox and send it into an array that is in my class that is named Employee. 我试图从文本框中接收用户输入,并将其发送到我的班级中名为Employee的数组中。

I am not sure if the code below is correct, but it seems like it is, because I have no compiling errors. 我不确定下面的代码是否正确,但是似乎是正确的,因为我没有编译错误。
However, when I press the button the employee name and number are still in the textbox. 但是,当我按下按钮时,员工姓名和编号仍在文本框中。 What I would like for this application to do is the following: 我希望该应用程序执行以下操作:

  1. receive an employee's name and ID number; 接收员工的姓名和身份证号;
  2. send them to my array that is in my " Employee" class, and as a name is sent to my array; 将它们发送到我的“ Employee”类中的数组,并作为名称发送到我的数组;
  3. I want the text box to clear in order for a new name to be entered. 我希望清除文本框,以便输入新名称。

Is this possible? 这可能吗? Or am I asking for too much? 还是我要求太多?

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

This is my main Form that's has the button_click event handler. 这是我的主要窗体,具有button_click事件处理程序。 I want the user to input the employee name and ID number. 我希望用户输入员工姓名和ID号。 Every time the user clicks the button to send the information to my "EmployeeClass", the textbox is cleared in order for new input to be entered. 每次用户单击按钮将信息发送到我的“ 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 's comment, to let you know why I changed what you had to this, is that what you had way overcomplicated something that is very simple. 根据@Palatiner的评论,要让您知道为什么我更改了此内容,是因为您所使用的方法过于简单了。 Not to mention, the code you have will never work. 更不用说,您拥有的代码将永远无法使用。 Based on your code, you would always be updating the same array item since you explicitly assign to the array item at the constant position of 100. 根据您的代码,您将始终在更新同一数组项,因为您已将数组项显式分配给常数位置100。

All you need to do is create a object that keeps track of the 2 properties you want to save, Name and Id , along with a List that keeps a collection of those objects. 您需要做的就是创建一个对象,该对象跟踪要保存的两个属性NameId ,以及一个保留这些对象的集合的List。 An array does not dynamically resize, while a List does. 数组不会动态调整大小,而列表会动态调整大小。 With it, you do not need to manage the size of the collection on your own. 使用它,您无需自己管理集合的大小。 It also provides access to a lot of LINQ extention methods. 它还提供对许多LINQ扩展方法的访问。



I would create an Employee class like this: 我将创建一个Employee类,如下所示:

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


I would then create a CreateEmployee method like this: 然后,我将创建一个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;
    }
}


The EmployeeValuesAreValid method could be as simple as this: EmployeeValuesAreValid方法可以像这样简单:

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


Inside your Button Click you simply have to call CreateEmployee : 在您的Button单击内,您只需调用CreateEmployee

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



Putting everything together, you get: 将所有内容放在一起,您将获得:

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

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

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