Control.Enter Kejadian

Definisi

Terjadi ketika kontrol dimasukkan.

public:
 event EventHandler ^ Enter;
public event EventHandler Enter;
public event EventHandler? Enter;
member this.Enter : EventHandler 
Public Custom Event Enter As EventHandler 

Jenis Acara

Contoh

Contoh kode berikut menggunakan Enter peristiwa untuk mengubah warna latar depan dan latar belakang dalam kondisi tertentu TextBox .

private:
   void textBox1_Enter( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      // If the TextBox contains text, change its foreground and background colors.
      if ( textBox1->Text != String::Empty )
      {
         textBox1->ForeColor = Color::Red;
         textBox1->BackColor = Color::Black;

         // Move the selection pointer to the end of the text of the control.
         textBox1->Select(textBox1->Text->Length,0);
      }
   }

   void textBox1_Leave( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      // Reset the colors and selection of the TextBox after focus is lost.
      textBox1->ForeColor = Color::Black;
      textBox1->BackColor = Color::White;
      textBox1->Select(0,0);
   }
private void textBox1_Enter(object sender, System.EventArgs e)
{
    // If the TextBox contains text, change its foreground and background colors.
    if (!string.IsNullOrEmpty(textBox1.Text))
    {
        textBox1.ForeColor = Color.Red;
        textBox1.BackColor = Color.Black;
        // Move the selection pointer to the end of the text of the control.
        textBox1.Select(textBox1.Text.Length, 0);
    }
}

private void textBox1_Leave(object sender, System.EventArgs e)
{
    // Reset the colors and selection of the TextBox after focus is lost.
    textBox1.ForeColor = Color.Black;
    textBox1.BackColor = Color.White;
    textBox1.Select(0,0);
}
    Private Sub textBox1_Enter(sender As Object, e As System.EventArgs) Handles textBox1.Enter
        ' If the TextBox contains text, change its foreground and background colors.
        If textBox1.Text <> [String].Empty Then
            textBox1.ForeColor = Color.Red
            textBox1.BackColor = Color.Black
            ' Move the selection pointer to the end of the text of the control.
            textBox1.Select(textBox1.Text.Length, 0)
        End If
    End Sub
   
   
    Private Sub textBox1_Leave(sender As Object, e As System.EventArgs) Handles textBox1.Leave
        ' Reset the colors and selection of the TextBox after focus is lost.
        textBox1.ForeColor = Color.Black
        textBox1.BackColor = Color.White
        textBox1.Select(0, 0)
    End Sub
End Class

Keterangan

Saat Anda mengubah fokus dengan menggunakan keyboard (TAB, SHIFT+TAB, dan sebagainya), dengan memanggil Select metode atau SelectNextControl , atau dengan mengatur ContainerControl.ActiveControl properti ke formulir saat ini, peristiwa fokus terjadi dalam urutan berikut:

  1. Enter

  2. GotFocus

  3. Leave

  4. Validating

  5. Validated

  6. LostFocus

Saat Anda mengubah fokus dengan menggunakan mouse atau dengan memanggil Focus metode , peristiwa fokus terjadi dalam urutan berikut:

  1. Enter

  2. GotFocus

  3. LostFocus

  4. Leave

  5. Validating

  6. Validated

CausesValidation Jika properti diatur ke false, Validating peristiwa dan Validated ditekan.

Catatan

Peristiwa Enter dan Leave ditekan oleh Form kelas . Peristiwa yang setara di Form kelas adalah Activated peristiwa dan Deactivate . Peristiwa Enter dan Leave bersifat hierarkis dan akan bertingkat ke atas dan ke bawah rantai induk sampai kontrol yang sesuai tercapai. Misalnya, asumsikan Anda memiliki Form dengan dua GroupBox kontrol, dan setiap GroupBox kontrol memiliki satu TextBox kontrol. Ketika tanda sisipan dipindahkan dari satu TextBox ke yang lain, Leave peristiwa dinaikkan untuk TextBox dan GroupBox, dan Enter peristiwa dinaikkan untuk yang lain GroupBox dan TextBox.

Perhatian

Jangan mencoba untuk mengatur fokus dari dalam Enterpenanganan aktivitas , , LeaveGotFocus, LostFocus, Validatingatau Validated . Melakukannya dapat menyebabkan aplikasi Anda atau sistem operasi berhenti merespons. Untuk informasi selengkapnya, lihat WM_KILLFOCUS topik di bagian "Referensi Input Keyboard" dan bagian "Kebuntuan Pesan" dari topik Tentang Pesan dan Antrean Pesan .

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

Berlaku untuk

Lihat juga