다음을 통해 공유


방법: Calendar 웹 서버 컨트롤의 날짜 선택에 응답

업데이트: 2007년 11월

Calendar 컨트롤의 SelectionMode 속성을 None이 아닌 다른 값으로 설정하면 사용자가 일자나 날짜 범위를 선택할 수 있습니다. 또한 사용자의 선택을 검색하고 이에 응답할 수 있습니다.

날짜 선택에 응답하려면

  • 다음과 같은 서명을 가지는 Calendar 컨트롤의 SelectionChanged 이벤트에 대한 메서드를 만듭니다.

    Private Sub Calendar1_SelectionChanged(ByVal sender _
        As System.Object, ByVal e As System.EventArgs) _
        Handles Calendar1.SelectionChanged
    
    private void Calendar1_SelectionChanged (object sender, 
        System.EventArgs e)
    
    참고:

    컨트롤에서 사용자 동작에 의해 날짜 선택이 변경되는 경우에만 이 이벤트가 발생됩니다. 예를 들어 사용자가 같은 날짜를 두 번 클릭하는 경우 두 번째 클릭은 이벤트를 발생시키지 않습니다. 날짜 범위를 프로그래밍 방식으로 설정하는 경우에도 이 이벤트가 발생되지 않습니다.

날짜 선택 관련 정보는 다음과 같은 속성에서 사용할 수 있습니다.

속성

설명

SelectedDate

하나의 날짜. 사용자가 하나의 날짜를 선택한 경우 이 속성은 해당 날짜를 포함합니다. 사용자가 여러 날짜를 선택한 경우 이 속성은 날짜 범위의 첫 번째 날짜를 포함합니다.

SelectedDates

선택된 모든 날짜를 포함하는 컬렉션. 이 컬렉션의 날짜는 순서가 지정되고 고유합니다. Calendar 컨트롤에서는 사용자가 여러 날짜를 개별적으로 선택할 수 없기 때문에 컬렉션의 날짜는 순차적입니다.

선택된 날짜 수를 확인하려면

  • 다음 예제처럼 SelectedDates 컬렉션의 Count 속성 값을 얻습니다.

    Public Sub Calendar1_SelectionChanged(ByVal sender As Object, _
       ByVal e As System.EventArgs) _
       Handles Calendar1.SelectionChanged
    
       Text1.Text = "You selected " _
          & Calendar1.SelectedDates.Count.ToString() _
          & " date(s)."
    End Sub
    
    private void Calendar1_SelectionChanged (object sender, System.EventArgs e)
    {
       Text1.Text = String.Format("You selected {0} date(s).",
          Calendar1.SelectedDates.Count);
    }   
    

사용자가 여러 날짜를 선택한 것으로 확인되는 경우 날짜 범위를 얻을 수 있습니다.

여러 날짜 선택의 날짜 범위를 얻으려면

  1. SelectedDates 속성의 Count 속성을 사용하여 선택한 날짜 수를 가져옵니다.

  2. 컬렉션의 첫 번째 날짜를 얻은 후 해당 날짜 수에서 1을 뺀 값의 인덱스에서 날짜를 추출하여 마지막 날짜를 얻습니다. 다음 예제는 페이지의 텍스트 상자 컨트롤에 있는 첫 번째 및 마지막 날짜를 표시합니다.

    Public Sub Calendar1_SelectionChanged(ByVal sender As Object, _
       ByVal e As System.EventArgs) _
       Handles Calendar1.SelectionChanged
    
       With Calendar1.SelectedDates
          If .Count > 1 Then
             Dim firstDate As Date = .Item(0).Date
             Dim lastDate As Date = .Item(.Count - 1).Date
             TextBox1.Text = firstDate.ToString()
             TextBox2.Text = lastDate.ToString()
          End If
       End With
    End Sub
    
    private void Calendar1_SelectionChanged (object sender,
       System.EventArgs e)
    {
       SelectedDatesCollection theDates = Calendar1.SelectedDates;
       if (theDates.Count > 1) 
       {
          DateTime firstDate = theDates[0];
          DateTime lastDate = theDates[theDates.Count-1];
          TextBox1.Text = firstDate.ToString();
          TextBox2.Text = lastDate.ToString();
       }
    }
    

여러 날짜 선택의 시간 범위를 얻으려면

  • TimeSpan 개체를 만들어 SelectedDates 컬렉션의 마지막 날짜와 처음 날짜의 차이로 설정한 다음 TimeSpan 개체의 Days 속성을 가져옵니다.

    Public Sub Calendar1_SelectionChanged(ByVal sender As Object, _
       ByVal e As System.EventArgs) Handles Calendar1.SelectionChanged
       With Calendar1.SelectedDates
          Dim days As Integer
          Dim firstDate As Date = .Item(0).Date
          Dim lastDate As Date = .Item(.Count - 1).Date
          ' The Subtract method returns a TimeSpan object.
          days = lastDate.Subtract(firstDate).Days + 1
          TextBox1.Text = "You have selected " & days.ToString() & " day(s)."
       End With
    End Sub
    
    private void Calendar1_SelectionChanged (object sender,
       System.EventArgs e)
    {
       SelectedDatesCollection theDates = Calendar1.SelectedDates;
       TimeSpan timeSpan = theDates[theDates.Count-1] - theDates[0];
       TextBox1.Text = 
          String.Format("You have selected {0} day(s).", timeSpan.Days + 1);
    }   
    

참고 항목

작업

방법: Calendar 웹 서버 컨트롤에서 프로그래밍 방식으로 날짜 선택

개념

Calendar 웹 서버 컨트롤 개요