SelectionRange 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 SelectionRange 類別的新實例。
多載
SelectionRange() |
初始化 SelectionRange 類別的新實例。 |
SelectionRange(SelectionRange) |
使用指定的選取範圍,初始化 SelectionRange 類別的新實例。 |
SelectionRange(DateTime, DateTime) |
使用指定的開始和結束日期,初始化 SelectionRange 類別的新實例。 |
SelectionRange()
初始化 SelectionRange 類別的新實例。
public:
SelectionRange();
public SelectionRange ();
Public Sub New ()
範例
下列範例會建立 SelectionRange 對象、設定其 Start 和 End 屬性,並將 SelectionRange 物件指派給 MonthCalendar 控件的 SelectionRange 屬性。 引發 DateChanged 事件時,文本框會顯示 Start 和 End 屬性值。 此範例假設您有一個具有兩個 TextBox 控件、Button和 MonthCalendar 控件的 Form。
private:
void button1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
// Create a SelectionRange object and set its Start and End properties.
SelectionRange^ sr = gcnew SelectionRange;
sr->Start = DateTime::Parse( this->textBox1->Text );
sr->End = DateTime::Parse( this->textBox2->Text );
/* Assign the SelectionRange object to the
SelectionRange property of the MonthCalendar control. */
this->monthCalendar1->SelectionRange = sr;
}
void monthCalendar1_DateChanged( Object^ /*sender*/, DateRangeEventArgs^ /*e*/ )
{
/* Display the Start and End property values of
the SelectionRange object in the text boxes. */
this->textBox1->Text = monthCalendar1->SelectionRange->Start.Date.ToShortDateString();
this->textBox2->Text = monthCalendar1->SelectionRange->End.Date.ToShortDateString();
}
private void button1_Click(object sender, System.EventArgs e)
{
// Create a SelectionRange object and set its Start and End properties.
SelectionRange sr = new SelectionRange();
sr.Start = DateTime.Parse(this.textBox1.Text);
sr.End = DateTime.Parse(this.textBox2.Text);
/* Assign the SelectionRange object to the
SelectionRange property of the MonthCalendar control. */
this.monthCalendar1.SelectionRange = sr;
}
private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
{
/* Display the Start and End property values of
the SelectionRange object in the text boxes. */
this.textBox1.Text =
monthCalendar1.SelectionRange.Start.Date.ToShortDateString();
this.textBox2.Text =
monthCalendar1.SelectionRange.End.Date.ToShortDateString();
}
Private Sub button1_Click(sender As Object, _
e As EventArgs) Handles button1.Click
' Create a SelectionRange object and set its Start and End properties.
Dim sr As New SelectionRange()
sr.Start = DateTime.Parse(Me.textBox1.Text)
sr.End = DateTime.Parse(Me.textBox2.Text)
' Assign the SelectionRange object to the
' SelectionRange property of the MonthCalendar control.
Me.monthCalendar1.SelectionRange = sr
End Sub
Private Sub monthCalendar1_DateChanged(sender As Object, _
e As DateRangeEventArgs) Handles monthCalendar1.DateChanged
' Display the Start and End property values of
' the SelectionRange object in the text boxes.
Me.textBox1.Text = monthCalendar1.SelectionRange.Start.Date.ToShortDateString()
Me.textBox2.Text = monthCalendar1.SelectionRange.End.Date.ToShortDateString()
End Sub
備註
使用這個建構函式時,Start 和 End 值會設定為 null
。
適用於
SelectionRange(SelectionRange)
使用指定的選取範圍,初始化 SelectionRange 類別的新實例。
public:
SelectionRange(System::Windows::Forms::SelectionRange ^ range);
public SelectionRange (System.Windows.Forms.SelectionRange range);
new System.Windows.Forms.SelectionRange : System.Windows.Forms.SelectionRange -> System.Windows.Forms.SelectionRange
Public Sub New (range As SelectionRange)
參數
- range
- SelectionRange
現有的 SelectionRange。
範例
下列範例會建立 SelectionRange 對象、設定其 Start 和 End 屬性,並將 SelectionRange 物件指派給 MonthCalendar 控件的 SelectionRange 屬性。 引發 DateChanged 事件時,文本框會顯示 Start 和 End 屬性值。 此範例假設您有一個具有兩個 TextBox 控件、Button和 MonthCalendar 控件的 Form。
private:
void button1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
// Create a SelectionRange object and set its Start and End properties.
SelectionRange^ sr = gcnew SelectionRange;
sr->Start = DateTime::Parse( this->textBox1->Text );
sr->End = DateTime::Parse( this->textBox2->Text );
/* Assign the SelectionRange object to the
SelectionRange property of the MonthCalendar control. */
this->monthCalendar1->SelectionRange = sr;
}
void monthCalendar1_DateChanged( Object^ /*sender*/, DateRangeEventArgs^ /*e*/ )
{
/* Display the Start and End property values of
the SelectionRange object in the text boxes. */
this->textBox1->Text = monthCalendar1->SelectionRange->Start.Date.ToShortDateString();
this->textBox2->Text = monthCalendar1->SelectionRange->End.Date.ToShortDateString();
}
private void button1_Click(object sender, System.EventArgs e)
{
// Create a SelectionRange object and set its Start and End properties.
SelectionRange sr = new SelectionRange();
sr.Start = DateTime.Parse(this.textBox1.Text);
sr.End = DateTime.Parse(this.textBox2.Text);
/* Assign the SelectionRange object to the
SelectionRange property of the MonthCalendar control. */
this.monthCalendar1.SelectionRange = sr;
}
private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
{
/* Display the Start and End property values of
the SelectionRange object in the text boxes. */
this.textBox1.Text =
monthCalendar1.SelectionRange.Start.Date.ToShortDateString();
this.textBox2.Text =
monthCalendar1.SelectionRange.End.Date.ToShortDateString();
}
Private Sub button1_Click(sender As Object, _
e As EventArgs) Handles button1.Click
' Create a SelectionRange object and set its Start and End properties.
Dim sr As New SelectionRange()
sr.Start = DateTime.Parse(Me.textBox1.Text)
sr.End = DateTime.Parse(Me.textBox2.Text)
' Assign the SelectionRange object to the
' SelectionRange property of the MonthCalendar control.
Me.monthCalendar1.SelectionRange = sr
End Sub
Private Sub monthCalendar1_DateChanged(sender As Object, _
e As DateRangeEventArgs) Handles monthCalendar1.DateChanged
' Display the Start and End property values of
' the SelectionRange object in the text boxes.
Me.textBox1.Text = monthCalendar1.SelectionRange.Start.Date.ToShortDateString()
Me.textBox2.Text = monthCalendar1.SelectionRange.End.Date.ToShortDateString()
End Sub
備註
Start 和 End 屬性值會指派指派 SelectionRange 物件的 Start 和 End 屬性值。
另請參閱
適用於
SelectionRange(DateTime, DateTime)
使用指定的開始和結束日期,初始化 SelectionRange 類別的新實例。
public:
SelectionRange(DateTime lower, DateTime upper);
public SelectionRange (DateTime lower, DateTime upper);
new System.Windows.Forms.SelectionRange : DateTime * DateTime -> System.Windows.Forms.SelectionRange
Public Sub New (lower As DateTime, upper As DateTime)
參數
- lower
- DateTime
SelectionRange中的開始日期。
- upper
- DateTime
SelectionRange中的結束日期。
範例
下列範例會根據按兩下 Button 時輸入兩個 TextBox 控件的兩個日期,設定 MonthCalendar 控件的 SelectionRange 屬性。 此程式代碼假設 MonthCalendar 控件的新實例、兩個 TextBox 控件,以及已在 Form上建立 Button。 您可以考慮新增程式碼來驗證指派給文字框的 Text,以確認它們是否包含有效的日期。
private:
void button1_Click( Object^ sender, EventArgs^ e )
{
// Set the SelectionRange with start and end dates from text boxes.
try
{
monthCalendar1->SelectionRange = gcnew SelectionRange(
DateTime::Parse( textBox1->Text ),
DateTime::Parse( textBox2->Text ) );
}
catch ( Exception^ ex )
{
MessageBox::Show( ex->Message );
}
}
private void button1_Click(object sender,
EventArgs e)
{
// Set the SelectionRange with start and end dates from text boxes.
try
{
monthCalendar1.SelectionRange = new SelectionRange(
DateTime.Parse(textBox1.Text),
DateTime.Parse(textBox2.Text));
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Private Sub button1_Click(sender As Object, e As EventArgs)
' Set the SelectionRange with start and end dates from text boxes.
Try
monthCalendar1.SelectionRange = New SelectionRange( _
DateTime.Parse(textBox1.Text), _
DateTime.Parse(textBox2.Text))
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub