开发者

adding integer and to an hours value

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