次の方法で共有


ListView.OnAfterLabelEdit メソッド

AfterLabelEdit イベントを発生させます。

Protected Overridable Sub OnAfterLabelEdit( _
   ByVal e As LabelEditEventArgs _)
[C#]
protected virtual void OnAfterLabelEdit(LabelEditEventArgse);
[C++]
protected: virtual void OnAfterLabelEdit(LabelEditEventArgs* e);
[JScript]
protected function OnAfterLabelEdit(
   e : LabelEditEventArgs);

パラメータ

解説

イベントが発生すると、デリゲートを使用してイベント ハンドラが呼び出されます。詳細については、「 イベントの発生 」を参照してください。

OnAfterLabelEdit メソッドを使用すると、デリゲートを結び付けずに、派生クラスでイベントを処理することもできます。派生クラスでイベントを処理する場合は、この手法をお勧めします。

継承時の注意: 派生クラスで OnAfterLabelEdit をオーバーライドする場合は、登録されているデリゲートがイベントを受け取ることができるように、基本クラスの OnAfterLabelEdit メソッドを呼び出してください。

使用例

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

 
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 = 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

[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;
      }
   }
}

[C++] 
private:
   void MyListView_AfterLabelEdit(Object* /*sender*/, System::Windows::Forms::LabelEditEventArgs* e) {

      // Determine if label is changed by checking for 0.
      if (e->Label == 0)
         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 (S"The text for the item cannot contain numerical values.");
            // Break out of the loop and exit.
            return;
         }
      }
   }

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

ListView クラス | ListView メンバ | System.Windows.Forms 名前空間 | AfterLabelEdit | LabelEditEventArgs