ListView.AfterLabelEdit Zdarzenie

Definicja

Występuje, gdy etykieta elementu jest edytowana przez użytkownika.

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 

Typ zdarzenia

Przykłady

W poniższym przykładzie kodu pokazano, jak za pomocą AfterLabelEdit zdarzenia ograniczyć nowo edytowaną etykietę do znaków alfabetu. W przykładzie użyto ASCIIEncoding klasy , aby uzyskać kod znaku ASCII każdego znaku nowej etykiety. Jeśli znak mieści się między kodami ASCII reprezentującymi liczby, nie można zastosować nowej etykiety do elementu. Ten przykład wymaga utworzenia kontrolki ListView w formularzu i dodania do niego elementów. Przykład wymaga również połączenia zdarzenia z AfterLabelEdit procedurą obsługi zdarzeń zdefiniowaną w przykładowym kodzie. Aby można było używać ASCIIEncoding klasy, plik musi zawierać System.Text przestrzeń nazw.

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

Uwagi

Zdarzenie AfterLabelEdit występuje, gdy użytkownik zakończy modyfikowanie tekstu elementu. Nowy ciąg, który użytkownik wpisze dla elementu, jest przekazywany do zdarzenia, a program obsługi zdarzeń może odrzucić zmianę. Jeśli program obsługi zdarzeń odrzuci zmianę, tekst zostanie przywrócony do tekstu, tak jak przed rozpoczęciem edytowania elementu przez użytkownika.

Uwaga

ListView.AfterLabelEdit Ponieważ zdarzenie ma miejsce przed zatwierdzeniu edycji etykiety, wywołanie ListView.Sort metody w procedurze obsługi dla tego zdarzenia spowoduje posortowanie elementu przy użyciu oryginalnej wartości.

Aby AfterLabelEdit zdarzenie zostało podniesione, LabelEdit właściwość kontrolki ListView musi być ustawiona na truewartość .

Można utworzyć program obsługi zdarzeń dla BeforeLabelEdit zdarzenia w celu wykonywania zadań, zanim użytkownik zmodyfikuje tekst elementu.

Aby uzyskać więcej informacji na temat obsługi zdarzeń, zobacz Obsługa i podnoszenie zdarzeń.

Dotyczy

Zobacz też