0

I use Enter event in textbox to manage my placeholder but the Enter event fire when my form is show without user action.

How can I fix it ?

Form loginWindow = new Form();
loginWindow.Show();

private void IdTextBox_Enter(object sender, EventArgs e)
    {
        if (IdTextBox.Text == "Identifiant")
        {
            IdTextBox.Text = "";
            IdTextBox.ForeColor = Color.Black;
        }
    }
MrB3NiT0
  • 137
  • 2
  • 16
  • 6
    Is your TextBox the first one in the TabIndex order ? – Steve May 02 '18 at 09:38
  • yes it's my first TextBox in the form the second work well and his Enter event not fired – MrB3NiT0 May 02 '18 at 09:39
  • 3
    The enter event fires when the control gets focus - when your form starts the first control (your textbox) is going to get focus - so the event fires as it should. – PaulF May 02 '18 at 09:43
  • Ok so why IdTextBox.ContainsFocus = false and i can't write in the textbox ? – MrB3NiT0 May 02 '18 at 09:45
  • Without seeing the rest of the code for the form - it is difficult to say. Exactly what are you trying to achieve. – PaulF May 02 '18 at 09:50
  • As @PaulF says. Can you explain what is the problem if you enter this event handler? The if check will block any side effect and the event handler becomes harmless. – Steve May 02 '18 at 09:56
  • It's just a way to have a placeholder in my TextBox. When enter erase text when i leave if empty write my placeholder. No more code to show or maybe the leave event but it's the opposite of Enter event – MrB3NiT0 May 02 '18 at 10:00
  • 1
    See https://stackoverflow.com/a/5450496/939213 for placeholder text. – ispiro May 02 '18 at 10:12
  • I have created a little test form with 2 textboxes - on showing the form everything works correctly - "Identifiant" is cleared and I can write in the textbox. So I cannot understand why you cannot type in the textbox. The reason why ContainsFocus is false is at that point the control has not fully got the focus - that only happens when the GotFocus event fires - see answer from Baskar John. – PaulF May 02 '18 at 10:18
  • ho ok my bad @PaulF.. I can write in textbox but my placeholder is erase because of Enter event so my textbox is empty instead of have my placeholder – MrB3NiT0 May 02 '18 at 12:09
  • If you want the placeholder text to remain until text is typed in the box (& re-appear if all of the text is deleted) then look at the link posted by ispiro. You do not need to create a derived class - you can simply call the SendMessage method with _"IdTextBox.Handle"_ as the first parameter. – PaulF May 02 '18 at 12:25
  • Set `TabStop = false`. The focus won't be moved to the TextBox. – Jimi May 02 '18 at 12:29
  • thanks @Jimi it works perfectly. – MrB3NiT0 May 02 '18 at 12:38
  • Note that will stop your users from being able to Tab to that control - it will only be accessible by mouse click. If it only an issue when you first start the form, then you could reset TabStop to true in the Enter event. – PaulF May 02 '18 at 13:09
  • thank for that clarification @PaulF – MrB3NiT0 May 02 '18 at 13:46

0 Answers0