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 完成修改專案的文字時,就會發生此事件。 使用者為專案輸入的新字串會傳遞至 事件,而事件處理常式可以拒絕變更。 如果事件處理常式拒絕變更,文字會還原為文字,如同使用者開始編輯專案之前一樣。

注意

由於事件 ListView.AfterLabelEdit 會在認可標籤編輯之前發生,因此 ListView.Sort 呼叫這個事件的處理常式中的 方法會使用原始值來排序專案。

若要 AfterLabelEdit 引發 事件, LabelEdit 控制項的 ListView 屬性必須設定為 true

您可以為 BeforeLabelEdit 事件建立事件處理常式,以在使用者編輯專案的文字之前執行工作。

如需處理事件的詳細資訊,請參閱 處理和引發事件

適用於

另請參閱