组合框上的SelectedIndexChanged方法的C#问题

时间:2011-07-11 13:23:11

标签: c# visual-studio-2010 combobox

我正在学习C#并正在制作自定义Payroll应用程序。我有一个雇主期的表格,允许用户一次输入/编辑或删除一个工资单期间,即在给定时间内数据库中只能有一条记录。

我有一个表单加载方法,如果存在工资核算期,它将检索所有需要的字段。

代码:

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;
    }
}

到目前为止,我的两个组合框中有两个已选择的索引更改事件:

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方法后,cb_number_hours.Text停止显示后,数据库中的所有文本框和组合框值都会显示。这导致方法中的计算不起作用(int.Parse(cb_number_days_week.Text))被视为空。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

cb_number_hours_SelectedIndexChanged事件处理程序中,在代码的其余部分之前尝试adding a check for a valid value,如下所示(未经测试):

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();
}
寻找你感兴趣的贴纸↓↓↓
豫ICP备18024241号-1