http://i.stack.imgur.com/DpO5L.jpg http://i.stack.imgur.com/S9XL4.jpg
如何从表单1中获取信息并将其添加到表单2?
表单1上的我有很多文本框,一些组合框和其他内容,我想将这些项添加到表单2的列表框中。
这就是我所拥有的,但它不起作用
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 ClassofEmployees
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
class employee
{ //will include the attributes of all employees of your organization.
//fields for employee
public int employeeId; // 5 digit number to represent employee
public int ssn; //social security number of employee
public string name; //employee name
public int dob; //date of birth
public int pay; //rate of pay
}
class managers : employee
{
public string backgroundCheck;
public string isSalary;
public string responsibilitys;
}
public void getEmployeeData(employee employee)
{
try
{
employee.employeeId = int.Parse(EmployeeID.Text);
employee.ssn = int.Parse(SSN.Text);
employee.name = employeeName.Text;
employee.dob = int.Parse(DOB.Text);
employee.pay = int.Parse(pay.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void BCToString()
{
string bcSelectedY;
string bcSelectedN;
if (bcselect.SelectedIndex != 1)
{
bcSelectedY = bcselect.SelectedItem.ToString();
}
else
{
bcSelectedN = bcselect.SelectedItem.ToString();
}
}
public void getMangerData(managers managers)
{
if (bcselect.SelectedIndex != 1)
{
managers.backgroundCheck = "yes";
}
else
{
managers.backgroundCheck = "no";
}
if (salary.SelectedIndex != 1)
{
managers.isSalary = "Yes";
}
else
{
managers.isSalary = "No";
}
managers.responsibilitys = responsibilitys.Text;
}
public void add_Click(object sender, EventArgs e)
{
//create new employee object
employee newemployee = new employee();
//get employee data
getEmployeeData(newemployee);
//create new manager object
managers newmanagerialemployee = new managers();
getMangerData(newmanagerialemployee);
EmployeeCumalitveList.employeeList.Items.Add(employee);
}
private void done_Click(object sender, EventArgs e)
{
EmployeeCumalitveList ecl = new EmployeeCumalitveList
ecl.Show;
this.Hide();
}
}
}
这是错误的地方:
EmployeeCumalitveList.employeeList.Items.Add(employee);
我得到的错误是:
错误1新表达式需要(),[]或{} 键入C:\ Users \ T-Ali \ Desktop \ SHawnasschool \ vb.net 2 c#\ projects \ ClassofEmployees \ ClassofEmployees \ Form1.cs 108 66 ClassofEmployees
错误2可访问性不一致:参数类型 'ClassofEmployees.Form1.employee'不如方法可访问 'ClassofEmployees.Form1.getEmployeeData(ClassofEmployees.Form1.employee)'C:\ Users \ T-Ali \ Desktop \ SHawnasschool \ vb.net 2 c#\ projects \ ClassofEmployees \ ClassofEmployees \ Form1.cs 42 21 ClassofEmployees
错误3可访问性不一致:参数类型 'ClassofEmployees.Form1.managers'不如方法可访问 'ClassofEmployees.Form1.getMangerData(ClassofEmployees.Form1.managers)'C:\ Users \ T-Ali \ Desktop \ SHawnasschool \ vb.net 2 c#\ projects \ ClassofEmployees \ ClassofEmployees \ Form1.cs 70 21 ClassofEmployees
答案 0 :(得分:0)
首先,你是否公开了列表框? 我相信您可以通过选择列表框,转到属性窗口,查找“修改器”项并将其设置为“公共”来完成此操作。
其次,确保包含员工列表的表单是静态的,并且每次要添加员工时都不会创建相同的表单。 这样做:
namespace WindowsFormsApplication1
{
public static class Program
{
public static EmployeeCumalitveList MyEmployeeList;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
MyEmployeeList = new EmployeeCumalitveList();
Application.Run(MyEmployeeList);
}
}
}