如何在Windows窗体应用程序中打开txt文件并将行导入文本框
How to open a txt file and import lines to textbox in Windows Forms application
我目前使用的代码本质上是:
private: System::Void toolStripMenuItem3_Click(System::Object^ sender, System::EventArgs^ e) {
OpenFileDialog^ ofd = gcnew OpenFileDialog();
if (ofd->ShowDialog() == System::Windows::Forms::DialogResult::OK) {
//Open file and set to textBox1 - textBox7
}
}
我想做的是打开一个文本文件,然后逐行读取,以指定该行应该进入哪个文本框,数字为1-7。我应该如何在VS Windows窗体应用程序中执行此操作?
这被硬编码为7行,不检查文件末尾读取等。
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
cli::array<TextBox^>^ tbArray = gcnew cli::array<TextBox^>(7)
{
textBox1, textBox2, textBox3, textBox4, textBox5, textBox6, textBox7
};
OpenFileDialog^ ofd = gcnew OpenFileDialog();
if (ofd->ShowDialog() == System::Windows::Forms::DialogResult::OK) {
//Open file and set to textBox1 - textBox7
StreamReader^ dataFile = File::OpenText(ofd->FileName);
for (int i = 0; i < 7; i++)
{
tbArray[i]->Text = dataFile->ReadLine()->Trim();
}
dataFile->Close();
dataFile->Dispose();
}
}
相关文章:
- 从文件导入数据时遇到问题.我有一个没完没了的循环
- 使用 GDAL 将 DXF 块从一个文件导入到另一个文件
- 如何将COM .tlb文件导入具有相对路径C++文件中?
- C++将分隔文件导入矢量
- 将 .ui 文件导入 Qt5 C++时出错"was not declared in this scope"
- 为什么我无法将头文件导入到其中一个头文件中的库中?
- 将 Obj 文件导入 vtk
- 在编译时将文本文件导入原始字符串文本
- 从文件导入注册表
- QComplete-如何从文件导入数据
- 如何从 .so 文件导入 python 模块
- 将.csv文件导入 QT GUI 上的 sqlite3 数据库表
- 这是将csv文件导入MS Access的正确SQL语法吗
- 从文本文件导入的不同线类型创建变量
- 如果使用LoadLibrary显式链接dll,则列出从PE文件导入的函数
- 如何将CSV文件导入QTableWidget
- 使用C++从MAT文件导入三维阵列
- C++ 操作方法:从.txt或.csv文件导入数值变量
- 为什么我将数字数据从文本文件导入二维数组的代码不起作用
- 将值从*.dat文件导入数组