나는 1-25의 값을 갖는 콤보 박스를 가지고있다.
또한 시간 값을 표시하는 2 datetimepickers 있습니다.정수와 시간 값을 더하는 것
첫 번째 datetimepicker에 대해 10:00 AM
을 선택하고 콤보 박스에서 2
을 선택하면 두 번째 datetimepicker에 12:00 PM
이 표시되도록하고 싶습니다.
어떻게이 작업을 수행 할 수 있습니까?
나는 1-25의 값을 갖는 콤보 박스를 가지고있다.
또한 시간 값을 표시하는 2 datetimepickers 있습니다.정수와 시간 값을 더하는 것
첫 번째 datetimepicker에 대해 10:00 AM
을 선택하고 콤보 박스에서 2
을 선택하면 두 번째 datetimepicker에 12:00 PM
이 표시되도록하고 싶습니다.
어떻게이 작업을 수행 할 수 있습니까?
secondDatePicker.Value = firstDatePicker.Value.AddHours(Convert.ToInt32(comboBox1.SelectedValue));
단순히 MSDN보고 : http://msdn.microsoft.com/en-us/library/system.datetime.aspx
사용하여있는 AddHours 방법을. 이 같은
뭔가 다음과 같이
int iIncrement = int.Parse(combobox.SelectedValue);
Datetime dt = firstDateTimePicker.value;
secondDateTimePicker.value = dt.AddHours(iIncrement);
두 DateTimePicker
의 (dateTimePicker1 및 DateTimePicker2)와 지정된 값을 포함하는 하나 ComboBox
과 양식을 가지고 가정은 콤보 상자의 SelectedIndexChanged
이벤트에 이벤트 처리기를 추가 :
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;
}
@ 스튜던트 나이프. firstDatePicker의 값에는 영향을주지 않습니다. checkout [AddHours()] (http://msdn.microsoft.com/en-us/library/system.datetime.addhours.aspx) 문서를 참조하십시오. –