ListView.AfterLabelEdit Evento

Definição

Ocorre quando o rótulo de um item é editado pelo usuário.

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 

Tipo de evento

Exemplos

O exemplo de código a seguir demonstra como usar o AfterLabelEdit evento para restringir um rótulo recém-editado a caracteres no alfabeto. O exemplo usa a ASCIIEncoding classe para obter o código de caractere ASCII de cada caractere do novo rótulo. Se o caractere ficar entre os códigos ASCII que representam números, o novo rótulo não poderá ser aplicado ao item. Este exemplo exige que você tenha criado um ListView controle em um formulário e adicionado itens a ele. O exemplo também exige que você tenha conectado o AfterLabelEdit evento ao manipulador de eventos definido no código de exemplo. Para usar a classe , seu ASCIIEncoding arquivo deve incluir o System.Text namespace.

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

Comentários

O AfterLabelEdit evento ocorre quando o usuário termina de modificar o texto de um item. A nova cadeia de caracteres que o usuário digita para o item é passada para o evento e o manipulador de eventos pode rejeitar a alteração. Se o manipulador de eventos rejeitar a alteração, o texto será revertido para o texto como era antes de o usuário começar a editar o item.

Observação

Como o ListView.AfterLabelEdit evento ocorre antes da edição do rótulo ser confirmada, chamar o ListView.Sort método em um manipulador para esse evento classificará o item usando o valor original.

Para que o AfterLabelEdit evento seja gerado, a LabelEdit propriedade do ListView controle deve ser definida truecomo .

Você pode criar um manipulador de eventos para o BeforeLabelEdit evento executar tarefas antes que o usuário edite o texto de um item.

Para obter mais informações sobre como lidar com eventos, consulte Manipulando e levantando eventos.

Aplica-se a

Confira também