【问题标题】:When a form loads read txt file into array当表单将读取的 txt 文件加载到数组中时
【发布时间】:2009-04-17 15:03:41
【问题描述】:

我正在开发一个 C# 应用程序,当加载表单时,我希望它读取 txt 文件的内容并将其存储到数组中。接下来,当单击表单上的按钮时,我希望按钮单击事件访问数组。如何将数组传递给按钮单击事件?我下面的代码有一个错误“statusArray 在当前上下文中不存在”,并且与按钮单击事件中对数组的引用有关。我需要做什么?

苏珊

private void btnCompleted_Click(object sender, EventArgs e)
    {

        for (int i = 0; i < statusArray.Count; i++)
        {
            if (statusArray[i].Equals("Complete"))

                lstReports.Items.Add(statusArray[i-2]);

        }
    }

    private void Reports_Load(object sender, EventArgs e)
    {
        // declare variables
        string inValue;
        string data;
        ArrayList statusArray = new ArrayList();


        inFile = new StreamReader("percent.txt");

        // Read each line from the text file

        while ((inValue = inFile.ReadLine()) != null)
        {
            data = Convert.ToString(inValue);
            statusArray.Add(inValue);

        }

        // Close the text file
        inFile.Close();


    }

【问题讨论】:

    标签: c# arraylist


    【解决方案1】:

    将 ArrayList 作为成员变量存储在表单中,如下所示:

    private ArrayList statusArray = new ArrayList();
    
    private void btnCompleted_Click(object sender, EventArgs e) {
    
        for (int i = 0; i < statusArray.Count; i++)
        {
            if (statusArray[i].Equals("Complete"))
    
                lstReports.Items.Add(statusArray[i-2]);
    
        }
    }
    
    private void Reports_Load(object sender, EventArgs e)
    {
        // declare variables
        string inValue;
        string data;
    
        inFile = new StreamReader("percent.txt");
    
        // Read each line from the text file
    
        while ((inValue = inFile.ReadLine()) != null)
        {
            data = Convert.ToString(inValue);
            statusArray.Add(inValue);
    
        }
    
        // Close the text file
        inFile.Close();
    
    
    }
    

    【讨论】:

      【解决方案2】:

      将您的 arrayList 声明移到 Reports_Load(object sender, EventArgs e) 方法之外,这使它成为一个全局类。

      您可能还会发现List&lt;string&gt; 会更好地存储您的数据(强类型)

      【讨论】:

        【解决方案3】:

        ArrayList 与 Array 相同,如果您使用 .Net 2.0 或更高版本,ArrayLists 邪恶

        至于失败的原因:您的数组列表的范围为 Reports_Load() 函数。您想将其移至类级别,并声明为List&lt;string&gt;

        如果你真的想要一个数组,另一个选择是使用 File 类的.ReadAllLines() 方法。

        private string[] status;
        
        private void btnCompleted_Click(object sender, EventArgs e)
        {
            for (int i = 2; i < status.Length; i++)
            {
                if (status[i] == "Complete")
                    lstReports.Items.Add(status[i-2]);
        
            }
        }
        
        private void Reports_Load(object sender, EventArgs e)
        {
            status = File.ReadAllLines("percent.txt");
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-04-19
          相关资源
          最近更新 更多