0

I am new to winForms. This is the first time I am trying creating a textbox dynamically. It looks there is no problem creating it, but I have problem creating-using my own placeholder. After some search, I am still stuck and got no progress, thanks for any help.

public partial class Form1 : Form
    {
        Button buttonC=new Button();
        TextBox txtOrder = new TextBox();
        public Form1()
        {
            
            InitializeComponent();
       
            txtOrder.Top = 100;
            txtOrder.Left = 300;
            this.Controls.Add(txtOrder);

   
         }
       
        private void txtOrder_Enter(object sender, EventArgs e)
        {
            if (txtOrder.Text == "my_placeholder_text")
            {
                txtOrder.Text = "";
                txtOrder.ForeColor = Color.Black;
            }
        }


        private void txtOrder_Leave(object sender, EventArgs e)
        {
            if (txtOrder.Text == "")
            {
                txtOrder.Text = "my_placeholder_text";
                txtOrder.ForeColor = Color.LightBlue;  
            }
        }
Ken White
  • 123,280
  • 14
  • 225
  • 444
  • What you're referring to as *placeholder* is *officially* named CueBanner, set sending an `EM_SETCUEBANNER` message to the underlying Edit control. An example [here](https://stackoverflow.com/a/59803179/7444103) (the C# version, of course) -- BTW, I don't see you subscribing to the `Enter` and `Leave` events of your TextBox. – Jimi Apr 22 '22 at 00:16
  • I have been doing it using using visual texbox properties before ,i just heard about subscribing something ,and will learn what it is thank you sir – user15465584 Apr 22 '22 at 00:20
  • You just need to add `txtOrder.Enter += txtOrder_Enter;` and `txtOrder.Leave += txtOrder_Leave;` in the Form's Constructor (where you're setting the `Top` and `Left` properties). – Jimi Apr 22 '22 at 00:35

0 Answers0