Evento ListView.AfterLabelEdit (System.Windows.Forms)

Cambia visualizzazione:
ScriptFree
Riferimento a .NET Framework
Evento ListView.AfterLabelEdit

Si verifica quando l'etichetta di un elemento viene modificata dall'utente.

Spazio dei nomi: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

Sintassi

Visual Basic - (Dichiarazione)
Public Event AfterLabelEdit As LabelEditEventHandler
Visual Basic (Utilizzo)
Dim instance As ListView
Dim handler As LabelEditEventHandler

AddHandler instance.AfterLabelEdit, handler

C#
public event LabelEditEventHandler AfterLabelEdit
C++
public:
event LabelEditEventHandler^ AfterLabelEdit {
	void add (LabelEditEventHandler^ value);
	void remove (LabelEditEventHandler^ value);
}
J#
/** @event */
public void add_AfterLabelEdit (LabelEditEventHandler value)

/** @event */
public void remove_AfterLabelEdit (LabelEditEventHandler value)

JScript
JScript supporta l'utilizzo di eventi ma non la dichiarazione di nuovi.
Note

L'evento AfterLabelEdit si verifica quando l'utente termina la modifica del testo di un elemento. La nuova stringa digitata dall'utente per l'elemento viene passata all'evento e il gestore eventi può rifiutare la modifica. In questo caso, viene ripristinato il testo esistente prima dell'inizio della modifica dell'elemento da parte dell'utente.

NotaNota

Dato che l'evento ListView.AfterLabelEdit ha luogo prima del commit della modifica dell'etichetta, la chiamata al metodo ListView.Sort in un gestore per questo evento consentirà di ordinare l'elemento con il valore originale.

Affinché l'evento AfterLabelEdit venga generato, è necessario che la proprietà LabelEdit del controllo ListView sia impostata su true.

È possibile creare un gestore eventi per l'evento BeforeLabelEdit per eseguire delle attività prima che l'utente modifichi il testo di un elemento.

Per ulteriori informazioni sulla gestione di eventi, vedere Utilizzo degli eventi.

Esempio

Nell'esempio di codice riportato di seguito viene illustrato l'utilizzo dell'evento AfterLabelEdit per limitare ai soli caratteri dell'alfabeto una nuova etichetta. Viene utilizzata la classe ASCIIEncoding per ottenere il codice di carattere ASCII relativo a ciascun carattere della nuova etichetta. Se il carattere rientra tra i codici ASCII che rappresentano numeri, la nuova etichetta non potrà essere applicata all'elemento. Per eseguire questo esempio è necessario che su un form sia stato creato un controllo ListView a cui siano stati aggiunti elementi. Si presuppone inoltre che l'evento AfterLabelEdit sia stato collegato al gestore eventi definito nel codice. Per utilizzare la classe ASCIIEncoding è necessario che nel file sia incluso lo spazio dei nomi System.Text.

Visual Basic
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

C#
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;
      }
   }
}

C++
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;
         }
      }
   }

J#
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

Piattaforme

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

.NET Framework non supporta tutte le versioni di ciascuna piattaforma. Per un elenco delle versioni supportate, vedere Requisiti di sistema.

Informazioni sulla versione

.NET Framework

Supportato in: 2.0 1.1 1.0
Vedere anche