将值传递给表单 [英] passing values to a form

查看:62
本文介绍了将值传递给表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类Employees,它具有一种加载方法,可以从文本文件中加载雇员详细信息,并将其传递给要在列表框中显示的表单.

我基本上想从Employees类中的Load方法发送详细信息,以将其添加到MainForm中的employees对象中.谢谢

员工类如下:

Hi, I have a class Employees which has a load method to load employee details from a text file and pass them to a form to be displayed in a listbox.

I basically want to send the details from Load method in Employees class to be added in employees object in MainForm.Thanks

The Employees class is as follows:

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace HRApplication
{
    public class Employees : List<Employee>
    {

        
        
        public Employees()
        {
        }

        Employees employees;
        
        
        public bool Load(string filename)
        {
            
            
            // You need to add code here to load the data from the file into the collection
            employees = new Employees();
            employees.Add(new Employee("Bishop", "Tingu", "173 vasant vihar", "de1 3lq", "07834451343", new DateTime(2008, 1, 1)));
            MainForm mf = new MainForm();
            mf.EmployeeList= employees;
            return true;
            
        }

       
        

        
        public bool Save(string filename)
        {
            // You need to add code here to save the data
            // in the collection to the file.
            return true;
        }
    }
}




目前,我正在手动将收益率设为真实,以便可以对其进行测试.我假设我从文本文件中获得的值是:




Currently, I am manually putting the return to be true so I can test it. I am assuming the values I get from the text files are:

"Bishop", "Tingu", "173 vasant vihar", "de1 3lq", "07834451343", new DateTime(2008, 1, 1)



我希望将这些值传递到另一种形式的"MainForm"中



I want these values to be passed onto another form ''MainForm''

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace HRApplication
{
    public partial class MainForm : Form
    {
        // The file used to store employee details
        string employeesFile = "employees.txt";

        // The collection used to hold the employee data

        Employees employees;







        public MainForm()
        {
            InitializeComponent();
        }



        public  Employees EmployeeList
        {

            get
            {
                return employees;
            }
            set
            {
                employees = value;
            }
        }



        private void MainForm_Load(object sender, EventArgs e)
        {

            employees = new Employees();


            if (!employees.Load(employeesFile))
            {
                MessageBox.Show("Unable to load employees file");
            }
            else
            {

                PopulateListBox();
            }
        }

        private void PopulateListBox()
        {

            listBoxEmployees.Items.Clear();

            //Employee emp23 = new Employee("Bishop", "Aggarwal", "173 vasant vihar", "de1 3lq", "07834451343", new DateTime(2008, 1, 1));



            employees.Add(new Employee("Anant", "Aggarwal", "173 vasant vihar", "de1 3lq", "07834451343", new DateTime(2008, 1, 1)));
            employees.Sort();


            foreach (Employee employee in employees)
            {
                listBoxEmployees.Items.Add(employee.LastName + ", " + employee.FirstName);
            }
            listBoxEmployees.SelectedIndex = 0;
        }

        private void listBoxEmployees_DoubleClick(object sender, EventArgs e)
        {
            // You should add code here that will allow the
            // user to edit the details of the employee that
            // has been selected.
        }



    }
}





但是,当我运行该程序时,它仅显示我在MainForm中手动输入的详细信息:





But, when I run the program it only shows the details I manually entered in MainForm:

"Anant", "Aggarwal", "173 vasant vihar", "de1 3lq", "07834451343", new DateTime(2008, 1, 1))




并且不显示我通过员工"类发送给我的邮件.
我还将附加"Employee"类以供参考.




and doesn''t show the ones I sent through ''Employees'' class.
I am also attaching the ''Employee'' class for reference.

using System;
using System.Collections.Generic;
using System.Text;

namespace HRApplication
{
    public class Employee : IComparable<Employee>
    {

        string mFirstName;
        string mLastName;
        string mAddress;
        string mPhoneNumber;
        string mPostCode;
        DateTime mDateOfBirth;

        public Employee()
        {
        }

