簡體   English   中英

如何在C#Visual 2010中使用Button_Click事件處理程序填充數組

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

我試圖從文本框中接收用戶輸入,並將其發送到我的班級中名為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事件處理程序。 我希望用戶輸入員工姓名和ID號。 每次用戶單擊按鈕將信息發送到我的“ 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;
            }
        }
    }
}            

根據@Palatiner的評論,要讓您知道為什么我更改了此內容,是因為您所使用的方法過於簡單了。 更不用說,您擁有的代碼將永遠無法使用。 根據您的代碼,您將始終在更新同一數組項,因為您已將數組項顯式分配給常數位置100。

您需要做的就是創建一個對象,該對象跟蹤要保存的兩個屬性NameId ,以及一個保留這些對象的集合的List。 數組不會動態調整大小,而列表會動態調整大小。 使用它,您無需自己管理集合的大小。 它還提供對許多LINQ擴展方法的訪問。



我將創建一個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;
    }
}


然后,我將創建一個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;
}


在您的Button單擊內,您只需調用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;
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM