ListView.OnAfterLabelEdit(LabelEditEventArgs) Metod
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Genererar händelsen 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)
Parametrar
En LabelEditEventArgs som innehåller händelsedata.
Exempel
Följande kodexempel visar hur du använder AfterLabelEdit händelsen för att begränsa en nyligen redigerad etikett till tecken i alfabetet. I exemplet används ASCIIEncoding klassen för att hämta ASCII-teckenkoden för varje tecken i den nya etiketten. Om tecknet hamnar mellan DE ASCII-koder som representerar tal kan den nya etiketten inte tillämpas på objektet. Det här exemplet kräver att du har skapat en ListView kontroll i ett formulär och lagt till objekt i det. Exemplet kräver också att du har anslutit AfterLabelEdit händelsen till händelsehanteraren som definierats i exempelkoden. För att kunna använda ASCIIEncoding klassen måste filen innehålla System.Text namnområdet.
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
Kommentarer
När du skapar en händelse anropas händelsehanteraren via ett ombud. Mer information finns i Hantera och höja händelser.
Metoden OnAfterLabelEdit gör det också möjligt för härledda klasser att hantera händelsen utan att koppla ett ombud. Det här är den bästa tekniken för att hantera händelsen i en härledd klass.
Anteckningar till arvingar
När du OnAfterLabelEdit(LabelEditEventArgs) åsidosätter i en härledd klass måste du anropa basklassens OnAfterLabelEdit(LabelEditEventArgs) metod så att registrerade ombud tar emot händelsen.