次の方法で共有


UpDownBase.Select(Int32, Int32) メソッド

定義

開始位置と選択する文字数を指定するスピン ボックス (アップダウン コントロールとも呼ばれます) 内のテキストの範囲を選択します。

public:
 void Select(int start, int length);
public void Select(int start, int length);
override this.Select : int * int -> unit
Public Sub Select (start As Integer, length As Integer)

パラメーター

start
Int32

選択する最初の文字の位置。

length
Int32

選択する文字数の合計。

次のコード例では、派生クラスの NumericUpDownを使用します。 このコードでは、フォームに NumericUpDown コントロールと Button が作成され、 System.Drawing 名前空間が参照として追加されている必要があります。 ボタンの Click イベントでは、 NumericUpDown コントロール内のテキストのポイント サイズが大きくなります。 これにより、すべてのテキストがコントロールに表示されるように、コントロールの PreferredHeight プロパティを調整するように求められます。 ユーザーが新しい値を入力し、 NumericUpDown コントロールを離れた後、テキストは文字列値から数値に変換され、 Minimum 値と Maximum 値の間にあると検証されます。 値が無効な場合は、 MessageBox がエラーと共に表示され、ユーザーが新しい値を入力できるように、 Select メソッドによってテキストが選択されます。

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 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 = gcnew System::Drawing::Font( "Microsoft Sans Serif",12.0,System::Drawing::FontStyle::Bold );
   MessageBox::Show( String::Format( "Before Font Change: {0}\nAfter Font Change: {1}", varPrefHeight1, numericUpDown1->PreferredHeight ) );
}
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());
}
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

注釈

Select メソッドは、スピン ボックスがフォーカスを取得したとき、または Text プロパティがデータの検証に失敗したときに使用できます。 派生クラスで ValidateEditText メソッドの検証コードを追加する場合は、検証が失敗したときに Select メソッドを呼び出します。

適用対象

こちらもご覧ください