views:

49

answers:

1

How do I get to increment a textbox content on page load in such a way that if i run my application and if the form is getting loaded the value should be 0000001 and if i load the same form again with out closing my application it should be 0000002. If i close my application and run again the value should be 0000001

+3  A: 

You need to create a static counter in your Form.
The static modifier makes the variable common to all Form1 instances.

public partial class Form1 : Form
{
    private static int visitCounter = 0; // Common to all Form1
    ...
}

Then in the Form Load event you increase the counter and format the number as you wish.

private void Form1_Load(object sender, EventArgs e)
{
    visitCounter++; // Increase each time a form is loaded
    textBox1.Text = visitCounter.ToString("0000000"); // Format the counter
    ...
}
Albin Sunnanbo
Thanks a lot albin
Dorababu