英語で読む

次の方法で共有


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

定義

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

public void Select(int start, int length);

パラメーター

start
Int32

選択される最初の文字の位置。

length
Int32

選択される文字の合計数。

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

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());
}

注釈

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

適用対象

製品 バージョン
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

こちらもご覧ください