1

我一直在花费数小时的研究试图找到适合我的项目但没有运气的解决方案。

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

namespace CustomerApplication
{
    public partial class ListAllCustomersForm : Form
    {   // default constructor method

    public ListAllCustomersForm()
    {
        InitializeComponent();
        /* Since this is the first method to be executed
         * call the displayCustomers method to populate
         * the list */


        displayCustomers();
    } // End of constructor

    // okButton Click method
    private void okBtn_Click(object sender, EventArgs e)
    {
        // retrieve hidden form's memory address
        Form myParentForm = CustomerAppStart.getParentForm();
        // now that we have the address use it to display the parent Form
        myParentForm.Show();
        //Finally close this form
        this.Close();
    } // end of okButton Click method
    //display the customers
    public void displayCustomers()
    {


        //format the header in the text box
        customerList.Text = ""; //Clear the textbox
        customerList.AppendText(
        "-------------------------------------------------- \r\n");
        customerList.AppendText(
        "Name Address Zip \r\n");
        customerList.AppendText(
        "-------------------------------------------------- \r\n");
        //local variables
        String name = "";
        String address = "";
        int zip = 0;
        //get all customers
        Customer[] myCustArray = Customer.getAllCustomers();
        //loop thru the array and display all the data


        for (int k = 0; k < myCustArray.Length; k++)
        {

            if (myCustArray[k] != null)
            {
               StreamWriter Writer = new StreamWriter(@"C:\Users\Jake\Documents\college\CIS340\data.csv", true);

                 Writer.Write(myCustArray[k].getCustomerName() + "," + myCustArray[k].getCustomerAddress() + "," + myCustArray[k].getCustomerZip() + ",");
                 Writer.Close();

                 StreamReader Reader = new StreamReader(@"C:\Users\Jake\Documents\college\CIS340\data.csv");
                 Reader.ReadToEnd();

                //get the customer attributes for this customer
                name = myCustArray[k].getCustomerName();
                address = myCustArray[k].getCustomerAddress();
                zip = myCustArray[k].getCustomerZip();
                //format the data
                name = name.PadRight(20, ' ');
                address = address.PadRight(23, ' ');
                //display it to the textBox
                this.customerList.AppendText(name + " " +
                address + " " + zip + "\r\n");
            }//end if not null

        }//end for loop
    }//end displayCustomers method


} // end of class definition
} // end of namespaces

这段代码允许我格式化我的文本框并给出我创建的数组标题。它还允许我将“AddCustomerForm.cs”中的任何用户输入写入 .CSV 文件,我可以在本地检查该文件并查看是否添加了值。

我遇到的问题是从我的 .CSV 文件中读取并将文本显示到我的格式化文本框中。

无论我尝试什么,我能看到的唯一输出是空白格式的表单,除非我尝试在同一个调试会话期间添加新客户,在这种情况下,它会在“AddCustomerForm.txt”中正确显示用户的输入。 CS”。

再一次,如果这似乎是一个新手问题,我很抱歉,我向你保证我已经研究了一段时间,但没有明确的解决方案。我将非常感谢任何人可以提供的任何帮助。

4

1 回答 1

0

您没有使用 Reader.ReadToEnd() 调用的结果,只是阅读它然后摆脱它们。

尝试使用 StreamReader 的ReadLine方法读取行。

于 2012-12-08T09:03:06.903 回答