none
how to remove text in textbox, when clicked in it

Answers

  • You can Remove text when TextBox Enter like as

     

    private void textBox_1_Enter(object sender, EventArgs e)

    {

          if(textBox_1.Text.Trim() != "" || textBox_1.Text != null)

        {

          textBox_1.Text = "";

        }

    }

    same code you can use for TextBox_Click event


    Thanks & Regards, PSWASULE
    Saturday, April 17, 2010 6:37 AM
  • Hi Sana,

    If you are reffering to clearing text in a text box upon clicking on it you will have to use the Click event of the text box. Have a look at the following code that performs the same.

    private void textBox1_Click(object sender, EventArgs e)
    {
      textBox1.Clear();
    }

    Hope this helps.

    Best Regards,

    Praneeth


    Praneeth Wickramasinghe (SCJP, SCWCD)
    Saturday, April 17, 2010 7:04 AM
  • Another interesting solution is selecting the whole text when the text box get the focus. i.e.
    bool alreadyFocused; 
     
    //... 
     
    textBox1.GotFocus += textBox1_GotFocus; 
    textBox1.MouseUp += textBox1_MouseUp; 
    textBox1.Leave += textBox1_Leave; 
     
    //... 
     
    void textBox1_Leave(object sender, EventArgs e) 
    { 
      alreadyFocused = false; 
    } 
     
     
    void textBox1_GotFocus(object sender, EventArgs e) 
    { 
      // Select all text only if the mouse isn't down. 
      // This makes tabbing to the textbox give focus. 
      if (MouseButtons == MouseButtons.None) 
      { 
        this.textBox1.SelectAll(); 
        alreadyFocused = true; 
      } 
    } 
     
    void textBox1_MouseUp(object sender, MouseEventArgs e) 
    { 
      // Web browsers like Google Chrome select the text on mouse up. 
      // They only do it if the textbox isn't already focused, 
      // and if the user hasn't selected all text. 
      if (!alreadyFocused && this.textBox1.SelectionLength == 0) 
      { 
        alreadyFocused = true; 
        this.textBox1.SelectAll(); 
      } 
    } 
    
    Reference: http://stackoverflow.com/questions/97459/automatically-select-all-text-on-focus-in-winforms-textbox (Automatically select all text on focus in WinForms TextBox)
    Saturday, April 17, 2010 9:10 AM

