Store variable value when reloading a Windows Form

0

Good morning,

I am developing an application in C # using Windows Forms and user controls, the application that I am developing is a small project for a restaurant which in the table interface, what it does is to click on the corresponding table changes its been either "busy" or "available." What happens is that by changing Form and returning to the tables they all return to "available".

There is some way that you can save the changes and do not go back to the default when you exit and re-enter the Form of tables.

I only save them as global variables:

class variables
{
    public static bool presionado ;
    public static bool presionado2 ;
    public static bool presionado3;
    public static bool presionado4;
    public static bool presionado5 ;
    public static bool presionado6 ;
    public static bool presionado7 ;
    public static bool presionado8 ;
    public static bool presionado9 ;
    public static bool presionado10 ;
    public static bool presionado11 ;
    public static bool presionado12 ;
}

Inside Form Tables:

if(variables.presionado == false)
{
    button1.BackColor = Color.Gold;
    variables.presionado = true;
}
else
{
    button1.BackColor = Color.Green;
    variables.presionado = false;
}
    
asked by Ezequie Lopez 06.09.2018 в 23:52
source

3 answers

4

I did the test, and it worked using a Static list and referencing all the buttons to button_Click , I also got all the controls of the form and saved them in a list where will I go and ask if it corresponds only  to the Tables , if they are Button and if they are in the list of occupied tables, I must clarify that until the application is closed to store the information but if it were to close for some reason it will no longer be possible to recover the information:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private IEnumerable<Control> ListaControles;
    private List<Button> ListaOcupados;

    public class Class1<T>
    {
        public static List<T> ListaOcupados { get; set; }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        ListaControles = obtenerControles(this, typeof(Button));
        ListaOcupados = new List<Button>();
        cargarListado();
    }

    private IEnumerable<Control> obtenerControles(Control control, Type tipo)
    {
        IEnumerable<Control> controles = control.Controls.Cast<Control>();
        return controles.SelectMany(x => obtenerControles(x, tipo))
                        .Concat(controles)
                        .Where(c => c.GetType() == tipo);
    }

    private void button_Click(object sender, EventArgs e)
    {
        if (!ListaOcupados.Any(x => x.Name == ((Button)sender).Name))
            ListaOcupados.Add((Button)sender);
        else
            ListaOcupados.RemoveAll(x => x.Name == ((Button)sender).Name);
        Class1<Button>.ListaOcupados = ListaOcupados;
        cargarListado();
    }

    private void cargarListado()
    {
        if (Class1<Button>.ListaOcupados != null)
        {
            if (Class1<Button>.ListaOcupados.Count > 0)
                ListaOcupados = Class1<Button>.ListaOcupados;
        }
        foreach (Control control in ListaControles)
        {
            if (control.GetType() == typeof(Button) && control.Text.Contains("Mesa"))
            {
                if (ListaOcupados.Any(x => x.Name == control.Name))
                    control.BackColor = Color.Red;
                else
                    control.BackColor = Color.Green;
            }
        }
    }
}
    
answered by 07.09.2018 / 01:16
source
0

When storing the information in variables, when the app is closed for some reason, since they are not persisted, they will be lost. To persist data you have many options, from database to JSON, XML and txt files.

Going back to the question.

The safest thing is that you are not correctly implementing the concept of global variables.

I recommend you implement the singleton pattern, where you have a class which has a single instance and you could access that instance globally.

link

  

Applicability

     

Use when:

     

There should be exactly one instance of a class and this should be   accessible to customers from a known access point.

     

The single instance should be extensible by inheritance and the   customers should be able to use an extended instance without   modify your code.

public class MesasPresionadas
{
    private static MesasPresionadas data;

    private MesasPresionadas()
    {
    }

    public static MesasPresionadas Instance()
    {
        if (data == null)
            data = new MesasPresionadas(); 
        return data;
    }

    public bool presionado{ get; set; }
    public bool presionado2{ get; set; }
    public bool presionado3{ get; set; }
    public bool presionado4{ get; set; }
    public bool presionado5{ get; set; }
    public bool presionado6{ get; set; }
    public bool presionado7{ get; set; }
    public bool presionado8{ get; set; }
    public bool presionado9{ get; set; }
    public bool presionado10{ get; set; }
    public bool presionado11{ get; set; }
    public bool presionado12{ get; set; }
}

And the way to access the data from any point would be

if (MesasPresionadas.Instance().presionado  == false)
{
    button1.BackColor = Color.Gold;
    MesasPresionadas.Instance().presionado = true;
}
else
{
    button1.BackColor = Color.Green;
    MesasPresionadas.Instance().presionado  = false;
}
    
answered by 07.09.2018 в 00:49
-1

With the IsolatedStorageFile class you can create virtual files to which only your application will have access and will only be visible by it. You could use it to save information about the last state of your application and its variables. Take a look at this link where an example of use comes.

Edit: I usually use it to store information such as the name of the last user who accessed or even his password. The reading and writing in the file is like the one that is done with an ordinary file.

Edited 2:

using System;
using System.IO;
using System.IO.IsolatedStorage;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);

            if (isoStore.FileExists("TestStore.txt"))
            {
                Console.WriteLine("The file already exists!");
                using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("TestStore.txt", FileMode.Open, isoStore))
                {
                    using (StreamReader reader = new StreamReader(isoStream))
                    {
                        Console.WriteLine("Reading contents:");
                        Console.WriteLine(reader.ReadToEnd());
                    }
                }
            }
            else
            {
                using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("TestStore.txt", FileMode.CreateNew, isoStore))
                {
                    using (StreamWriter writer = new StreamWriter(isoStream))
                    {
                        writer.WriteLine("Hello Isolated Storage");
                        Console.WriteLine("You have written to the file.");
                    }
                }
            }   
        }
    }
}

I hope it serves you. Greetings.

    
answered by 07.09.2018 в 00:22