在获取ListBox以从EmployeeClass中读取时遇到问题

时间:2017-05-11 19:31:10

标签: c# winforms

我目前正在为学校做作业,经过几天的尝试后,我无法弄清楚如何从我的属性表单中获取ListBox以显示我的员工档案的内容一种可以编辑或添加信息的方式。有什么建议吗?

这是我在这里发表的第一篇文章,如果是这样,请告诉我: A)格式太差 B)缺少信息(例如表格设计) C)其他任何包含的东西都需要,我想

using System;
using System.Linq;
using System.IO;
using System.Windows.Forms;

namespace EmployeeFormFinal
{
    public partial class EmployeeForm : Form
    {
        StreamReader inFile;
        StreamWriter outFile;
        eProperties form;

        public EmployeeForm(EmployeeClass emp)
        {
            InitializeComponent();
            employeeListbx.DoubleClick += new EventHandler(employeeListbx_DoubleClick);
            form = new eProperties(emp);                                                             //WIP
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutForm aboutForm = new AboutForm();
            aboutForm.Show();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFile();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveAs();
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Delete();
        }

        private void toolStripOpen_Click(object sender, EventArgs e)
        {
            OpenFile();
        }

        private void toolStripButtonSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void toolStripButtonSaveAs_Click(object sender, EventArgs e)
        {
            SaveAs();
        }

        private void toolStripButtonAddNew_Click(object sender, EventArgs e)
        {

        }

        private void toolStripButtonProperties_Click(object sender, EventArgs e)
        {
            Properties();                                                               //Broke with current WIP ideas
        }

        private void toolStripButtonDelete_Click(object sender, EventArgs e)
        {
            Delete();
        }

        private void propertiesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Properties();                                                               //Broke with current WIP ideas
        }

        private void employeeListbx_DoubleClick(object sender, EventArgs e)
        {
            Properties();                                                               //Broke with current WIP ideas
        }

        //Methods

        public void OpenFile()
        {
            string inValue;
            string dir = Directory.GetCurrentDirectory();
            string filepath = dir + @"\TextFile\EmployeeNames.txt";

            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = filepath;
            openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;
            openFileDialog1.ShowDialog();

            if (File.Exists(filepath))
            {
                try
                {
                    using (StreamReader inFile = new StreamReader(openFileDialog1.FileName))
                    {
                        while ((inValue = inFile.ReadLine()) != null)
                        {
                            EmployeeClass emp = new EmployeeClass();
                            string[] nArray = inValue.Split(' ');
                            emp.FName = nArray[0];
                            emp.LName = nArray[1];
                            emp.EmpType = nArray[2];
                            emp.Salary = nArray[3];

                            this.employeeListbx.Items.Add(emp.ToString());
                        }
                    }
                }
                catch (FileNotFoundException exc)
                {
                    lblMessage.Text = "That is not the employee file !\n" + exc.Message;
                }
                catch (Exception exc)
                {
                    lblMessage.Text = exc.Message;
                }
            }
            else
            {
                lblMessage.Text = "File Unavailable";
            }
        }

        public void SaveAs()                                            // Doesn't work, come back to later
        {
            string dir = Directory.GetCurrentDirectory();
            string filepath = dir + @"\TextFile\EmployeeNames.txt";

            try
            {
                SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                saveFileDialog1.InitialDirectory = filepath;
                saveFileDialog1.FileName = "EmployeeNamesEdited.txt";
                saveFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
                saveFileDialog1.ShowDialog();

                using (StreamWriter outFile = new StreamWriter(saveFileDialog1.FileName.ToString()))
                {

                    outFile.WriteLine(employeeListbx.SelectedItem);

                }

            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }
        }

        public void Save()                                             //Temporary, not sure exactly what this is doing
        {
            string dir = Directory.GetCurrentDirectory();
            string filepath = dir + @"\TextFile";

            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.InitialDirectory = filepath;
            saveFileDialog1.Title = "Save text Files";
            saveFileDialog1.CheckFileExists = true;
            saveFileDialog1.CheckPathExists = true;
            saveFileDialog1.DefaultExt = "txt";
            saveFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
            saveFileDialog1.FilterIndex = 2;
            saveFileDialog1.RestoreDirectory = true;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                employeeListbx.Text = saveFileDialog1.FileName;
            }
        }

        public void Properties(EmployeeClass emp)
        {
            eProperties eProp = new eProperties(emp);
            eProp.Show();
        }

        public void Delete()
        {
            ListBox.SelectedObjectCollection selectedItems = new ListBox.SelectedObjectCollection(employeeListbx);
            selectedItems = employeeListbx.SelectedItems;

            if (employeeListbx.SelectedIndex != -1)
            {
                for (int i = selectedItems.Count - 1; i >= 0; i--)
                    employeeListbx.Items.Remove(selectedItems[i]);
            }
            else
                MessageBox.Show("No selection possible.");
        }
    }
}

using System;
using System.Linq;
using System.IO;
using System.Windows.Forms;

namespace EmployeeFormFinal
{
    public partial class eProperties : Form
    {
        public eProperties(EmployeeClass emp)
        {
            InitializeComponent();
            propertiesEList.Items.Add(emp.ToString());                  //WIP

        }

        EmployeeClass emp;                                             //Working to get employee properties to display, I guess

        public EmployeeClass Employee
        {
            get
            {
                return emp;
            }
            set
            {
                emp = value;
            }
        }

        private void propertiesOkBtn_Click(object sender, EventArgs e)
        {
            eProperties.ActiveForm.Close();
        }

        private void propertiesCancelBtn_Click(object sender, EventArgs e)
        {
            eProperties.ActiveForm.Close();
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EmployeeFormFinal
{
    public class EmployeeClass
    {
        private string _fname,
            _lname,
            empType,
            salary;

        public string FName
        {
            get
            {
                return _fname;
            }
            set
            {
                _fname = value;
            }
        }

        public string LName
        {
            get
            {
                return _lname;
            }
            set
            {
                _lname = value;
            }
        }

        public string EmpType
        {
            get
            {
                return empType;
            }
            set
            {
                empType = value;
            }
        }

        public string Salary
        {
            get
            {
                return salary;
            }
            set
            {
                salary = value;
            }
        }

        public EmployeeClass()
        { }

        public EmployeeClass(string fName, string lName, string empType, string salary)
        {
            this._fname = fName;
            this._lname = lName;
            this.empType = empType;
            this.salary = salary;
        }

        public override string ToString()
        {
            return LName + ", " + FName;
        }

    }
}

1 个答案:

答案 0 :(得分:0)

请使用datagridview而不是listbox(仅列出数据)。连接事件:因为这是为了课程。请研究代码并了解它的作用。

    private void dataGridView1_RowLeave(object sender, DataGridViewCellEventArgs e)
    {
        var rowIndex = e.RowIndex;
        var currentRowVals = dataGridView1.Rows[e.RowIndex];
        var Id = currentRowVals.Cells[0].EditedFormattedValue;
        var FName = currentRowVals.Cells[1].EditedFormattedValue;
        var LName = currentRowVals.Cells[2].EditedFormattedValue;
        var EmpType = currentRowVals.Cells[3].EditedFormattedValue;
        var Salary = currentRowVals.Cells[4].EditedFormattedValue;

        if (dataGridView1.IsCurrentCellDirty)
        {
            if (Id.ToString() == "-1")
            {
                //do add
            }
            else
            {
                //do edit
            }
        }
    }