Control.Enter Событие
Определение
Важно!
Некоторые сведения относятся к предварительной версии продукта, в которую до выпуска могут быть внесены существенные изменения. Майкрософт не предоставляет никаких гарантий, явных или подразумеваемых, относительно приведенных здесь сведений.
Происходит при вводе элемента управления.
public:
event EventHandler ^ Enter;
public event EventHandler Enter;
public event EventHandler? Enter;
member this.Enter : EventHandler
Public Custom Event Enter As EventHandler
Тип события
Примеры
В следующем примере кода событие используется Enter для изменения цветов переднего плана и фона 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
Комментарии
При изменении фокуса с помощью клавиатуры (TAB, SHIFT+TAB и т. д.), вызывая Select методы или SelectNextControlContainerControl.ActiveControl вызывая свойство текущей формы, события фокуса происходят в следующем порядке:
При изменении фокуса с помощью мыши или вызова Focus метода события фокуса происходят в следующем порядке:
CausesValidation Если для свойства задано значение false, Validating события Validated и события подавляются.
Замечание
Leave События Enter подавляются классомForm. Эквивалентные события в Form классе — это Activated и Deactivate события. И Enter события являются иерархическими и Leave будут каскадно вверх и вниз родительской цепочки, пока не будет достигнут соответствующий элемент управления. Например, предположим, что у вас есть два GroupBox элемента управления, и каждый GroupBox элемент управления имеет Form один TextBox элемент управления. При перемещении курсора с одного TextBox на другое Leave событие вызывается для TextBox и GroupBox, и Enter событие поднимается для другого GroupBox и TextBox.
Предостережение
Не пытайтесь задать фокус из Enterобработчиков событий , , , , ValidatingLostFocusGotFocusLeaveValidated а также с помощью обработчиков событий. Это может привести к тому, что приложение или операционная система перестают отвечать. Дополнительные сведения см WM_KILLFOCUS . в разделе "Справочник по вводу клавиатуры" и в разделе "Взаимоблокировка сообщений" раздела " Сообщения и очереди сообщений ".
Дополнительные сведения об обработке событий см. в разделе "Обработка и создание событий".