2011-02-18 5 views
0

나는 1-25의 값을 갖는 콤보 박스를 가지고있다.
또한 시간 값을 표시하는 2 datetimepickers 있습니다.정수와 시간 값을 더하는 것

첫 번째 datetimepicker에 대해 10:00 AM을 선택하고 콤보 박스에서 2을 선택하면 두 번째 datetimepicker에 12:00 PM이 표시되도록하고 싶습니다.

어떻게이 작업을 수행 할 수 있습니까?

답변

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

@ 스튜던트 나이프. firstDatePicker의 값에는 영향을주지 않습니다. checkout [AddHours()] (http://msdn.microsoft.com/en-us/library/system.datetime.addhours.aspx) 문서를 참조하십시오. –

1

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; 
} 
관련 문제