【问题标题】:I have two combo Boxes where I want users to calculate a total time for a process我有两个组合框,我希望用户在其中计算一个过程的总时间
【发布时间】:2015-03-24 23:58:28
【问题描述】:

//当前组合框的格式为 hh:mm,我知道我没有将它们正确转换为组合框中的 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();
    }

【问题讨论】:

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


【解决方案1】:
string format = "hh:mm"
DateTime dateTime = DateTime.ParseExact(comboBox.Text, format, CultureInfo.InvariantCulture);

试试这个解析成 dateTime :)

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

【讨论】:

  • 应用时出现以下错误:当前上下文中不存在名称“CultureInfo”
  • 使用 System.Globalization;
  • 非常高兴,我使用以下方法使其工作,但也将 z 变量更改为 TimeSpan,感谢您的帮助!
猜你喜欢
  • 2014-07-21
  • 1970-01-01
  • 2021-05-18
  • 2014-06-20
  • 2017-12-25
  • 2015-08-22
  • 1970-01-01
  • 2021-04-19
  • 2017-05-27
相关资源
最近更新 更多