Click here to Skip to main content
16,009,391 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
How to clear textboxes so that information is not seen
Posted
Updated 2-Jun-11 9:36am
v2
Comments
ambarishtv 2-Jun-11 6:07am    
??textBox.Text = string.Empty

hi,,

MIDL
textbox1.text="";
textbox1.text=string.empty;
textbox1.Clear();



you can use any of this
 
Share this answer
 
Comments
Kim Togo 2-Jun-11 15:41pm    
Many options. Drop the textbox.Text="" and only string.Empty. It is easier to read and understand.
Sergey Alexandrovich Kryukov 4-Jun-11 0:15am    
I agree, Kim. Not just this. No immediate constants, especially strings! This is not supportable, and "" is not a good excuse for exclusion. (I mean, using 0 or even 1 has good excuses, null is unavoidable but "" has no excuse. Probably string.Empty was devised just for that (this is not constant but immutable static variable)).
--SA
textbox1.clear();
OR

texbox.text="";


OR
textbox1.text=string.empty;

or

textbox1.text=null;

or
 
Share this answer
 
Comments
Kim Togo 2-Jun-11 15:40pm    
Many options. Drop the textbox.Text="" and only string.Empty. It is easier to read and understand.
Here is an complete example: Just copy to a new WindowsForms project and replace Program.cs content with following:

using System;
using System.Windows.Forms;

namespace ClearTextbox
{
    static class Program
    {
        static void Main()
        {
            // Create a form
            Form form = new Form();
            // ... and a Textbox
            TextBox textbox = new TextBox();
            textbox.Dock = DockStyle.Top;
            textbox.Text = "Clear me!";
            // Let's say you have a button "Clear"
            Button buttonClear = new Button();
            buttonClear.Text = "Clear";
            buttonClear.Dock = DockStyle.Top;
            // ... and maybe on Click
            buttonClear.Click += delegate(object sender, EventArgs e)
            {
                // you clear the TextBox
                textbox.Clear(); 
            };

            // Add the 2 Controls to the Form
            form.Controls.Add(buttonClear);
            form.Controls.Add(textbox);
            // ... and run it
            Application.Run(form);
        }
    }
}
 
Share this answer
 
Comments
Kim Togo 2-Jun-11 15:39pm    
My 5. TextBox.Clear method. Nice and simple
textbox1.Clear();

write it in code behind of any event
 
Share this answer
 
v2
Comments
Kim Togo 2-Jun-11 15:40pm    
My 5. TextBox.Clear method. Nice and simple.
Sergey Alexandrovich Kryukov 4-Jun-11 0:16am    
Sure, a 5.
--SA
textbox1.text=string.empty;
 
Share this answer
 
where you want to clear your textbox clear

textbox1.Text = "" ;
 
Share this answer
 
v3
Comments
Kim Togo 2-Jun-11 15:38pm    
Use the string.Empty. It is easier to read and understand.
textbox1.text="";

textbox1.text=string.empty;
 
Share this answer
 
v2
Comments
[no name] 2-Jun-11 6:11am    
IS WRITE THIS CODE BEHIND BUTTON?
Smithers-Jones 3-Jun-11 5:01am    
Why are you shouting?
rudrik8 2-Jun-11 6:19am    
u can write this code on button event or any function u want to call..
Kim Togo 2-Jun-11 15:38pm    
My 5. But always go for the string.Empty. It is easier to read and understand.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900