You need to keep a reference date that you use to compare with the current date when you create the serial number.
If the current date is different from the reference date, you reset the serial number to 1.
You also need to store the last serial number, so you can close the application and continue with the next number in the sequence when you start it again.
You can use the Application Settings for this, see
Application Settings[
^]
You can also use other methods to store the variables, e.g. a text file or XML file.
When creating a new serial number:
if (DateTime.Now.Date > Properties.Settings.Default.ReferenceDate.Date)
{
Properties.Settings.Default.ReferenceDate = DateTime.Now;
Properties.Settings.Default.LastSerialNumber = 1;
}
else
{
Properties.Settings.Default.LastSerialNumber++;
}
When the program closes:
Properties.Settings.Default.Save();
Cannot swear that the syntax is 100% correct, but the principal should be right.
[UPDATE]
This code should do the job.
using System;
using System.Windows.Forms;
namespace SerialNumberReset
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
tbSerialNumber.Text = GetSerialNumber().ToString();
}
private int GetSerialNumber()
{
if (DateTime.Now.Date > Properties.Settings.Default.ReferenceDate.Date)
{
Properties.Settings.Default.ReferenceDate = DateTime.Now;
return 1;
}
else
{
return Properties.Settings.Default.SerialNumber;
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.Save();
}
private void buttonUpdateSerialNumber_Click(object sender, EventArgs e)
{
Properties.Settings.Default.SerialNumber = GetSerialNumber() + 1;
tbSerialNumber.Text = Properties.Settings.Default.SerialNumber.ToString();
}
}
}