Freigeben über


ListView.AfterLabelEdit-Ereignis

Tritt ein, wenn die Bezeichnung eines Elements vom Benutzer bearbeitet wird.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

Syntax

'Declaration
Public Event AfterLabelEdit As LabelEditEventHandler
'Usage
Dim instance As ListView
Dim handler As LabelEditEventHandler

AddHandler instance.AfterLabelEdit, handler
public event LabelEditEventHandler AfterLabelEdit
public:
event LabelEditEventHandler^ AfterLabelEdit {
    void add (LabelEditEventHandler^ value);
    void remove (LabelEditEventHandler^ value);
}
/** @event */
public void add_AfterLabelEdit (LabelEditEventHandler value)

/** @event */
public void remove_AfterLabelEdit (LabelEditEventHandler value)
JScript unterstützt die Verwendung von Ereignissen, aber nicht die Deklaration von neuen Ereignissen.

Hinweise

Das AfterLabelEdit-Ereignis tritt ein, wenn der Benutzer die Änderung des Texts für ein Element fertig stellt. Die neue, vom Benutzer für das Element eingegebene Zeichenfolge wird an das Ereignis übergeben, und der Ereignishandler kann die Änderung ablehnen. Wenn der Ereignishandler die Änderung ablehnt, wird der Text auf den ursprünglichen Text zurückgesetzt, der vor der Bearbeitung durch den Benutzer vorlag.

Hinweis

Da das ListView.AfterLabelEdit-Ereignis vor dem Commit der Beschriftungsbearbeitung ausgeführt wird, wird das Element anhand seines ursprünglichen Werts sortiert, wenn Sie die ListView.Sort-Methode in einem Handler aufrufen.

Damit das AfterLabelEdit-Ereignis ausgelöst werden kann, muss die LabelEdit-Eigenschaft des ListView-Steuerelements auf true festgelegt sein.

Sie können einen Ereignishandler für das BeforeLabelEdit-Ereignis erstellen, um vor der Bearbeitung des Elementtexts durch einen Benutzer Aufgaben auszuführen.

Weitere Informationen zum Behandeln von Ereignissen finden Sie unter Behandeln von Ereignissen.

Beispiel

Im folgenden Codebeispiel wird veranschaulicht, wie Sie mithilfe des AfterLabelEdit-Ereignisses eine neu bearbeitete Beschriftung auf alphabetische Zeichen beschränken. Im Beispiel wird der entsprechende ASCII-Zeichencode für alle Zeichen der neuen Beschriftung mit der ASCIIEncoding-Klasse abgerufen. Wenn der ASCII-Code eines Zeichens eine Ziffer darstellt, kann die neue Beschriftung nicht auf das Element angewendet werden. Für dieses Beispiel müssen Sie ein ListView-Steuerelement in einem Formular erstellt und diesem Elemente hinzugefügt haben. Zudem müssen Sie für dieses Beispiel das AfterLabelEdit-Ereignis dem im Beispielcode definierten Ereignishandler zugeordnet haben. Wenn Sie die ASCIIEncoding-Klasse verwenden möchten, muss die Datei den System.Text-Namespace enthalten.

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
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:
   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.get_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.get_Label().ToCharArray();

    // Check each character in the new label to determine if it is a number.
    for (int x = 0; x < temp.get_Length(); x++) {
        // Encode the character from the character array to its ASCII code.
        ubyte bc[] = ae.GetBytes(temp.get_Item(x).ToString());

        // Determine if the ASCII code is within the valid range 
        // of numerical values.
        if (Convert.ToInt32(bc.get_Item(0)) > 47 
            && Convert.ToInt32(bc.get_Item(0)) < 58) {
            // Cancel the event and return the lable to its original state.
            e.set_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;
        }
    }
} //MyListView_AfterLabelEdit

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

Siehe auch

Referenz

ListView-Klasse
ListView-Member
System.Windows.Forms-Namespace
OnAfterLabelEdit
BeforeLabelEdit
LabelEditEventHandler-Delegat
ListView.LabelEdit-Eigenschaft