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 DateTimePicker
s (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;
}
精彩评论