Language

ListView.AfterLabelEdit イベント

定義

アイテムのラベルがユーザーによって編集されたときに発生します。

public:
 event System::Windows::Forms::LabelEditEventHandler ^ AfterLabelEdit;
public event System.Windows.Forms.LabelEditEventHandler AfterLabelEdit;
public event System.Windows.Forms.LabelEditEventHandler? AfterLabelEdit;
member this.AfterLabelEdit : System.Windows.Forms.LabelEditEventHandler 
Public Custom Event AfterLabelEdit As LabelEditEventHandler 

イベントの種類

例

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

private:
   void MyListView_AfterLabelEdit( Object^ /*sender*/, System::Windows::Forms::LabelEditEventArgs^ e )
   {
      // Determine if label is changed by checking for 0.
      if ( e->Label == nullptr )
               return;

      // ASCIIEncoding is used to determine if a number character has been entered.
      ASCIIEncoding^ AE = gcnew ASCIIEncoding;

      // Convert the new label to a character array.
      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.
         array<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;
         }
      }
   }
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;
      }
   }
}
Private Sub MyListView_AfterLabelEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.LabelEditEventArgs) Handles listView1.AfterLabelEdit

   ' Determine if label is changed by checking to see if it is equal to Nothing.
   If e.Label Is Nothing Then
      Return
   End If
   ' ASCIIEncoding is used to determine if a number character has been entered.
   Dim AE As New ASCIIEncoding()
   ' Convert the new label to a character array.
   Dim temp As Char() = e.Label.ToCharArray()

   ' Check each character in the new label to determine if it is a number.
   Dim x As Integer
   For x = 0 To temp.Length - 1
      ' Encode the character from the character array to its ASCII code.
      Dim bc As Byte() = AE.GetBytes(temp(x).ToString())

      ' Determine if the ASCII code is within the valid range of numerical values.
      If bc(0) > 47 And bc(0) < 58 Then
         ' 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
      End If
   Next x
End Sub

注釈

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

Note

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

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

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

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

適用対象

こちらもご覧ください