我正在學習C#並且正在製作一個自定義的工資覈算應用程序。我有一份僱主期的表格,允許用戶一次輸入/編輯或刪除一個工資覈算期,即在給定時間只能有一個記錄在數據庫中。ComboBox上的SelectedIndexChanged方法的C#問題
我有一個表單加載的方法,如果存在工資期限,它將檢索所有需要的字段。
代碼:
private void PayrollPeriodForm_Load(object sender, EventArgs e)
{
dateTimePicker2.Value = dateTimePicker2.Value.AddYears(1).AddDays(-1);
// Get service instance
var employerPeriodService = Program.Kernel.Get<IEmployerPeriodService>();
//Query database
var employerPeriods = employerPeriodService.GetAllEmployerPeriods();
if (employerPeriods.Any(x => x != null))
{
var employerPeriod = employerPeriods.First();
txt_tax_year.Text = employerPeriod.U_Tax_year.ToString();
lbl_code.Text = employerPeriod.Code;
cb_number_hours.Text = employerPeriod.U_Day_hrs.ToString();
cb_number_days_week.Text = employerPeriod.U_Week_days.ToString();
cb_number_days_month.Text = employerPeriod.U_Month_days.ToString();
cb_number_days_fortnight.Text = employerPeriod.U_Fortnight_days.ToString();
txt_number_hours_week.Text = employerPeriod.U_Week_hrs.ToString();
txt_number_hours_fortnight.Text = employerPeriod.U_Fortnight_hrs.ToString();
txt_number_hours_month.Text = employerPeriod.U_Month_hrs.ToString();
cb_avg_weeks_month.Text = employerPeriod.U_Weeks_in_month.ToString();
cb_avg_fortnights_month.Text = employerPeriod.U_No_of_Fortnights.ToString();
dateTimePicker1.Text = employerPeriod.U_Starting_period.ToString();
dateTimePicker2.Text = employerPeriod.U_Ending_period.ToString();
txt_tax_year.Enabled = false;
dateTimePicker1.Enabled = false;
dateTimePicker2.Enabled = false;
btn_add.Enabled = false;
btn_update.Enabled = true;
btn_delete.Enabled = true;
btn_update.Visible = true;
btn_delete.Visible = true;
btn_add.Visible = false;
// Store SAP code
SAPCodePeriod = employerPeriod.Code;
}
else
{
//clear textfields after input
cb_number_hours.SelectedIndex = 0;
cb_number_days_week.SelectedIndex = 0;
txt_number_hours_week.Text = "";
cb_number_days_month.SelectedIndex = 0;
txt_number_hours_month.Text = "";
cb_number_days_fortnight.SelectedIndex = 0;
txt_number_hours_fortnight.Text = "";
cb_avg_weeks_month.Text = null;
cb_avg_fortnights_month.Text = null;
lbl_code.Text = "";
txt_tax_year.Enabled = true;
dateTimePicker1.Enabled = true;
dateTimePicker2.Enabled = true;
btn_add.Enabled = true;
btn_update.Enabled = false;
btn_delete.Enabled = false;
btn_add.Visible = true;
btn_update.Visible = false;
btn_delete.Visible = false;
}
}
我有兩個selectd指數變化事件對我的組合框的2到目前爲止:
private void cb_number_hours_SelectedIndexChanged(object sender, EventArgs e)
{
// calculate hours/week
var hoursWeek = (int.Parse(cb_number_hours.Text)) * (int.Parse(cb_number_days_week.Text));
txt_number_hours_week.Text = hoursWeek.ToString();
}
private void cb_number_days_week_SelectedIndexChanged(object sender, EventArgs e)
{
// calculate hours/week
var hoursWeek = (int.Parse(cb_number_hours.Text)) * int.Parse(cb_number_days_week.Text);
txt_number_hours_week.Text = hoursWeek.ToString();
}
我的問題: 之前,我有任何的SelectedIndexChanged
方法,所有的數據庫中的值顯示在適當的文本框和組合框中。
但是,在添加cb_number_hours_SelectedIndexChanged
方法後,停止顯示後,來自DB的所有文本框和組合框值。這導致方法中的計算不起作用(int.Parse(cb_number_days_week.Text))
被視爲空。
我該如何解決這個問題?
這不回答你的問題,但以下是沒有意義的:var employerPeriods =從EP中employerPeriodService 。GetAllEmployerPeriods() select ep; 您可以編寫var employerPeriods = employerPeriodService.GetAllEmployerPeriods();而不是 – Dismissile
true;從中選擇,忘記刪除不必要的 –