0

Hi,

I'm having problem with "WriteLine". What I'm tryin to do is write into the selected txt file(from openFileDialog) when user type something in the textBox.
******************************************************
My output is something wrong. For example: when I type "testing" in the textBox, what I see in my text file is as below:
t
te
tes
test
testi
testin
testing
******************************************************

kindly give me some advice if I'm wrong with the code.

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
			 {
				openFileDialog1->ShowDialog();
				
				StreamReader^ sr = gcnew StreamReader(openFileDialog1->FileName); 
				String^ textfile = sr->ReadToEnd();
				sr->Close();
				textBox1->Text= textfile;
}

	private: System::Void textBox2_TextChanged(System::Object^  sender, System::EventArgs^  e) 
			 {
				
				StreamWriter^ writeToFile = File::AppendText(openFileDialog1->FileName);
				//String^ fileName=openFileDialog1->FileName;   //save file directory to txt file
				
				writeToFile->WriteLine(textBox2->Text);
				writeToFile->Close();
			 }

Edited by Nick Evan: n/a

3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by jonsca
0

It's doing exactly what you asked it to. You're having the program look for when the text has changed in the textbox (so if you type in any characters that event is firing with each one) and outputting the results to the file. You are getting the text of entire textbox written out each time and the entries are being appended after one another.
You need to either wait for the user to press enter or put a "send" button on the window.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.