ListView.AfterLabelEdit Událost
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Nastane, když uživatel upraví popisek položky.
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
Event Type
Příklady
Následující příklad kódu ukazuje, jak pomocí AfterLabelEdit události omezit nově upravený popisek na znaky v abecedě. Příklad používá ASCIIEncoding třídu k získání kódu znaku ASCII každého znaku nového popisku. Pokud znak spadá mezi kódy ASCII, které představují čísla, nový popisek nelze u položky použít. Tento příklad vyžaduje, abyste vytvořili ListView ovládací prvek ve formuláři a přidali do něj položky. Příklad také vyžaduje, abyste událost připojili AfterLabelEdit k obslužné rutině události definované v ukázkovém kódu. Aby bylo možné třídu použít ASCIIEncoding , musí soubor obsahovat System.Text obor názvů.
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
Poznámky
K AfterLabelEdit události dojde, když uživatel dokončí úpravu textu položky. Nový řetězec, který uživatel zadá pro položku, se předá události a obslužná rutina události může změnu odmítnout. Pokud obslužná rutina události změnu odmítne, text se vrátí k textu tak, jak byl předtím, než uživatel začal upravovat položku.
Poznámka:
Vzhledem k tomu, že ListView.AfterLabelEdit událost probíhá před potvrzením úprav popisku, volání ListView.Sort metody v obslužné rutině pro tuto událost seřadí položku pomocí původní hodnoty.
Aby AfterLabelEdit byla událost vyvolána, LabelEdit musí být vlastnost ListView ovládacího prvku nastavena na truehodnotu .
Před úpravou textu položky můžete vytvořit obslužnou rutinu události, BeforeLabelEdit která má událost provádět úkoly.
Další informace o zpracování událostí naleznete v tématu Zpracování a vyvolávání událostí.