C++ cli 如何在单击按钮时读取csv文件中的下一行

C++ cli 如何在单击按钮时读取csv文件中的下一行,c++-cli,C++ Cli,我有一个windows窗体,有两个按钮和一个文本框。“我的开始”按钮读取csv文件中的第一行,并将我想要的数据输出到文本框中: private: System::Void StartBtn_Click(System::Object^ sender, System::EventArgs^ e) { String^ fileName = "same_para_diff_uprn1.csv"; StreamReader^ din = File::OpenText(fileName

我有一个windows窗体,有两个按钮和一个文本框。“我的开始”按钮读取csv文件中的第一行,并将我想要的数据输出到文本框中:

private: System::Void StartBtn_Click(System::Object^  sender, System::EventArgs^  e)
{ 
    String^ fileName = "same_para_diff_uprn1.csv";
    StreamReader^ din = File::OpenText(fileName);

    String^ delimStr = ",";
    array<Char>^ delimiter = delimStr->ToCharArray( );   
    array<String^>^ words;
    String^ str = din->ReadLine();

    words = str->Split( delimiter ); 

    textBox1->Text += gcnew String (words[10]);
    textBox1->Text += gcnew String ("\r\n"); 
    textBox1->Text += gcnew String (words[11]);
    textBox1->Text += gcnew String ("\r\n");
    textBox1->Text += gcnew String (words[12]);
    textBox1->Text += gcnew String ("\r\n");    
    textBox1->Text += gcnew String (words[13]);
private:System::Void StartBtn\u单击(System::Object^sender,System::EventArgs^e)
{ 
字符串^fileName=“same_para_diff_uprn1.csv”;
StreamReader ^din=文件::OpenText(文件名);
字符串^delimStr=“,”;
数组^delimiter=delimStr->ToCharArray();
数组^字;
字符串^str=din->ReadLine();
words=str->Split(分隔符);
textBox1->Text+=gcnewstring(单词[10]);
textBox1->Text+=gcnew字符串(“\r\n”);
textBox1->Text+=gcnewstring(单词[11]);
textBox1->Text+=gcnew字符串(“\r\n”);
textBox1->Text+=gcnewstring(单词[12]);
textBox1->Text+=gcnew字符串(“\r\n”);
textBox1->Text+=gcnewstring(单词[13]);
然后我的“下一步按钮”我希望它清除文本框,并显示如上所述的下一行数据。然后每次点击下一步按钮时,文本框将被清除,并显示csv文件的下一行。直到我到达文件末尾。我将如何管理它


TIA

您的问题是您的
按钮单击()
函数在完成后忘记了
StreamReader
对象和所有其他变量。
您需要使一些变量(至少
din
)独立于函数,将它们定义为WinForms对象的成员。无论何时调用函数,您都可以读取下一行。并且您需要添加一个检查
din
是否为null ptr(第一次调用时将如此),然后加载该文件,否则只需使用它:

StreamReader^ din;

private: System::Void StartBtn_Click(System::Object^  sender, System::EventArgs^  e)
{ 
    String^ fileName = "same_para_diff_uprn1.csv";
    if (!din)  // or: if (din == nullptr)
        din = File::OpenText(fileName);

    String^ delimStr = ",";
    ...