語言

SelectedDatesCollection 類別

定義

封裝一組 DateTime 代表控制中 Calendar 選定日期的物件。 此類別無法獲得繼承。

public ref class SelectedDatesCollection sealed : System::Collections::ICollection
public sealed class SelectedDatesCollection : System.Collections.ICollection
type SelectedDatesCollection = class
    interface ICollection
    interface IEnumerable
Public NotInheritable Class SelectedDatesCollection
Implements ICollection
繼承
SelectedDatesCollection
實作

範例

以下程式碼範例示範如何程式化地使用 SelectedDatesCollection 類別來選擇控制項中的 Calendar 日期。

<%@ Page Language="C#"%>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
 
  void Page_Load(Object sender, EventArgs e) 
  {
    DisplayCalendar.VisibleDate = DisplayCalendar.TodaysDate;
  }

  void SelectButton_Click(Object sender, EventArgs e) 
  {

    int current_day = DisplayCalendar.VisibleDate.Day;
    int current_month = DisplayCalendar.VisibleDate.Month;
    int current_year = DisplayCalendar.VisibleDate.Year;

    DisplayCalendar.SelectedDates.Clear();
   
    // Iterate through the current month and add all Wednesdays to the 
    // SelectedDates collection of the Calendar control.
    for (int i = 1; i <= System.DateTime.DaysInMonth(current_year, current_month); i++)
    {
       DateTime currentDate = new DateTime(current_year, current_month, i);
       if (currentDate.DayOfWeek == DayOfWeek.Wednesday)
       {
         DisplayCalendar.SelectedDates.Add(currentDate);
       }
    }

     MessageLabel.Text = "Selection Count = " + DisplayCalendar.SelectedDates.Count.ToString();
 
  }

  void DisplayCalendar_SelectionChanged(Object sender, EventArgs e) 
  {
    MessageLabel.Text = "Selection Count = " + DisplayCalendar.SelectedDates.Count.ToString();
  }
 
</script> 
 
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
 
      <asp:calendar id="DisplayCalendar" runat="server"  
        selectionmode="DayWeekMonth" 
        onselectionchanged="DisplayCalendar_SelectionChanged" />
 
      <hr />
 
      <asp:button id="SelectButton"
        text="Select All Weds in Month" 
        onclick="SelectButton_Click"  
        runat="server"/> 
        
      <br/>
 
      <asp:label id="MessageLabel" 
        runat="server" />
 
    </form>
  </body>
</html>
<%@ Page Language="VB"%>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
 
  Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
 
    DisplayCalendar.VisibleDate = DisplayCalendar.TodaysDate
    
  End Sub

  Sub SelectButton_Click(ByVal sender As Object, ByVal e As EventArgs)

    Dim current_day As Integer = DisplayCalendar.VisibleDate.Day
    Dim current_month As Integer = DisplayCalendar.VisibleDate.Month
    Dim current_year As Integer = DisplayCalendar.VisibleDate.Year

    DisplayCalendar.SelectedDates.Clear()
   
    ' Iterate through the current month and add all Wednesdays to the 
    ' SelectedDates collection of the Calendar control.
    Dim i As Integer
    For i = 1 To System.DateTime.DaysInMonth(current_year, current_month)
    
      Dim currentDate As New DateTime(current_year, current_month, i)
      If currentDate.DayOfWeek = DayOfWeek.Wednesday Then
       
        DisplayCalendar.SelectedDates.Add(currentDate)
        
      End If
      
    Next

    MessageLabel.Text = "Selection Count = " + DisplayCalendar.SelectedDates.Count.ToString()
 
  End Sub

  Sub DisplayCalendar_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs)
  
    MessageLabel.Text = "Selection Count = " & DisplayCalendar.SelectedDates.Count.ToString()
  
  End Sub
 
</script> 
 
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
 
      <asp:calendar id="DisplayCalendar" runat="server"  
        selectionmode="DayWeekMonth" 
        onselectionchanged="DisplayCalendar_SelectionChanged" />
 
      <hr />
 
      <asp:button id="SelectButton"
        text="Select All Weds in Month" 
        onclick="SelectButton_Click"  
        runat="server"/> 
        
      <br/>
 
      <asp:label id="MessageLabel" 
        runat="server" />
 
    </form>
  </body>
</html>

備註

使用此類別程式管理一組 System.DateTime 代表控制中選定日期 Calendar 的物件集合。 此類別常用於新增或移除集合中的日期。

此收藏僅儲存整日期。 每個時間部分 System.DateTime 都被移除。 日期依升序儲存。 若有重複日期,則集合中只會儲存一個日期。

建構函式

名稱 Description
SelectedDatesCollection(ArrayList)

初始化一個以指定日期清單的新類別實例 SelectedDatesCollection 。

屬性

名稱 Description
Count

取得集合中的DateTime物件數量SelectedDatesCollection。

IsReadOnly

會獲得一個值,表示該 SelectedDatesCollection 集合是否為唯讀。

IsSynchronized

會獲得一個值,表示存取 SelectedDatesCollection 集合是否同步(執行緒安全)。

Item[Int32]

在集合中DateTime取得SelectedDatesCollection指定的索引物件。

SyncRoot

取得可用來同步存取 SelectedDatesCollection 集合的物件。

方法

名稱 Description
Add(DateTime)

將指定的 DateTime 物件附加到集合的 SelectedDatesCollection 末尾。

Clear()

移除收藏中的所有 DateTime 物件。

Contains(DateTime)

回傳一個值,表示集合 SelectedDatesCollection 是否包含指定 DateTime 物件。

CopyTo(Array, Int32)

從 SelectedDatesCollection 集合中複製項目到指定的 Array索引 ,從指定的索引開始。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetEnumerator()

回傳一個IEnumerator包含集合內DateTime所有SelectedDatesCollection物件的 -實作物件。

GetHashCode()

做為預設哈希函式。

(繼承來源 Object)
GetType()

取得目前實例的 Type。

(繼承來源 Object)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
Remove(DateTime)

從集合中移除指定的 DateTime 物件 SelectedDatesCollection 。

SelectRange(DateTime, DateTime)

將指定的日期範圍加入集合。SelectedDatesCollection

ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)

擴充方法

名稱 Description
AsParallel(IEnumerable)

啟用查詢的平行處理。

AsQueryable(IEnumerable)

將 IEnumerable 轉換成 IQueryable。

Cast<TResult>(IEnumerable)

將 IEnumerable 的項目轉換成指定的型別。

OfType<TResult>(IEnumerable)

根據指定的型別篩選 IEnumerable 的專案。

適用於

另請參閱