C# 通过单击C中的按钮返回更新的列表

C# 通过单击C中的按钮返回更新的列表,c#,winforms,button,C#,Winforms,Button,我有一个简单的表格,有一个面板,上面有一个问题,有四个复选按钮作为答案。 用户将浏览表单并为每个问题选择答案。 一旦他们点击按钮接受答案,请点击下面的代码 答案被整合到一个名为answers的列表中,然后我将其写入结果并格式化,这样我就可以在.csv文件中写入一行。 一旦涵盖所有问题,用户将单击按钮下一步单击按钮 这将把结果写入.csv并退出应用程序。 不幸的是,我无法通过单击按钮来获取结果列表。 谢谢你的帮助/建议 using System; using System.IO; using Sy

我有一个简单的表格,有一个面板,上面有一个问题,有四个复选按钮作为答案。 用户将浏览表单并为每个问题选择答案。 一旦他们点击按钮接受答案,请点击下面的代码 答案被整合到一个名为answers的列表中,然后我将其写入结果并格式化,这样我就可以在.csv文件中写入一行。 一旦涵盖所有问题,用户将单击按钮下一步单击按钮 这将把结果写入.csv并退出应用程序。 不幸的是,我无法通过单击按钮来获取结果列表。 谢谢你的帮助/建议

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

namespace SIMPLE_FORM
{
    public partial class Form1 : Form
    {
        //public List<String> results = new List<String>();
    string myCsvFileTest = @"myFile.csv"


    // Button to update the answers list
        private void buttonNewAnswer_Click(object sender, EventArgs e)
        {

    // Algorithm to update the "answers" list

            var results = new StringBuilder();
            foreach (var i in answers)
            {
                results.AppendFormat("{0},", i.ToString());
            }
        }

    // Button to write the results to a .csv and then close the application
        private void buttonExit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Press \"Yes\" to confirm closing the Application", " ", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                using (StreamWriter writer = new StreamWriter(myCsvFileTest, true, Encoding.UTF8))
                {
                    writer.WriteLine(results);  
                }
                System.Windows.Forms.Application.Exit();
            }
            else
            {
                this.Activate();
            }
        }   
我正在尝试从按钮中获取结果列表。单击鼠标右键,然后在代码中的其他地方使用它,例如,单击鼠标右键写入.csv。您需要将结果对象声明为Form1类成员

现在,您正在buttonNewAnswer_Click函数中将其定义为局部变量,因此一旦函数结束,它就会被销毁

基于问题代码的简化代码:

public partial class Form1 : Form
{
    // declare and allocate
    StringBuilder results = new StringBuilder();

    private void buttonNewAnswer_Click(object sender, EventArgs e)
    {         
        // fill the results object
        foreach (var i in answers)
        {
            results.AppendFormat("{0},", i.ToString());
        }
    }

    private void buttonExit_Click(object sender, EventArgs e)
    {
        // you can use the result here.
        // results
    }   
}
您需要将结果对象声明为Form1类成员

现在,您正在buttonNewAnswer_Click函数中将其定义为局部变量,因此一旦函数结束,它就会被销毁

基于问题代码的简化代码:

public partial class Form1 : Form
{
    // declare and allocate
    StringBuilder results = new StringBuilder();

    private void buttonNewAnswer_Click(object sender, EventArgs e)
    {         
        // fill the results object
        foreach (var i in answers)
        {
            results.AppendFormat("{0},", i.ToString());
        }
    }

    private void buttonExit_Click(object sender, EventArgs e)
    {
        // you can use the result here.
        // results
    }   
}

将StringBuilder定义移到click事件之外,以便两个click方法都可以访问变量。例如,myCsvFileTest不在任何方法中。它位于Form类的全局空间中。将StringBuilder定义移到click事件之外,以便两个click方法都可以访问该变量。例如,myCsvFileTest不在任何方法中。它位于Form类的全局空间中。