ListView.AfterLabelEdit Událost

Definice

Nastane při úpravě popisku položky uživatelem.

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 použít AfterLabelEdit událost k omezení nově upraveného popisku 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, nelze nový popisek použít pro položku. Tento příklad vyžaduje, abyste ve formuláři vytvořili ListView ovládací prvek 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é použít ASCIIEncoding třídu, 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 odmítnout změnu. Pokud obslužná rutina události změnu odmítne, text se vrátí k textu tak, jak byl před tím, než uživatel začal upravovat položku.

Poznámka

Vzhledem k tomu, že ListView.AfterLabelEdit událost nastane před potvrzením úpravy popisku, volání ListView.Sort metody v obslužné rutině pro tuto událost seřadí položku pomocí původní hodnoty.

Aby se AfterLabelEdit událost vyvolala, LabelEdit musí být vlastnost ovládacího prvku nastavena ListView na truehodnotu .

Můžete vytvořit obslužnou rutinu BeforeLabelEdit události pro událost, která bude provádět úlohy předtím, než uživatel upraví text položky.

Další informace o zpracování událostí najdete v tématu Zpracování a vyvolávání událostí.

Platí pro

Viz také