選択する対象の文字の開始位置と文字数を指定する、アップダウン コントロールのテキストの範囲を選択します。
Overloads Public Sub Select( _
ByVal start As Integer, _ ByVal length As Integer _)
[C#]
public void Select(intstart,intlength);
[C++]
public: void Select(intstart,intlength);
[JScript]
public function Select(
start : int,length : int);
パラメータ
- start
選択される最初の文字の位置。 - length
選択される文字の合計数。
解説
アップダウン コントロールにフォーカスがある場合、または Text プロパティがデータの検証に失敗した場合は、 Select メソッドを使用できます。派生クラスの ValidateEditText メソッド用の検証コードを追加する場合は、検証が失敗したときに Select メソッドを呼び出すようにします。
使用例
[Visual Basic, C#, C++] 派生クラス NumericUpDown を使用する例を次に示します。このコードは、 NumericUpDown コントロールと Button がフォーム上に作成されていること、および System.Drawing 名前空間が参照として追加されていることを前提にしています。ボタンの Click が発生すると、 NumericUpDown コントロールのテキストのポイント数が増えます。ポイント数が増えると、コントロールにすべてのテキストを表示できるように、コントロールで PreferredHeight プロパティを調整するように促されます。ユーザーが新しい値を入力して NumericUpDown コントロールを終了すると、テキストが文字列値から数値へ変換され、 Minimum 値と Maximum 値の間にあるかどうかが検証されます。値が無効な場合は、 MessageBox にエラーが表示され、ユーザーが新しい値を入力できるように Select メソッドがテキストを選択します。
Private Sub numericUpDown1_Leave(sender As Object, e As EventArgs)
' If the entered value is greater than Minimum or Maximum,
' select the text and open a message box.
If (System.Convert.ToInt32(numericUpDown1.Text) > numericUpDown1.Maximum) Or _
(System.Convert.ToInt32(numericUpDown1.Text) < numericUpDown1.Minimum) Then
MessageBox.Show("The value entered was not between the Minimum and " & _
"Maximum allowable values." & Microsoft.VisualBasic.ControlChars.Cr & _
"Please re-enter.")
numericUpDown1.Focus()
numericUpDown1.Select(0, numericUpDown1.Text.Length)
End If
End Sub
Private Sub button1_Click(sender As Object, e As EventArgs)
Dim varPrefHeight1 As Integer
' Capture the PreferredHeight before and after the Font
' is changed, and display the results in a message box.
varPrefHeight1 = numericUpDown1.PreferredHeight
numericUpDown1.Font = New System.Drawing.Font("Microsoft Sans Serif", _
12F, System.Drawing.FontStyle.Bold)
MessageBox.Show("Before Font Change: " & varPrefHeight1.ToString() & _
Microsoft.VisualBasic.ControlChars.Cr & "After Font Change: " & _
numericUpDown1.PreferredHeight.ToString())
End Sub
[C#]
private void numericUpDown1_Leave(Object sender,
EventArgs e)
{
/* If the entered value is greater than Minimum or Maximum,
select the text and open a message box. */
if((System.Convert.ToInt32(numericUpDown1.Text) > numericUpDown1.Maximum) ||
(System.Convert.ToInt32(numericUpDown1.Text) < numericUpDown1.Minimum))
{
MessageBox.Show("The value entered was not between the Minimum and" +
"Maximum allowable values." + "\n" + "Please re-enter.");
numericUpDown1.Focus();
numericUpDown1.Select(0, numericUpDown1.Text.Length);
}
}
private void button1_Click(Object sender,
EventArgs e)
{
int varPrefHeight1;
/* Capture the PreferredHeight before and after the Font
is changed, and display the results in a message box. */
varPrefHeight1 = numericUpDown1.PreferredHeight;
numericUpDown1.Font = new System.Drawing.Font("Microsoft Sans Serif",
12F, System.Drawing.FontStyle.Bold);
MessageBox.Show("Before Font Change: " + varPrefHeight1.ToString() +
"\n" + "After Font Change: " + numericUpDown1.PreferredHeight.ToString());
}
[C++]
private:
void numericUpDown1_Leave(Object* /*sender*/,
EventArgs* /*e*/)
{
/* If the entered value is greater than Minimum or Maximum,
select the text and open a message box. */
if((System::Convert::ToInt32(numericUpDown1->Text) > numericUpDown1->Maximum) ||
(System::Convert::ToInt32(numericUpDown1->Text) < numericUpDown1->Minimum))
{
MessageBox::Show(S"The value entered was not between the Minimum andMaximum allowable values.\nPlease re-enter.");
numericUpDown1->Focus();
numericUpDown1->Select(0, numericUpDown1->Text->Length);
}
}
void button1_Click(Object* /*sender*/,
EventArgs* /*e*/)
{
int varPrefHeight1;
/* Capture the PreferredHeight before and after the Font
is changed, and display the results in a message box. */
varPrefHeight1 = numericUpDown1->PreferredHeight;
numericUpDown1->Font = new System::Drawing::Font(S"Microsoft Sans Serif",
12.0, System::Drawing::FontStyle::Bold);
MessageBox::Show(String::Format( S"Before Font Change: {0}\nAfter Font Change: {1}",
__box(varPrefHeight1), __box(numericUpDown1->PreferredHeight)));
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン
をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ
参照
UpDownBase クラス | UpDownBase メンバ | System.Windows.Forms 名前空間 | UpDownBase.Select オーバーロードの一覧 | ValidateEditText