使用列表将值从一个 class 转移到另一个

Transfer values from one class to another using a list

我在 Visual Studio 2019 年构建的 winforms 应用程序中遇到问题。我的应用程序有 8 个表单,我构建了一个新的 class,我想在其中保存用户通过列表访问的表单,并将它们保存在 .txt 文件中。我正在为您提供我要实现的代码片段。

8种形式之一的代码

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();
        }
    }
}

将数据另存为txt

的单独代码class
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();
            }
        }
    }
}

我的问题是我无法将数据(放置在 InitializeComponent 上)从表单传输到我的 class。我正在获取我的 txt 文件,其中没有任何内容。我的数据实际上保存在我的 public 静态列表中,但我无法将它转移到 User_History class.

的其余部分旁边

关于如何实现这个的任何想法???

你可以试试这个 SaveHistory() 方法

public static void SaveHistory()
{
    SaveFileDialog saveFileDialog = new SaveFileDialog();
    if (saveFileDialog.ShowDialog() == DialogResult.OK)
    {
        string path = saveFileDialog.FileName;

        using (StreamWriter writer = new StreamWriter(path, false))
        {
            foreach (string history in HistList)
            {
                writer.WriteLine(history);
            }
        }
    }
}