mohtady Tamer
mohtady Tamer

Reputation: 33

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);
}

I sure that is the file name is correct when I run the program it shows an empty text box.

The result enter image description here

Upvotes: 3

Views: 227

Answers (2)

TheGeneral
TheGeneral

Reputation: 81573

As noted other places, the actual problem stems from the show dialog blocking before you can execute your dialog

There are a couple of things here

  1. Why don't you create a dedicated Form i.e MyDeciatedShowAllForm, instead of dynamically creating it

  2. If you are using something that implements IDisposable, Its best to use a using statement

Example

using(var r = new StreamReader("empData.txt"))
{
    string strShowAllData = r.ReadToEnd();                
    txtShowAll.Text = strShowAllData;
}
  1. Why don't you use File.ReadAllText instead, save your self some printable characters

Example

string strShowAllData = File.ReadAllText(path);
  1. You probably need to set the TextBox to MultiLine

Example

// 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;
  1. Why don't you check if the file exists first using File.Exists

Exmaple

if(!File.Exists("someFile.txt"))
{
    MessageBox.Show(!"oh nos!!!!! the file doesn't exist");
    return;
}
  1. Lastly, and this is one you need to remember, you need to learn how to use the debugger and breakpoints, Navigating through Code with the Debugger. I mean, you should have known if there is text or not in your text file if you had just put a breakpoint on txtShowAll.Text = strShowAllData; there should be no doubt in your mind or ours.

Upvotes: 2

Mohammad Ajmal Amirzad
Mohammad Ajmal Amirzad

Reputation: 629

I just noticed you are adding text to the textbox after showing the form in dialog mode. Why don't you move the frmShow.ShowDialog(); to the end of the try block just like I have done it in the code below, and make sure the empData.txt exists at its path.

        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);
        }

Upvotes: 2

Related Questions