Transfer values from one class to another using a list

  c++, list, visual-studio, windows, winforms

I am facing a problem in my winforms app I am building in Visual Studio 2019. My app has 8 forms and I constructed a new class in which I want to save the name of the forms that users visited through a list and save them in a .txt file. I am providing you the pieces of code that I am trying to implement.

Code of one of 8 forms

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SQLite;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Speech.Synthesis;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Delfoi_Tourist_Guide
{
    public partial class Welcome : Form
    {
        SQLiteConnection connection;
        private SpeechSynthesizer speech = new SpeechSynthesizer();
        public Welcome()
        {
            InitializeComponent();
            User_History.HistList.Add(this.Name);
            User_History.SaveHistory();
        }

        private void Welcome_Load(object sender, EventArgs e)
        {
            String conn = "Data Source=Delfoidb1.db;Version=3";
            connection = new SQLiteConnection(conn);
            connection.Open(); //or Create Database
        }

        private void Timer1_Tick(object sender, EventArgs e)
        {
            label1.Show();
            label2.Show();
            button1.Visible = true;
            button2.Visible = true;
            timer2.Enabled = true;
            timer1.Enabled = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (speech.State == SynthesizerState.Speaking)
            {
                speech.Pause();
            }
            Form2 form2 = new Form2();
            form2.Show();
            this.Visible = false;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            speech.SelectVoice("Microsoft Stefanos");
            speech.SpeakAsync(label2.Text);
            timer2.Enabled = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (speech.State == SynthesizerState.Speaking)
            {
                speech.Pause();
            }
            Form8 form8 = new Form8();
            form8.Show();
            this.Visible = false;
        }

        private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Developed by Ανδρέας Κρεούζος(ΜΠΠΛ20040) and Δημήτρης Γιογάκης(ΜΠΠΛ20008)");
        }

        private void helpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Implement try - catch to avoid exception specifically for url error
            try
            {
                Help.ShowHelp(this, "_tmphhp/Delfoi_Tourist_Guide_Help.chm", HelpNavigator.KeywordIndex, "Topic 1");
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.ToString());
            }
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (speech.State == SynthesizerState.Speaking)
            {
                speech.Pause();
            }
            Welcome welcome = new Welcome();
            welcome.Show();
            this.Visible = false;
        }

        private void έξοδοςToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}

Code of the separate class that saves the data as txt

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Delfoi_Tourist_Guide
{
    public static class User_History
    {
        public static List<string> HistList = new List<string>();

        public static void SaveHistory()
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                string path = saveFileDialog.FileName;
                StreamWriter sw = new StreamWriter(path);
                sw.WriteLine(HistList);
                sw.Close();
            }
        }
    }
}

My problem is that I can’t transfer the data (placed on the InitializeComponent) from forms to my class. I am getting my txt file with nothing in it. My data is actually saved on my public static list but I can’t transfer it next to the rest of my User_History class.

Any ideas on how to accomplish this???

Source: Windows Questions

LEAVE A COMMENT