英語で読む

次の方法で共有


ListView.AfterLabelEdit イベント

定義

項目のラベルがユーザーによって編集されると発生します。

C#
public event System.Windows.Forms.LabelEditEventHandler AfterLabelEdit;
C#
public event System.Windows.Forms.LabelEditEventHandler? AfterLabelEdit;

イベントの種類

次のコード例では、 イベントを AfterLabelEdit 使用して、新しく編集したラベルをアルファベットの文字に制限する方法を示します。 この例では、 クラスを ASCIIEncoding 使用して、新しいラベルの各文字の ASCII 文字コードを取得します。 文字が数値を表す ASCII コードの間にある場合、新しいラベルを項目に適用することはできません。 この例では、フォームにコントロールを ListView 作成し、それに項目を追加する必要があります。 この例では、サンプル コードで定義されている AfterLabelEdit イベント ハンドラーにイベントを接続している必要もあります。 クラスを使用 ASCIIEncoding するには、ファイルに 名前空間を System.Text 含める必要があります。

C#
private void MyListView_AfterLabelEdit(object sender, System.Windows.Forms.LabelEditEventArgs e)
{
 
   // Determine if label is changed by checking for null.
   if (e.Label == null)
      return;

   // ASCIIEncoding is used to determine if a number character has been entered.
   ASCIIEncoding AE = new ASCIIEncoding();
   // Convert the new label to a character array.
   char[] temp = e.Label.ToCharArray();

   // Check each character in the new label to determine if it is a number.
   for(int x=0; x < temp.Length; x++)
   {
      // Encode the character from the character array to its ASCII code.
      byte[] bc = AE.GetBytes(temp[x].ToString());
   
      // Determine if the ASCII code is within the valid range of numerical values.
      if(bc[0] > 47 && bc[0] < 58)
      {
         // Cancel the event and return the lable to its original state.
         e.CancelEdit = true;
         // Display a MessageBox alerting the user that numbers are not allowed.
         MessageBox.Show ("The text for the item cannot contain numerical values.");
         // Break out of the loop and exit.
         return;
      }
   }
}

注釈

イベントは AfterLabelEdit 、ユーザーがアイテムのテキストの変更を完了したときに発生します。 ユーザーが項目に入力した新しい文字列がイベントに渡され、イベント ハンドラーは変更を拒否できます。 イベント ハンドラーが変更を拒否した場合、テキストはユーザーがアイテムの編集を開始する前のテキストに戻ります。

注意

イベントはラベルの ListView.AfterLabelEdit 編集がコミットされる前に行われるため、このイベントの ListView.Sort ハンドラーで メソッドを呼び出すと、元の値を使用してアイテムが並べ替えられます。

イベントを AfterLabelEdit 発生させるには、コントロールの プロパティを LabelEditListView に設定する true必要があります。

ユーザーがアイテムのテキストを編集する BeforeLabelEdit 前に、イベントのイベント ハンドラーを作成してタスクを実行できます。

イベントの処理の詳細については、「処理とイベントの発生」を参照してください。

適用対象

製品 バージョン
.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

こちらもご覧ください