Programming

How to clear textbox after few seconds in c#

Considering you have a textbox that displays a message every time a control(button) is pushed. Each time the button gets pushed, some even will rise and you are displaying the result in a textbox.

Now if you want to automatically disappear the message on the textbox, following that event, you will need to set the textbox empty after a few seconds.

This post is particularly about how to clear textbox after few seconds in c# and I have added an example for demonstration purpose.

Steps to clear textbox after few seconds in c#

Basically, we have to take advantage of the Timer class (Windows.Forms.Timer) that will allow us to rise and event at user-defined intervals. Then we can use the Timer.Tickthat will occur when the given time interval has elapsed.

Make sure to use:

using System.Windows.Forms;

Then follow this:

public partial class Form1 : Form
{
     private Timer x = new Timer();

     public Form1()
     {
          x.Interval = (6000);  //1 second = 1000
          x.Tick += new EventHandler(TimerTask); //evoke your custom event
          x.Start();
     }

     // describe your method
}

Then create a event to clear your textbox or anything you want to perform after the configured time frame.

private void TimerMethod(object sender, EventArgs e)
{
    // do your job here
}

In this example, I needed to clear the string (message) from textbox.

How to clear a textbox in c#?

Just set the string to empty for the desired textbox.

TextboxName.Text = String.Empty;

Here’s the complete code required:

using System.Windows.Forms;

public partial class Form1 : Form
{
     private Timer x = new Timer();

     public Form1()
     {
          x.Interval = (6000);  //1 second = 1000
          x.Tick += new EventHandler(TimerTask); 
          x.Start();
     }

     private void TimerTask(object sender, EventArgs e)
     {
          TextboxName.Text = String.Empty;
     }
}

Clear textbox after few seconds in c# (Example)

I know it’s not necessary, most people already know what they are looking for, however, if you need an working example, I have added one here.

I tried this method with a demo application where I need to display a message after browsing and uploading a file. Depending on the uploading process a text box will display the message whether it’s a success message or warning.

But I need to flush or reset the textbox after 5 or 6 seconds every time the message appears. So I have a textbox in the form where the timer needs to do his work.

clear textbox after few seconds

And I add the exact same code as mentioned above in the .cs file;

C-code

Now every time I upload a file a message will be displayed for 6 seconds and then disappear on the UI. I have taken a few snapshot, you will find them in the slider below.

image-1
Select a file to upload
image-2
file ready for upload
image-3
Event Response
image-4
Message auto clear

How to reset a form after a few seconds?

If you have a form and want to reset it after a successful submission message in a specific time range then you can create a button_click_event like this;

For the form submit button;

protected void btnSubmit_click(object sender, EventArgs e) 
{
     textboxName.text = "Your message has been sent";
     textboxName.Visibile = true; // will display the message
     
     MainForm.Visible =false; // Hide the form 

     Response.AddHeader("Refresh", "4") // refresh the page in 4 sec
}

Set label content and make it disappear automatically

For a label to disappear automatically after a given time that can be reinvoke on a button click can be something like this.

  • Use a timer.
  • On callback, set the label content empty.
  • Restart the timer every time the button gets clicked.
  • … and the process continues.

The implementation using timer class would be;

private Timer timer;

private void btnClick(object sender, RoutedEventArgs e)
{
   Label = "Display your message here";
   
   if(timer == null)
   {
      timer = new Timer(state => Label = String.empty, null, 5_000, Timeout.Infinte);
   }

   else
   {
      timer.Change(5_000, Timeout.Infinte);
   }
} 

If you have other ideas to clear textbox after few seconds in c# or using JavaScript let us know in the comment box.

Similar tutorials:

how to remove intel delayed launcher

How to fix incorrect psk provided for network SSID error

How to fix Fitbit charge 2 not syncing

contributor
I am Rupam, a Computer Application graduate with a passion for gadgets and technology. GameSpec is a platform I created to share my knowledge on gadgets and software tutorials. On this site, you can find helpful troubleshooting guides to assist you in solving common problems with software and gadgets.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Want to share your thoughts?x
X