【问题标题】:Transfer values from one class to another using a list使用列表将值从一个类传输到另一个类
【发布时间】:2021-11-20 18:58:56
【问题描述】:

我在 Visual Studio 2019 中构建的 winforms 应用程序遇到问题。我的应用程序有 8 个表单,我构建了一个新类,我想在其中保存用户通过列表访问的表单的名称并保存它们在 .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的单独类的代码

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 上)从表单传输到我的班级。我得到了我的 txt 文件,其中没有任何内容。我的数据实际上保存在我的公共静态列表中,但我无法将其传输到我的 User_History 类的其余部分。

关于如何做到这一点的任何想法???

【问题讨论】:

  • 如果你想将每个字符串写入文件,你应该遍历列表:foreach (String s in HistList) sw.WriteLine(s); 更短的方法是使用System.IO.File.WriteAllLines(path, HistList);

标签: c# windows list visual-studio winforms


【解决方案1】:

你可以试试这个 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);
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 2011-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多