流 reader 无法将文本从文件文本获取到文本框
Stream reader cannot get the text from file text to a textbox
try
{
Form frmShow = new Form();
TextBox txtShowAll = new TextBox();
frmShow.StartPosition = FormStartPosition.CenterScreen;
frmShow.Font = this.Font;
frmShow.Size = this.Size;
frmShow.Icon = this.Icon;
frmShow.Text = "All data";
txtShowAll.Dock = DockStyle.Fill;
txtShowAll.Multiline = true;
frmShow.Controls.Add(txtShowAll);
frmShow.ShowDialog();
StreamReader r = new StreamReader("empData.txt");
string strShowAllData = r.ReadToEnd();
txtShowAll.Text = strShowAllData;
r.Close();
}
catch (Exception x)
{
MessageBox.Show(x.Message);
}
我确定文件名是正确的
当我 运行 程序时,它显示一个空文本框。
结果
如其他地方所述,实际问题源于显示对话框在执行对话框之前被阻塞
这里有一些东西
为什么不创建专用表单,即 MyDeciatedShowAllForm
,而不是动态创建它
如果你使用的是实现IDisposable
的东西,最好使用using
语句
例子
using(var r = new StreamReader("empData.txt"))
{
string strShowAllData = r.ReadToEnd();
txtShowAll.Text = strShowAllData;
}
- 你为什么不使用
File.ReadAllText
来代替,为自己保存一些可打印的字符
例子
string strShowAllData = File.ReadAllText(path);
- 您可能需要将文本框设置为 MultiLine
例子
// Set the Multiline property to true.
textBox1.Multiline = true;
// Add vertical scroll bars to the TextBox control.
textBox1.ScrollBars = ScrollBars.Vertical;
// Allow the RETURN key to be entered in the TextBox control.
textBox1.AcceptsReturn = true;
// Allow the TAB key to be entered in the TextBox control.
textBox1.AcceptsTab = true;
// Set WordWrap to true to allow text to wrap to the next line.
textBox1.WordWrap = true;
// Show all data
textBox1.Text = strShowAllData;
- 为什么不先使用 File.Exists
检查文件是否存在
例子
if(!File.Exists("someFile.txt"))
{
MessageBox.Show(!"oh nos!!!!! the file doesn't exist");
return;
}
- 最后,这是你需要记住的,你需要学习如何使用调试器和断点,Navigating through Code with the Debugger。我的意思是,如果您刚刚在
txtShowAll.Text = strShowAllData;
上设置断点,您应该知道文本文件中是否有文本,您或我们的想法应该毫无疑问。
我刚注意到您在对话框模式下显示表单后向文本框添加文本。为什么不移动 frmShow.ShowDialog();到 try 块的末尾,就像我在下面的代码中所做的那样,并确保 empData.txt 存在于它的路径中。
try
{
Form frmShow = new Form();
TextBox txtShowAll = new TextBox();
frmShow.StartPosition = FormStartPosition.CenterScreen;
frmShow.Font = this.Font;
frmShow.Size = this.Size;
frmShow.Icon = this.Icon;
frmShow.Text = "All data";
txtShowAll.Dock = DockStyle.Fill;
txtShowAll.Multiline = true;
frmShow.Controls.Add(txtShowAll);
StreamReader r = new StreamReader("empData.txt");
string strShowAllData = r.ReadToEnd();
txtShowAll.Text = strShowAllData;
r.Close();
frmShow.ShowDialog();
}
catch (Exception x)
{
MessageBox.Show(x.Message);
}
try
{
Form frmShow = new Form();
TextBox txtShowAll = new TextBox();
frmShow.StartPosition = FormStartPosition.CenterScreen;
frmShow.Font = this.Font;
frmShow.Size = this.Size;
frmShow.Icon = this.Icon;
frmShow.Text = "All data";
txtShowAll.Dock = DockStyle.Fill;
txtShowAll.Multiline = true;
frmShow.Controls.Add(txtShowAll);
frmShow.ShowDialog();
StreamReader r = new StreamReader("empData.txt");
string strShowAllData = r.ReadToEnd();
txtShowAll.Text = strShowAllData;
r.Close();
}
catch (Exception x)
{
MessageBox.Show(x.Message);
}
我确定文件名是正确的 当我 运行 程序时,它显示一个空文本框。
结果
如其他地方所述,实际问题源于显示对话框在执行对话框之前被阻塞
这里有一些东西
为什么不创建专用表单,即
MyDeciatedShowAllForm
,而不是动态创建它如果你使用的是实现
IDisposable
的东西,最好使用using
语句
例子
using(var r = new StreamReader("empData.txt"))
{
string strShowAllData = r.ReadToEnd();
txtShowAll.Text = strShowAllData;
}
- 你为什么不使用
File.ReadAllText
来代替,为自己保存一些可打印的字符
例子
string strShowAllData = File.ReadAllText(path);
- 您可能需要将文本框设置为 MultiLine
例子
// Set the Multiline property to true.
textBox1.Multiline = true;
// Add vertical scroll bars to the TextBox control.
textBox1.ScrollBars = ScrollBars.Vertical;
// Allow the RETURN key to be entered in the TextBox control.
textBox1.AcceptsReturn = true;
// Allow the TAB key to be entered in the TextBox control.
textBox1.AcceptsTab = true;
// Set WordWrap to true to allow text to wrap to the next line.
textBox1.WordWrap = true;
// Show all data
textBox1.Text = strShowAllData;
- 为什么不先使用 File.Exists 检查文件是否存在
例子
if(!File.Exists("someFile.txt"))
{
MessageBox.Show(!"oh nos!!!!! the file doesn't exist");
return;
}
- 最后,这是你需要记住的,你需要学习如何使用调试器和断点,Navigating through Code with the Debugger。我的意思是,如果您刚刚在
txtShowAll.Text = strShowAllData;
上设置断点,您应该知道文本文件中是否有文本,您或我们的想法应该毫无疑问。
我刚注意到您在对话框模式下显示表单后向文本框添加文本。为什么不移动 frmShow.ShowDialog();到 try 块的末尾,就像我在下面的代码中所做的那样,并确保 empData.txt 存在于它的路径中。
try
{
Form frmShow = new Form();
TextBox txtShowAll = new TextBox();
frmShow.StartPosition = FormStartPosition.CenterScreen;
frmShow.Font = this.Font;
frmShow.Size = this.Size;
frmShow.Icon = this.Icon;
frmShow.Text = "All data";
txtShowAll.Dock = DockStyle.Fill;
txtShowAll.Multiline = true;
frmShow.Controls.Add(txtShowAll);
StreamReader r = new StreamReader("empData.txt");
string strShowAllData = r.ReadToEnd();
txtShowAll.Text = strShowAllData;
r.Close();
frmShow.ShowDialog();
}
catch (Exception x)
{
MessageBox.Show(x.Message);
}