我有两个组合框,我希望用户计算一个进程的总时间

时间:2015-03-24 23:58:28

标签: c# visual-studio-2010 visual-studio visual-studio-2012

//目前combox的格式是hh:mm,我知道我没有将它们正确地转换为来自comboBoxes的DateTime.Parse,任何帮助都会很棒!!

    private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        string start = comboBox2.SelectedItem;
        starttime = DateTime.Parse(start);

    }

    private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
    {
        string end = comboBox3.SelectedItem;
        endtime = DateTime.Parse(end);

    }

    private void button2_Click(object sender, EventArgs e)
    {
        double z;

        z = endtime - starttime;
        textBox1.Text = z.ToString();
    }

1 个答案:

答案 0 :(得分:0)

string format = "hh:mm"
DateTime dateTime = DateTime.ParseExact(comboBox.Text, format, CultureInfo.InvariantCulture);

尝试将其解析为dateTime:)

顺便说一句,ComboBox.SelectedItem不返回字符串,因此如果您不打算读取Text属性,请确保调用SelectedItem.ToString()。