我有一個名爲frmEmployees的員工form
,我需要加載幾個combo box
以及諸如國家,類別,國籍等數據。在c#中完成組合框數據加載後加載Windows窗體
現在當用戶點擊打開frmEmployees時,窗口會卡住一點點然後打開。我認爲這是因爲數據加載和初始化combo box
。 現在!我想要的是,點擊按鈕打開後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));
}
您應該使用進度的BackgroundWorker的bar.An更簡單的方法是簡單地將光標圖標更改爲加載符號。 – kurdy
你能推薦任何示例代碼嗎?因爲我嘗試了一些,但沒有運氣 – Siraj
@kurdy Backgroundworker不適用於UI元素。 UI控件需要在UI線程中加載。 – Harsh