开发者

adding integer and to an hours value

开发者 https://www.devze.com 2023-02-11 17:30 出处:网络
I have a combobox which has values from 1-25. I also have 2 datetimepickers which display time values.

I have a combobox which has values from 1-25.

I also have 2 datetimepickers which display time values.

If I choose 10:00 AM for the first datetimepicker and 2 from the combobox, I'd like the result to be 12:00 PM in the second datetimep开发者_StackOverflow社区icker.

How can this be done?


secondDatePicker.Value = firstDatePicker.Value.AddHours(Convert.ToInt32(comboBox1.SelectedValue));


Simply look at MSDN : http://msdn.microsoft.com/en-us/library/system.datetime.aspx

Use the AddHours method.

Something like this :

int iIncrement = int.Parse(combobox.SelectedValue);
Datetime dt = firstDateTimePicker.value;
secondDateTimePicker.value = dt.AddHours(iIncrement );


Assuming you have a form with two DateTimePickers (dateTimePicker1 and DateTimePicker2) and one ComboBox containing the values specified, add an event handler to the SelectedIndexChanged event of the combobox as follows:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    // Get the DateTime from the first picker
    var currentDateTime = dateTimePicker1.Value;
    // Get the number of Hours to add to the DateTime
    var hoursToAdd = Convert.ToInt32(comboBox1.SelectedItem);
    // Add the hours to the DateTim
    var newDateTime = currentDateTime.AddHours(hoursToAdd);
    // Tell dateTimePicker2 to use the DateTime that has the hours added
    dateTimePicker2.Value = newDateTime;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号