ListView.AfterLabelEdit Evento
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Si verifica quando l'etichetta per un elemento viene modificata dall'utente.
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 evento
Esempio
Nell'esempio di codice seguente viene illustrato come usare l'evento AfterLabelEdit per limitare un'etichetta appena modificata ai caratteri nell'alfabeto. Nell'esempio viene utilizzata la ASCIIEncoding classe per ottenere il codice carattere ASCII di ogni carattere della nuova etichetta. Se il carattere rientra tra i codici ASCII che rappresentano numeri, la nuova etichetta non può essere applicata all'elemento. In questo esempio è necessario creare un ListView controllo in una maschera e aggiungervi elementi. L'esempio richiede inoltre di aver connesso l'evento AfterLabelEdit al gestore eventi definito nel codice di esempio. Per usare la ASCIIEncoding classe , il file deve includere lo System.Text spazio dei nomi .
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
Commenti
L'evento AfterLabelEdit si verifica quando l'utente termina la modifica del testo per un elemento. La nuova stringa che l'utente digita per l'elemento viene passata all'evento e il gestore eventi può rifiutare la modifica. Se il gestore eventi rifiuta la modifica, il testo viene ripristinato come prima che l'utente iniziasse a modificare l'elemento.
Annotazioni
Poiché l'evento viene eseguito prima del ListView.AfterLabelEdit commit della modifica dell'etichetta, la chiamata al ListView.Sort metodo in un gestore per questo evento ordina l'elemento usando il valore originale.
Affinché l'evento AfterLabelEdit venga generato, la LabelEdit proprietà del ListView controllo deve essere impostata su true.
È possibile creare un gestore eventi per l'evento BeforeLabelEdit per eseguire attività prima che l'utente modifichi il testo di un elemento.
Per ulteriori informazioni sulla gestione degli eventi, consultare gestione e generazione di eventi.