ListView.OnAfterLabelEdit(LabelEditEventArgs) ListView.OnAfterLabelEdit(LabelEditEventArgs) ListView.OnAfterLabelEdit(LabelEditEventArgs) ListView.OnAfterLabelEdit(LabelEditEventArgs) Method

定義

AfterLabelEdit イベントを発生させます。Raises the AfterLabelEdit event.

protected:
 virtual void OnAfterLabelEdit(System::Windows::Forms::LabelEditEventArgs ^ e);
protected virtual void OnAfterLabelEdit (System.Windows.Forms.LabelEditEventArgs e);
abstract member OnAfterLabelEdit : System.Windows.Forms.LabelEditEventArgs -> unit
override this.OnAfterLabelEdit : System.Windows.Forms.LabelEditEventArgs -> unit
Protected Overridable Sub OnAfterLabelEdit (e As LabelEditEventArgs)

パラメーター

e
LabelEditEventArgs LabelEditEventArgs LabelEditEventArgs LabelEditEventArgs

イベント データを格納している LabelEditEventArgsA LabelEditEventArgs that contains the event data.

次のコード例は、使用する方法を示します、AfterLabelEdit新しく編集されたラベルをアルファベットの文字に制限するイベントです。The following code example demonstrates how to use the AfterLabelEdit event to restrict a newly edited label to characters in the alphabet. この例では、ASCIIEncodingクラスの新しいラベルの各文字の ASCII 文字コードを取得します。The example uses the ASCIIEncoding class to obtain the ASCII character code of each character of the new label. 文字が数字を表す ASCII コードの間にある場合は、項目に新しいラベルを適用できません。If the character falls between the ASCII codes that represent numbers, the new label cannot be applied to the item. この例では、作成済みである必要があります、ListViewフォームとそれに追加された項目へのコントロール。This example requires that you have created a ListView control on a form and added items to it. また、例では接続していることが必要です、AfterLabelEditイベントをコードの例で定義されているイベント ハンドラー。The example also requires that you have connected the AfterLabelEdit event to the event handler defined in the example code. 使用するには、 ASCIIEncoding 、ファイルに含める必要があります、クラス、System.Text名前空間。In order to use the ASCIIEncoding class, your file must include the System.Text namespace.

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

注釈

イベントを発生させると、イベント ハンドラーがデリゲートから呼び出されます。Raising an event invokes the event handler through a delegate. 詳細については、次を参照してください。処理とイベントの発生します。For more information, see Handling and Raising Events.

OnAfterLabelEdit メソッドを使用すると、デリゲートを結び付けずに、派生クラスでイベントを処理することもできます。The OnAfterLabelEdit method also allows derived classes to handle the event without attaching a delegate. 派生クラスでイベントを処理する場合は、この手法をお勧めします。This is the preferred technique for handling the event in a derived class.

注意 (継承者)

派生クラスで OnAfterLabelEdit(LabelEditEventArgs) をオーバーライドする場合は、登録されているデリゲートがイベントを受け取ることができるように、基本クラスの OnAfterLabelEdit(LabelEditEventArgs) メソッドを呼び出してください。When overriding OnAfterLabelEdit(LabelEditEventArgs) in a derived class, be sure to call the base class's OnAfterLabelEdit(LabelEditEventArgs) method so that registered delegates receive the event.

適用対象

こちらもご覧ください