员工文件搜索

时间:2015-09-06 07:18:31

标签: c#

我写了一个文件,然后输入名字,姓氏,员工ID,地址和邮政编码然后保存,然后如果你去搜索并输入相同邮政编码的邮政编码将会显示,但是当我输入文件不会显示的邮政编码。我认为我的代码有问题可以帮助我。

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pnlRead.Location = pnlWrite.Location;
            pnlSearch.Location = pnlWrite.Location;
            pnlWrite.BringToFront();
            System.IO.File.WriteAllText("Employee.txt", string.Empty);
        }

        private void writeToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            pnlWrite.BringToFront();
        }

        private void exitToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click_1(object sender, EventArgs e)
        {
            string EmployeeData;
            listRead.Items.Clear();

            EmployeeData = txtFirstName.Text + "\n" + txtLastName.Text + "\n"
                + txtID.Text + "\n" + txtAddress.Text + "\n" + txtZipCode.Text + "\n\n";
            File.AppendAllText("Employee.txt", EmployeeData);
            txtFirstName.Clear();
            txtLastName.Clear();
            txtID.Clear();
            txtAddress.Clear();
            txtZipCode.Clear();
            txtFirstName.Focus();
            listRead.Items.Clear();

        }

        private void readToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pnlRead.BringToFront();
            try
            {
                string[] EmployeeData = File.ReadAllLines("Employee.txt");
                listRead.Items.AddRange(EmployeeData);
                listRead.ClearSelected();    
            }
            catch
            {
                MessageBox.Show("File or path not found or invalid.");
            }
        }

        private void searchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pnlSearch.BringToFront();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            pnlRead.BringToFront();
            listRead.Items.Clear();

            try
            {
                string[] EmployeeData = File.ReadAllLines("Employee.txt");
                for (int i = 0; i < EmployeeData.Length; i += 2)
                {
                    if (EmployeeData[i] == txtSearch.Text)
                    {
                        listRead.Items.Add(EmployeeData[i]);
                        listRead.Items.Add(EmployeeData[i + 1]);
                    }
                }
            }
            catch
            {
                MessageBox.Show("File or path not found or invalid.");
            } 
        }
    }
}

1 个答案:

答案 0 :(得分:0)

在for循环中,如果您找到与邮政编码匹配的内容,则只添加该行和下一行(存在超出界限的风险)

有一种简单的方法可以将数组中的每一行添加到Items集合

private void btnSearch_Click(object sender, EventArgs e)
{
    pnlRead.BringToFront();
    listRead.Items.Clear();

    try
    {
        string[] EmployeeData = File.ReadAllLines("Employee.txt");
        if(EmployeeData.Contains(txtSearch.Text))
            listRead.Items.AddRange(EmployeeData);
    }
    catch
    {
        MessageBox.Show("File or path not found or invalid.");
    } 
}