Share via


ListView.AfterLabelEdit Kejadian

Definisi

Terjadi ketika label untuk item diedit oleh pengguna.

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 

Jenis Acara

Contoh

Contoh kode berikut menunjukkan cara menggunakan AfterLabelEdit peristiwa untuk membatasi label yang baru diedit ke karakter dalam alfabet. Contohnya menggunakan ASCIIEncoding kelas untuk mendapatkan kode karakter ASCII dari setiap karakter label baru. Jika karakter berada di antara kode ASCII yang mewakili angka, label baru tidak dapat diterapkan ke item. Contoh ini mengharuskan Anda membuat ListView kontrol pada formulir dan menambahkan item ke dalamnya. Contoh ini juga mengharuskan Anda telah menghubungkan AfterLabelEdit peristiwa ke penanganan aktivitas yang ditentukan dalam kode contoh. Untuk menggunakan ASCIIEncoding kelas , file Anda harus menyertakan System.Text namespace.

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

Keterangan

Peristiwa AfterLabelEdit terjadi ketika pengguna selesai memodifikasi teks untuk item. String baru yang jenis pengguna untuk item diteruskan ke peristiwa, dan penanganan aktivitas dapat menolak perubahan. Jika penanganan aktivitas menolak perubahan, teks akan kembali ke teks seperti sebelum pengguna mulai mengedit item.

Catatan

ListView.AfterLabelEdit Karena peristiwa terjadi sebelum pengeditan label diterapkan, memanggil ListView.Sort metode dalam handler untuk peristiwa ini akan mengurutkan item menggunakan nilai asli.

Agar AfterLabelEdit peristiwa dinaikkan, LabelEdit properti ListView kontrol harus diatur ke true.

Anda dapat membuat penanganan aktivitas agar BeforeLabelEdit peristiwa dapat melakukan tugas sebelum pengguna mengedit teks item.

Untuk informasi selengkapnya tentang menangani peristiwa, lihat Menangani dan Menaikkan Peristiwa.

Berlaku untuk

Lihat juga