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);
}
我确定文件名是正确的 当我运行程序时,它显示一个空文本框。
答案 0 :(得分:2)
如其他地方所述,实际问题源于在执行对话之前阻止显示对话框
这里有几件事
为什么不创建专用表格,MyDeciatedShowAllForm
,而不是动态创建
如果您使用的是实现IDisposable
的内容,最好使用using
语句
示例强>
using(var r = new StreamReader("empData.txt"))
{
string strShowAllData = r.ReadToEnd();
txtShowAll.Text = strShowAllData;
}
File.ReadAllText
代替,为自己保存一些可打印的字符示例强>
string strShowAllData = File.ReadAllText(path);
示例强>
// 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;
<强>〔实施例强>
if(!File.Exists("someFile.txt"))
{
MessageBox.Show(!"oh nos!!!!! the file doesn't exist");
return;
}
txtShowAll.Text = strShowAllData;
上设置了一个断点,那么你的思想或我们应该毫无疑问。答案 1 :(得分:2)
我刚注意到在对话框模式下显示表单后,您正在向文本框添加文本。你为什么不移动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);
}