        public Employee(string firstName,
                        string lastName,
                        string address,
                        string postCode,
                        string phoneNumber,
                        DateTime dateOfBirth)
        {
            mFirstName = firstName;
            mLastName = lastName;
            mAddress = address;
            mPostCode = postCode;
            mPhoneNumber = phoneNumber;
            mDateOfBirth = dateOfBirth;


        }

        public string FirstName
        {
            set
            {
                mFirstName = value;
            }

            get
            {
                return mFirstName;

            }

        }

        public string LastName
        {
            set
            {
                mLastName = value;
            }

            get
            {
                return mLastName;

            }
        }

        public string Address
        {
            set
            {
                mAddress = value;
            }

            get
            {
                return mAddress;

            }
        }

        public string PostCode
        {
            set
            {
                mPostCode = value;
            }

            get
            {
                return mPostCode;

            }
        }

        public string PhoneNumber
        {
            set
            {
                mPhoneNumber = value;
            }

            get
            {
                return mPhoneNumber;

            }
        }

        public DateTime DateOfBirth
        {
            set
            {
                mDateOfBirth = value;
            }

            get
            {
                return mDateOfBirth;

            }
        }

        public int CompareTo(Employee otherEmployee)
        {
            return mLastName.CompareTo(otherEmployee.LastName);
        }


    }
}





请帮助!!!





PLEASE HELP!!!

推荐答案

您需要在Employees类中删除MainForm的硬依赖性.

You need to remove the hard dependency of MainForm in your Employees class.

MainForm mf = new MainForm();
mf.Thevalue = emp;



这是一个非常糟糕的设计,并确保您永远无法重用该类.



This is an extremely poor design and ensured you will never be able to reuse the class.


员工应从事任何UI工作.它不应引用主要形式.相反,主表单应引用一个Employees(已经引用); MainForm_Load应该为其刚创建的Employees实例分配其theValue(这是一个很糟糕的名称,使用Employees或EmployeeList之类的东西).

(实际上,在第二次查看时,该属性是完全没有意义的,应该将其删除.)

为什么雇员引用另一个雇员实例(emp)?这是没有意义的. Employees.Load应该自己调用Clear and Add. (实际上,有人可能会争辩说列表类也不应该对文件I/O负责,而让我们暂时搁置这一辩论.)这就是问题的根源,就像打电话给雇员. ,它将填充在Employees.Load中创建的新实例,然后在方法末尾将其丢弃.

您将编写这样一个混乱的类的事实意味着您需要回到OO基础知识,并且真正了解概念.对我来说,这段代码看起来像是在四处乱窜并猜测事物.
Employees should not be doing any UI work. It should not have a reference to a main form. Instead, the main form should have a reference to an Employees (it does already); MainForm_Load should assign its theValue (that is a terrible name by the way, use something like Employees or EmployeeList) to the Employees instance it just created.

(Actually on second viewing that property is entirely pointless and should just be removed.)

Why does Employees have a reference to another Employees instance (emp)? That makes no sense. Employees.Load should just call Clear and Add on itself. (Actually one might argue that the list class shouldn''t be responsible for file I/O either but let''s leave that debate for now.) This is the source of your problem, as calling employees.Load is not populating employees, it is populating the new instance created in Employees.Load which is then thrown away at the end of the method.

The fact that you would write such a confused class implies that you need to go back to OO basics and really understand the concepts. This code looks like flailing around and guessing at things, to me.


正如我所看到的,您正在创建MainForm的新实例并将该集合分配给该实例,这与您的实例不同.当前运行的主要形式.因此,您无法在列表框中看到数据.

尝试将其设置为静态,以便您可以在所有实例中访问它.但这不是一个好习惯.
As I could see you are creating a new instance of MainForm and assigning the collection to that instance, which is different from your current running main form. Hence you could not see your data in the listbox.

Try to make that static so that you can access it in all instance. But this is not a good practice.
public static Employees Thevalue
        {

            get;set;
        }



2.
尝试从Load方法返回集合,这样您就可以将所有雇员带到所需的任何地方.

让我们从其他方面说,只要您需要调用该方法即可.




2.
Try to return the collection from the Load method so that you can get all your employees where ever you want.

Lets say in some other from you want this, Just you need to call the method.



这篇关于将值传递给表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