C# .NET - how to increment the value in textbox automatically

Asked By Hari on 18-Aug-10 02:22 AM
end of post
Anand Malli replied to Hari on 18-Aug-10 02:42 AM
HI Hari,

It requires two things to achive this,

1) some event in which you want to incr the value of your textbox
2) constant which will decide you want to incr the value of textbox by by how many point or value

let me know with this
thxs
Sagar P replied to Hari on 18-Aug-10 02:53 AM
You can easily do it by using TIMER.... like;
On Form Load take a new timer and create it elapsed event like;

Timer myTimer = new Timer();
myTimer.Elapsed += new ElapsedEventHandler( DisplayTimeEvent );
myTimer.Interval = 1000;
myTimer.Start();
textBox1.Text = "0";


In elapsed event increment textbox value and display it again like;

public static void DisplayTimeEvent( object source, ElapsedEventArgs e )
{
    int number = Convert.ToInt32(textBox1.Text);
    textBox1.Text = Convert.ToString(number + 1);
}


So it will increment text box value automatically after every 1 sec....

You can also use Timer_Tick event as shown in this link;

http://www.c-sharpcorner.com/UploadFile/mahesh/WorkingwithTimerControlinCSharp11302005054911AM/WorkingwithTimerControlinCSharp.aspx
Anoop S replied to Hari on 18-Aug-10 03:05 AM
if you want increment in button click then use this code, set the default value of txtbox as your starting number

string strInput = TextBox1.Text;
 int incrValue = Convert.ToInt32(strInput);
 incrValue += 1;
 TextBox1.Text = incrValue.ToString();
Super Man replied to Hari on 18-Aug-10 09:24 AM

If you are using in asp.net project then use this code: )

 

   <script type="text/javascript" language="javascript">

 

     setInterval(automatic, 1000);

 

     function automatic() {

       alert(Date);

       document.getElementById("t1").value = parseInt( document.getElementById("t1").value) + 1;

     }

  

   </script>

 

<asp:TextBox ID="t1" runat="server" Text="0">

 </asp:TextBox>

 

Brett Fondale replied to Hari on 24-Aug-10 07:48 AM
This code increments integer values every second in a textBox or maskedTextBox using a timer.


private Timer timer = new Timer();

public void myTimer()  {

   timer.Interval = 1000;                      // timer interval set to 1 second

   timer.Tick += new EventHandler(timer_Tick);    // call to timer_Tick(sender, e)

   timer.Start();                            // start timer

   }

private int i = 0;                          // make i private and initialize to 0 , so i will not be set back to zero   
                                     // during iterations
public void timer_Tick(object sender, EventArgs e)  {

   if ( i <= 99 )  {

    this.maskedTextBox1.Text = i.ToString();     // passes int i to maskedTextBox1

    i += 1;                              // increment i by 1

    }

   if ( i >= 99 )  {

   timer.Stop();

   i = 0;      // (optional) ; sets the value of i back to zero after timer has stopped, so if a button click invokes the                             //  textBox a click after timer.Stop will repeat the timer

   }

}


If using a button to invoke, use myTimer() as myTimer() calls timer_Tick(sender, e) :

private void button1_Click(object sender, EventArgs e)
      {
        CrackTimer();
      }


Many tweaks can be made, this snippet just provides a basis.
chitra ganapathy replied to Hari on 20-Jul-11 03:21 AM
hello sir, ur post very useful to me...thank you very much.