Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

textfile to textbox how?

Status
Not open for further replies.
Apr 2, 2003
58
GB
Hi I want to read a text file and then put the text into a textbox.

what doesn't seem to be possibleis how to access a variable outside of the static. e.g. textBox1.Text = S; isn't right!!!

Here is the code - any ideas or alternative suggestions wouldbe greatly appreciated.


[STAThread]
static void Main()
{
//Get the text from the AUP.txt file
try
{
Fileread();
}

catch (Exception)
{
MessageBox.Show("The file does not exist!");
}

Application.Run(new Form1());
}

static void Fileread()
{
StreamReader SR;
string S;
SR=File.OpenText("AUP.txt");
S=SR.ReadToEnd();
SR.Close();
textBox1.Text = S;
}
 
You need to prefix your call to the textbox with the name of a variable that points to your form:

Form1.Textbox1.Text = "blah";

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Nope that's not the answer! - I'd already tried that.

you get:
Form1.cs(153): An object reference is required for the nonstatic field, method, or property 'Myapp.Form1.textBox1'

I've also tried using Myapp.Form1.textBox1.Text = "stuff";

too.

There's something about static - I'm new to this and I don't have a grasp of exactly why.

 
Well, Form1 is obviously the class name for your form (remember, forms are classes). You'd have to supply the instance variable:

Form1 MyForm = new Form1();
MyForm.TextBox1.Text = "blah";

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Thanks for that now I'm not getting the errors but the text is not finding it's way into the textbox!

If I use the following the messagebox shows the text but the textBox does not! See my first post for the code starting the app.

static void Fileread()
{
StreamReader SR;
string S;
SR=File.OpenText("AUP.txt");
S=SR.ReadToEnd();
SR.Close();
Form1 thisform = new Form1();
thisform.textBox1.Text = S;
MessageBox.Show(S);
}

 
There are many reasons for what you do not see the text in the textbox.


> Form1 thisform = new Form1();
> thisform.textBox1.Text = S;
This is working but you should call thisform.ShowDialog() function otherwise at the end of the function , "thisform" object is deleted!
Solution:
Implement the Form1_Load handler and process there the existance of the file e.g show message and exit else set the content of the file in the textBox1.Text.
The Form1_Load is called before the form is displayed for the first time.
Code:
this.Load += new System.EventHandler(this.Form1_Load);
private void Form1_Load(object sender, System.EventArgs e)
{
	string s = Fileread();
	if (s == null)
	{
		System.Windows.Forms.MessageBox.Show("The file does not exist!");
		Application.Exit();
	}
	else
		textBox1.Text = Fileread();
}
The main:
Code:
[STAThread]
static void Main() 
{
	Application.Run(new Form1());
}
The Fileread()
Code:
private static string Fileread()
{
	StreamReader SR;
	string S;
	try
	{
		SR=File.OpenText("C:\\AUP.txt");
		S=SR.ReadToEnd();
		SR.Close();
	}
	catch (Exception ex)
	{
		S= null;
	}
	return S;
}
-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top