C# problem with SelectedIndexChanged method on combobox
I am learning C# and am making a custom Payroll application. I have a form for employer periods that allows a user to enter/edit or delete one payroll period at a time, i.e, there can only be one record in the database at a given time.
I have a method on form load that will retrieve all needed fields if a payroll period exists.
Code:
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_mo开发者_运维技巧nth.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;
}
}
I have two selectd index changed events for 2 of my comboboxes so far:
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();
}
My Problem:
Before I had any of the SelectedIndexChanged
methods, All the values from the database were showing in their appropriate textboxes and comboboxes.
However after adding the cb_number_hours_SelectedIndexChanged
method, all textbox and combobox values from the DB after cb_number_hours.Text
stopped showing. This has resulted in The calculation in the methods not to work as (int.Parse(cb_number_days_week.Text))
is seen as null.
How do I fix this?
In your cb_number_hours_SelectedIndexChanged
event handler, try adding a check for a valid value before the rest of your code, like this (untested):
int hours;
int days;
if (Int32.TryParse(cb_number_hours.Text, out hours) && Int32.TryParse(cb_number_days_week.Text, out days))
{
var hoursWeek = hours * days;
txt_number_hours_week.Text = hoursWeek.ToString();
}
精彩评论