C# 在c中完成组合框数据加载后加载Windows窗体#
我有一个名为frmememployees的员工的C# 在c中完成组合框数据加载后加载Windows窗体#,c#,winforms,asynchronous,combobox,async-await,C#,Winforms,Asynchronous,Combobox,Async Await,我有一个名为frmememployees的员工的表单,我需要在其中加载几个组合框,其中包含国家、类别、国籍等数据 现在,当用户点击打开frmEmployees时,窗口被卡住一位,然后打开。我假设这是由于数据加载和初始化组合框。 现在!我想要的是,在点击按钮打开frmEmployees之后,运行一个进度条,直到数据加载完成,然后打开表单 public frmEmployee() { InitializeComponent(); con = new Conne
表单
,我需要在其中加载几个组合框
,其中包含国家、类别、国籍等数据
现在,当用户点击打开frmEmployees时,窗口被卡住一位,然后打开。我假设这是由于数据加载和初始化组合框。
现在!我想要的是,在点击按钮打开frmEmployees之后,运行一个进度条,直到数据加载完成,然后打开表单
public frmEmployee()
{
InitializeComponent();
con = new Connection();
LoadComboboxDS();
}
我也试过了
private void FrmEmployee_Load(object sender, EventArgs e)
{
LoadComboboxDS();
}
private void LoadComboboxDS()
{
//company
var _companies = con.Companies.Where(x => x.IsDeleted == false).ToList();
_companies.Insert(0,new data.Models.CompanyModels.Company { Address = new data.Models.Address(), Code = null, Name = "--Select--", BaseCurrency = new data.Models.Currency() });
cbCompany.DataSource = _companies;
cbCompany.DisplayMember = "Name";
cbCompany.ValueMember = "ID";
//gender
cbGender.DataSource = Enum.GetValues(typeof(Gender));
//merital status
cbMeritalStatus.DataSource = Enum.GetValues(typeof(MaritalStatus));
//citizenship
var _citizenships = con.Countries.Select(x => x.Citizenship).Distinct().ToList();
_citizenships.Insert(0, "--Select--");
cbCitizenship.DataSource = _citizenships;
cbCitizenship.DisplayMember = "Citizenship";
//nationality
var _nations = con.Countries.Select(x => x.Name).Distinct().ToList();
_nations.Insert(0, "--Select--");
cbNationality.DataSource = _nations;
cbNationality.DisplayMember = "Name";
//domicile
var _domiciles = con.Countries.Select(x => x.Name).Distinct().ToList();
_domiciles.Insert(0, "--Select--");
cbDomicile.DataSource = _domiciles;
cbDomicile.DisplayMember = "Name";
//cast category
var _casts = con.CastCategories.Select(x => new {x.ShortText, x.Description}).Distinct().ToList();
_casts.Insert(0, new { ShortText = "", Description = "--Select--" });
cbCategory.DataSource = _casts;
cbCategory.DisplayMember = "Description";
cbCategory.ValueMember = "ShortText";
//religion
cbReligion.DataSource = Enum.GetValues(typeof(Religion));
}
您可以使用EntityFramework6的异步扩展方法使代码异步 首先,分离数据访问层和表示层:
public static class MyRepository // consider not static, just an example
{
public static async Task<List<Company>> GetCompanies()
{
using (var connection = new Connection()) // consider factory
{
return await con.Companies.Where(x => x.IsDeleted == false).ToListAsync();
}
}
public async Task<List<Citizenship>> GetCitizenships()
{
using (var connection = new Connection()) // factory?
{
return await con.Countries.Select(x => x.Citizenship).Distinct().ToList();
}
}
}
现在,您只需要让窗体在构造函数中接受完整的数据,而不是让窗体加载打破抽象的数据:
public class FrmEmployees
{
public FrmEmployees(List<Company> companies, List<Citizenship> citizenships)
{
companies.Insert(0,new data.Models.CompanyModels.Company { Address = new data.Models.Address(), Code = null, Name = "--Select--", BaseCurrency = new data.Models.Currency() });
cbCompany.DataSource = companies;
cbCompany.DisplayMember = "Name";
cbCompany.ValueMember = "ID";
citizenships.Insert(0, "--Select--");
cbCitizenship.DataSource = _citizenships;
cbCitizenship.DisplayMember = "Citizenship";
// etc.
}
}
公共类员工
{
公众雇员(上市公司、上市公民)
{
插入(0,new data.Models.CompanyModels.CompanyModels.Company{Address=new data.Models.Address(),Code=null,Name=“--Select--”,BaseCurrency=new data.Models.Currency());
cbCompany.DataSource=公司;
cbCompany.DisplayMember=“Name”;
cbCompany.ValueMember=“ID”;
公民身份。插入(0,“--Select--”;
cbcitizensity.DataSource=\u公民身份;
cbcidential.DisplayMember=“公民身份”;
//等等。
}
}
有一点很重要:可以将许多任务和数据传递给表单构造函数。如果所有这些数据将经常用于设置,那么您可能希望将“获取所有这些内容”逻辑封装到一个地方,以消除可能的代码重复。查看我所做的。。。如果有人可以查看我的代码,这将是可观的
public class EmployeeFormDataRepesenter
{
public List<Company> Companies { get; set; }
public List<Country> Countries { get; set; }
public List<CastCategory> CastCategories { get; set; }
}
public void LoadData(EmployeeFormDataRepesenter representer)
{
//gender
cbGender.DataSource = Enum.GetValues(typeof(Gender));
//merital status
cbMeritalStatus.DataSource = Enum.GetValues(typeof(MaritalStatus));
//religion
cbReligion.DataSource = Enum.GetValues(typeof(Religion));
//company
var _companies = representer.Companies;
_companies.Insert(0, new data.Models.CompanyModels.Company { Address = new data.Models.Address(), Code = null, Name = "--Select--", BaseCurrency = new data.Models.Currency() });
cbCompany.DataSource = _companies;
cbCompany.DisplayMember = "Name";
cbCompany.ValueMember = "ID";
//citizenship
var _citizenships = representer.Countries.Select(x => x.Citizenship).ToList();
_citizenships.Insert(0, "--Select--");
cbCitizenship.DataSource = _citizenships;
cbCitizenship.DisplayMember = "Citizenship";
//nationality
var _nations = representer.Countries.Select(x => x.Name).ToList();
_nations.Insert(0, "--Select--");
cbNationality.DataSource = _nations;
cbNationality.DisplayMember = "Name";
//domicile
var _domiciles = representer.Countries.Select(x => x.Name).ToList();
_domiciles.Insert(0, "--Select--");
cbDomicile.DataSource = _domiciles;
cbDomicile.DisplayMember = "Name";
//cast category
var _casts = representer.CastCategories.Select(x => new { x.ShortText, x.Description }).Distinct().ToList();
_casts.Insert(0, new { ShortText = "", Description = "--Select--" });
cbCategory.DataSource = _casts;
cbCategory.DisplayMember = "Description";
cbCategory.ValueMember = "ShortText";
}
private async void btnEmplyee_Click(object sender, EventArgs e)
{
con = new Connection();
Action showProgress = () => frmStatrup._progressBar.Visible = true;
Action hideProgress = () => frmStatrup._progressBar.Visible = false;
EmployeeFormDataRepesenter representer;
Task<List<Company>> _taskCompany = new Task<List<Company>>(() =>
{
BeginInvoke(showProgress);
var list = con.Companies.ToListAsync();
BeginInvoke(hideProgress);
if (list != null)
return list.Result;
return null;
});
Task<List<Country>> _taskCountry = new Task<List<Country>>(() =>
{
BeginInvoke(showProgress);
var list = con.Countries.ToListAsync();
BeginInvoke(hideProgress);
if (list != null)
return list.Result;
return null;
});
Task<List<CastCategory>> _taskCasts = new Task<List<CastCategory>>(() =>
{
BeginInvoke(showProgress);
var list = con.CastCategories.ToListAsync();
BeginInvoke(hideProgress);
if (list != null)
return list.Result;
return null;
});
_taskCompany.Start();
var _companies = await _taskCompany;
_taskCountry.Start();
var _countries = await _taskCountry;
_taskCasts.Start();
var _casts = await _taskCasts;
if (_companies.Count != 0)
{
representer = new EmployeeFormDataRepesenter();
representer.Companies = _companies;
representer.Countries = _countries;
representer.CastCategories = _casts;
}
else
{
representer = null;
}
if (representer != null)
{
frmEmployee _emp = new frmEmployee();
_emp.LoadData(representer);
Functions.OpenForm(_emp, this.ParentForm);
}
}
公共类EmployeeFormDataRepenter
{
上市公司{get;set;}
公共列表国家{get;set;}
公共列表类别{get;set;}
}
公共无效加载数据(EmployeeFormDataRepenster重新输入)
{
//性别
cbGender.DataSource=Enum.GetValues(typeof(Gender));
//经位
cbMeritalStatus.DataSource=Enum.GetValues(typeof(MaritalStatus));
//宗教信仰
cbReligion.DataSource=Enum.GetValues(typeof(宗教));
//公司
var_companies=代表公司;
_插入(0,new data.Models.CompanyModels.CompanyModels.Company{Address=new data.Models.Address(),Code=null,Name=“--Select--”,BaseCurrency=new data.Models.Currency());
cbCompany.DataSource=\u公司;
cbCompany.DisplayMember=“Name”;
cbCompany.ValueMember=“ID”;
//公民身份
var_citizenships=representer.Countries.Select(x=>x.Citizenship.ToList();
_公民身份。插入(0,“--Select--”;
cbcitizensity.DataSource=\u公民身份;
cbcidential.DisplayMember=“公民身份”;
//国籍
var_nations=representer.Countries.Select(x=>x.Name.ToList();
_国家。插入(0,“--”选择“-”;
cbnationary.DataSource=\u国家;
cbnational.DisplayMember=“Name”;
//住所
var_domiciles=representer.Countries.Select(x=>x.Name.ToList();
_住所。插入(0,“--选择--”;
cbDomicile.DataSource=_住所;
cbDomicile.DisplayMember=“Name”;
//演员类别
var\u casts=representer.CastCategories.Select(x=>new{x.ShortText,x.Description}).Distinct().ToList();
_Insert(0,new{ShortText=”“,Description=“--Select--”});
cbCategory.DataSource=\u强制转换;
cbCategory.DisplayMember=“说明”;
cbCategory.ValueMember=“ShortText”;
}
私有异步无效btnEmplyee\u单击(对象发送方,事件参数)
{
con=新连接();
动作showProgress=()=>frmStatrup.\u progressBar.Visible=true;
动作hideProgress=()=>frmStatrup.\u progressBar.Visible=false;
EmployeeFormDataRepenter代表输入;
任务公司=新任务(()=>
{
BeginInvoke(显示进度);
var list=con.companys.toListSync();
开始发汗;
如果(列表!=null)
返回列表。结果;
返回null;
});
任务_taskCountry=新任务(()=>
{
BeginInvoke(显示进度);
var list=con.Countries.ToListAsync();
开始发汗;
如果(列表!=null)
返回列表。结果;
返回null;
});
任务_taskCasts=新任务(()=>
{
BeginInvoke(显示进度);
var list=con.CastCategories.toListSync();
开始发汗;
如果(列表!=null)
返回列表。结果;
返回null;
});
_taskCompany.Start();
var_companys=等待公司;
_taskCountry.Start();
var_countries=等待任务国;
_taskCasts.Start();
var _casts=wait _taskCasts;
如果(_companys.Count!=0)
{
representer=new employeeFormDataRepenter();
代表公司=_家公司;
representer.Countries=\u国家;
representer.CastCategories=\u强制转换;
}
其他的
{
representer=null;
}
如果(重新输入!=null)
{
frmEmployee_emp=新frmEmployee();
_emp.LoadData(重新输入);
Functions.OpenForm(_emp,this.ParentForm);
}
}
您应该为进度条使用backgroundworker。更简单的方法是将光标图标更改为加载符号。您可以推荐任何示例代码吗。因为我试过一些,但没有luck@kurdyBackgroundworker将不适用于UI元素。用户界面控件需要
public class EmployeeFormDataRepesenter
{
public List<Company> Companies { get; set; }
public List<Country> Countries { get; set; }
public List<CastCategory> CastCategories { get; set; }
}
public void LoadData(EmployeeFormDataRepesenter representer)
{
//gender
cbGender.DataSource = Enum.GetValues(typeof(Gender));
//merital status
cbMeritalStatus.DataSource = Enum.GetValues(typeof(MaritalStatus));
//religion
cbReligion.DataSource = Enum.GetValues(typeof(Religion));
//company
var _companies = representer.Companies;
_companies.Insert(0, new data.Models.CompanyModels.Company { Address = new data.Models.Address(), Code = null, Name = "--Select--", BaseCurrency = new data.Models.Currency() });
cbCompany.DataSource = _companies;
cbCompany.DisplayMember = "Name";
cbCompany.ValueMember = "ID";
//citizenship
var _citizenships = representer.Countries.Select(x => x.Citizenship).ToList();
_citizenships.Insert(0, "--Select--");
cbCitizenship.DataSource = _citizenships;
cbCitizenship.DisplayMember = "Citizenship";
//nationality
var _nations = representer.Countries.Select(x => x.Name).ToList();
_nations.Insert(0, "--Select--");
cbNationality.DataSource = _nations;
cbNationality.DisplayMember = "Name";
//domicile
var _domiciles = representer.Countries.Select(x => x.Name).ToList();
_domiciles.Insert(0, "--Select--");
cbDomicile.DataSource = _domiciles;
cbDomicile.DisplayMember = "Name";
//cast category
var _casts = representer.CastCategories.Select(x => new { x.ShortText, x.Description }).Distinct().ToList();
_casts.Insert(0, new { ShortText = "", Description = "--Select--" });
cbCategory.DataSource = _casts;
cbCategory.DisplayMember = "Description";
cbCategory.ValueMember = "ShortText";
}
private async void btnEmplyee_Click(object sender, EventArgs e)
{
con = new Connection();
Action showProgress = () => frmStatrup._progressBar.Visible = true;
Action hideProgress = () => frmStatrup._progressBar.Visible = false;
EmployeeFormDataRepesenter representer;
Task<List<Company>> _taskCompany = new Task<List<Company>>(() =>
{
BeginInvoke(showProgress);
var list = con.Companies.ToListAsync();
BeginInvoke(hideProgress);
if (list != null)
return list.Result;
return null;
});
Task<List<Country>> _taskCountry = new Task<List<Country>>(() =>
{
BeginInvoke(showProgress);
var list = con.Countries.ToListAsync();
BeginInvoke(hideProgress);
if (list != null)
return list.Result;
return null;
});
Task<List<CastCategory>> _taskCasts = new Task<List<CastCategory>>(() =>
{
BeginInvoke(showProgress);
var list = con.CastCategories.ToListAsync();
BeginInvoke(hideProgress);
if (list != null)
return list.Result;
return null;
});
_taskCompany.Start();
var _companies = await _taskCompany;
_taskCountry.Start();
var _countries = await _taskCountry;
_taskCasts.Start();
var _casts = await _taskCasts;
if (_companies.Count != 0)
{
representer = new EmployeeFormDataRepesenter();
representer.Companies = _companies;
representer.Countries = _countries;
representer.CastCategories = _casts;
}
else
{
representer = null;
}
if (representer != null)
{
frmEmployee _emp = new frmEmployee();
_emp.LoadData(representer);
Functions.OpenForm(_emp, this.ParentForm);
}
}