Well, as far as I have been able to know, there is more than one possible answer for what you want to do, aki I am going to give you two solutions.
The first one is using the Windows Registry, in which you can save the counter and you can consult it later.
In the class of your main form you create a protected or private attribute:
//Variable donde se almacenara el contador que usas al imprimir
protected int contador;
Then you have to assign to the event Load
and FormClosed
of your main form the two methods that I am going to leave you next:
private void Form1_Load(object sender, EventArgs e)
{
//Con esto lees el valor almacenado en la clave HKEY_CURRENT_USER\SOFTWARE\NOMBRE_DE_TU_APP\config
contador = Convert.ToInt32(Registry.CurrentUser.CreateSubKey("SOFTWARE\"+Application.ProductName + "\config").GetValue("contador", 1));
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
//Con esto actualizas su valor, en caso de no existir la clave, la crea con el valor de contador actualizado
Registry.CurrentUser.CreateSubKey(Application.ProductName + "\config").SetValue("contador", this.contador+1);
}
With this you already have a solution. If you have not worked with the windows registry before or for security reasons do not want to use it you can save that value in an xml. Here I leave you as you would, as in the previous one you must assign the main form in the events Load
and FormClosed
the methods shown below.
First we would create a class called Configuration to store all the data that we might need in the future. In case your program keeps evolving.
public class Configuracion
{
//Constructor
public Configuracion()
{
//Inicializo el contador en 1
Contador = 1;
}
public int Contador;
}
Then in the class of your Main Form I add these attributes:
protected Configuracion config;
protected XmlSerializer serializer;
Remember to make use of the using in this case would be:
using System.IO;
using System.Xml.Serialization;
The first one is because I'm going to need to work with directories and with files and the second is to serialize my class inside an xml file that I can load later.
In the form's constructor you would have to put the following:
public Form1()
{
InitializeComponent();
//Inicializo el atributo de donde voy a leer el contador
config = new Configuracion();
//Creo el atributo que voy a emplear para deserializar y serializar la clase Configuracion
serializer = new XmlSerializer(Type.GetType("WindowsFormsApplication4.Configuracion"));
}
I must make it clear that if your space name is MiApp the last line should be
XmlSerializer(Type.GetType("MiApp.Configuracion"));
That is the space name of the Configuration class
Methods
private void Form1_Load(object sender, EventArgs e)
{
//Si existe el archivo config.xml dentro de la carpeta data en el directorio donde reside mi ejecutable...
if (File.Exists("data\config.xml"))
{
//Abro el archivo para escribir en el la instancia de la clase Configuracion serializada
using (StreamReader reader = new StreamReader("data\config.xml"))
{
config = (Configuracion)serializer.Deserialize(reader);
}
}
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
//Si no existe el archivo config.xml dentro de la carpeta data en el directorio donde reside tu ejecutable...
if (!File.Exists("data\config.xml"))
{
//Crea la carpeta data
Directory.CreateDirectory("data\");
}
//Actualizo el contador almacenado en la instancia de la clase Configuracion que tengo como atributo de mi formulario
config.Contador++;
//Abro el archivo para escribir en el la clase serializada
using (StreamWriter writer = new StreamWriter("data\config.xml"))
{
serializer.Serialize(writer, config);
}
}
Now depending on which of the 2 cases you have used your method would be as follows:
CASE 1 :
LNumDoc.Text = contador.ToString();
CASE 2 :
LNumDoc.Text = config.Contador.ToString();
I hope to have explained myself well. Any questions you already know