ListView.OnAfterLabelEdit(LabelEditEventArgs) Método

Definición

Genera el evento AfterLabelEdit.

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)

Parámetros

e
LabelEditEventArgs

Objeto LabelEditEventArgs que contiene los datos del evento.

Ejemplos

En el ejemplo de código siguiente se muestra cómo usar el AfterLabelEdit evento para restringir una etiqueta recién editada a caracteres del alfabeto. En el ejemplo se usa la ASCIIEncoding clase para obtener el código de caracteres ASCII de cada carácter de la nueva etiqueta. Si el carácter se encuentra entre los códigos ASCII que representan números, la nueva etiqueta no se puede aplicar al elemento. Este ejemplo requiere que haya creado un ListView control en un formulario y agregado elementos a él. El ejemplo también requiere que haya conectado el AfterLabelEdit evento al controlador de eventos definido en el código de ejemplo. Para usar la clase , el ASCIIEncoding archivo debe incluir el System.Text espacio de nombres .

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

Comentarios

Cuando se genera un evento, se invoca el controlador de eventos a través de un delegado. Para obtener más información, consulte controlar y provocar eventos.

El método OnAfterLabelEdit también permite que las clases derivadas controlen el evento sin adjuntar ningún delegado. Ésta es la técnica preferida para controlar el evento en una clase derivada.

Notas a los desarrolladores de herederos

Al reemplazar OnAfterLabelEdit(LabelEditEventArgs) en una clase derivada, asegúrese de llamar al método OnAfterLabelEdit(LabelEditEventArgs) de la clase base para que los delegados registrados reciban el evento.

Se aplica a

Consulte también