All replies

  • You can Remove text when TextBox Enter like as

     

    private void textBox_1_Enter(object sender, EventArgs e)

    {

          if(textBox_1.Text.Trim() != "" || textBox_1.Text != null)

        {

          textBox_1.Text = "";

        }

    }

    same code you can use for TextBox_Click event


    Thanks & Regards, PSWASULE
    Saturday, April 17, 2010 6:37 AM
  • Hi Sana,

    If you are reffering to clearing text in a text box upon clicking on it you will have to use the Click event of the text box. Have a look at the following code that performs the same.

    private void textBox1_Click(object sender, EventArgs e)
    {
      textBox1.Clear();
    }

    Hope this helps.

    Best Regards,

    Praneeth


    Praneeth Wickramasinghe (SCJP, SCWCD)
    Saturday, April 17, 2010 7:04 AM
  • Hi Sana,

    If you are reffering to clearing text in a text box upon clicking on it you will have to use the Click event of the text box. Have a look at the following code that performs the same.

     

    private
     void
     textBox1_Click(object
     sender, EventArgs e)
    {
    textBox1.Clear();
    }

     

    Hope this helps.

    Best Regards,

    Praneeth


    Praneeth Wickramasinghe (SCJP, SCWCD)
    Yes its work fine but i need what that it will clear only first time,not every time .how can i do this?
    Saturday, April 17, 2010 8:25 AM
  • I done it using if condition.Thanks :)

    Saturday, April 17, 2010 8:45 AM
  • You can do it by a flag like

    bool firstTime = true;
    private void textBox1_Click(object sender, EventArgs e)
    {
     if(firstTime)
     {
      firstTime = false;
      textBox1.Clear();
     }
    }
    
    • Proposed as answer by pswasule Saturday, April 17, 2010 8:53 AM
    Saturday, April 17, 2010 8:46 AM
  • You can do it by a flag like

    bool
     firstTime = true
    ;
    private
     void
     textBox1_Click(object
     sender, EventArgs e)
    {
     if
    (firstTime)
     {
     firstTime = false
    ;
     textBox1.Clear();
     }
    }
    
    


    Thanks :)

    i do this using below code

     

    if (textBox1.Text == "Please Enter Keywords ")
                {
                    textBox1.Clear();
                }

    Both work fine :)

    Saturday, April 17, 2010 8:53 AM
  • Ooops. one thing! what will happen if the user enter "Please Enter Keywords" as his/her keywords?!
    Saturday, April 17, 2010 8:59 AM
  • Ooops. one thing! what will happen if the user enter "Please Enter Keywords" as his/her keywords?!


    Ahan.... true okay me going to use

    bool firstTime = true;

     

    Saturday, April 17, 2010 9:03 AM
  • Another interesting solution is selecting the whole text when the text box get the focus. i.e.
    bool alreadyFocused; 
     
    //... 
     
    textBox1.GotFocus += textBox1_GotFocus; 
    textBox1.MouseUp += textBox1_MouseUp; 
    textBox1.Leave += textBox1_Leave; 
     
    //... 
     
    void textBox1_Leave(object sender, EventArgs e) 
    { 
      alreadyFocused = false; 
    } 
     
     
    void textBox1_GotFocus(object sender, EventArgs e) 
    { 
      // Select all text only if the mouse isn't down. 
      // This makes tabbing to the textbox give focus. 
      if (MouseButtons == MouseButtons.None) 
      { 
        this.textBox1.SelectAll(); 
        alreadyFocused = true; 
      } 
    } 
     
    void textBox1_MouseUp(object sender, MouseEventArgs e) 
    { 
      // Web browsers like Google Chrome select the text on mouse up. 
      // They only do it if the textbox isn't already focused, 
      // and if the user hasn't selected all text. 
      if (!alreadyFocused && this.textBox1.SelectionLength == 0) 
      { 
        alreadyFocused = true; 
        this.textBox1.SelectAll(); 
      } 
    } 
    
    Reference: http://stackoverflow.com/questions/97459/automatically-select-all-text-on-focus-in-winforms-textbox (Automatically select all text on focus in WinForms TextBox)
    Saturday, April 17, 2010 9:10 AM
  • Another interesting solution is selecting the whole text when the text box get the focus. i.e.
    bool
     alreadyFocused; 
     
    //... 
    
     
    textBox1.GotFocus += textBox1_GotFocus; 
    textBox1.MouseUp += textBox1_MouseUp; 
    textBox1.Leave += textBox1_Leave; 
     
    //... 
    
     
    void
     textBox1_Leave(object
     sender, EventArgs e) 
    { 
     alreadyFocused = false
    ; 
    } 
     
     
    void
     textBox1_GotFocus(object
     sender, EventArgs e) 
    { 
     // Select all text only if the mouse isn't down. 
    
     // This makes tabbing to the textbox give focus. 
    
     if
     (MouseButtons == MouseButtons.None) 
     { 
      this
    .textBox1.SelectAll(); 
      alreadyFocused = true
    ; 
     } 
    } 
     
    void
     textBox1_MouseUp(object
     sender, MouseEventArgs e) 
    { 
     // Web browsers like Google Chrome select the text on mouse up. 
    
     // They only do it if the textbox isn't already focused, 
    
     // and if the user hasn't selected all text. 
    
     if
     (!alreadyFocused && this
    .textBox1.SelectionLength == 0) 
     { 
      alreadyFocused = true
    ; 
      this
    .textBox1.SelectAll(); 
     } 
    } 
    
    
    Reference: http://stackoverflow.com/questions/97459/automatically-select-all-text-on-focus-in-winforms-textbox  (Automatically select all text on focus in WinForms TextBox )

    Let me check.... :)
    Saturday, April 17, 2010 10:28 AM