Click here to Skip to main content
16,010,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have 2 window forms, form1 and form 2. Form1 had a button(btnRead) that will read a file. How can I load the file that reading from button in form1 and load to textbox in form2.

Please help

C#
private void btnRead_Click(object sender, EventArgs e)
{
StreamReader streamRdr = new StreamReader(fileName);
textbox1.text = streamRdr.ReadToEnd();
streamRdr.Close();
}

The above code will read the file and display content in textbox in form1. How I can load to textbox in form2.

thanks,
Posted
Updated 10-Apr-12 8:31am
v2
Comments
Nelek 10-Apr-12 14:31pm    
Edit: Fixed Tags

You can do the reading a lot simpler:
C#
string s = File.ReadAllText(fileName);
Or
string[] lines = File.ReadAllLines(fileName);
The later is useful when you load a multiline TextBox:
C#
myTextBox.Lines = lines;


How to move the text to the second form (call it frmTwo):
In frmTwo, create a property:
C#
public string Text
    {
    get { return myTextBox.Text; }
    set { myTextBox.Text = value; }
    }
Or
C#
public string[] Lines
    {
    get { return myTextBox.Lines; }
    set { myTextBox.Lines = value; }
   }

In your first form, create an instance of the second form, set the data, and show it:
C#
frmTwo f = new frmTwo();
f.Text = File.ReadAllText(fileName);
f.ShowDialog();
Or
C#
frmTwo f = new frmTwo();
f.Lines = File.ReadAllLines(fileName);
f.ShowDialog();
You can use f.Show instead of f.ShowDialog
 
Share this answer
 
Simple Way:

1)go to:
Project->Project Properties->Settings
and add a new string(filename for example)

2)When Opening File you need code:

C#
Properties.Settings.Default.filename = filename;


3)On your second form add a timer and a code(in timer):

C#
if(Properties.Settings.Default.filename != "")
{
textBox1.Text = File.ReadAllLines(Properties.Settings.Default.fileName);
Properties.Settings.Default.filename = "";
}


Dont forget to enable added timer
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900