so far I could manage to create OpenFileDialog and select the txt file.


after that ,How could I display the contect of selected txt file into TextBox?

*************************************************************
private: virtual System::Void button1_Click(System::Object^  sender, System::EventArgs^  e)  
		 {
 System::Windows::Forms::DialogResult OK = openFileDialog1->ShowDialog();
		 }

private: System::Void openFileDialog1_FileOk(System::Object^  sender, System::ComponentModel::CancelEventArgs^  e)
		 {
????????????????????????????????
                                  }

***********************************************************

Edited 6 Years Ago by WaltP: Fixed CODE tags -- use the PREVIEW button

your going to first need to read the contents of the text file...

either using a ifstream or just a fstream(fstream is both ifstream and ofstream)

lots of examples on the net... just google ifstream/fstream

Use a StreamReader object (in System::IO) to read in the text from the given filename as a string. You may be able to send it right to the textbox from there.

Here's the code I used:

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
{
      OPEN DIALOG
      StreamReader^ sr = gcnew StreamReader(fileName);
      textfile = sr->ReadToEnd();
      CLOSE STREAMREADER
      SEND TO TEXTBOX
			
}
private: System::Void openFileDialog1_FileOk(System::Object^  sender, System::ComponentModel::CancelEventArgs^  e) 
{
     fileName = openFileDialog1->FileName;
 }

Edited 6 Years Ago by jonsca: n/a

textfile and fileName are just string^s declared elsewhere. I was just using the caps as labels, wasn't yelling lol.

your going to first need to read the contents of the text file...

either using a ifstream or just a fstream(fstream is both ifstream and ofstream)

lots of examples on the net... just google ifstream/fstream

Hi Jonsca,

my experience in programming is very little. Am I right to write as below?
I'm still not able to get the output.

namespace doctor {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace	System::IO;
	using namespace	System::Text;

	/// <summary>
	/// Summary for Form1
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Button^  button1;
	protected: 
	private: System::Windows::Forms::OpenFileDialog^  openFileDialog1;
	private: System::Windows::Forms::TextBox^  textBox1;
	private: System::IO::Stream^ str;
	private: System::IO::StreamReader^ myStream;
// *********************************************************************************
/*void ClearTextBox()
{
	textBox1->Text="";
}*/
// *********************************************************************************
	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->openFileDialog1 = (gcnew System::Windows::Forms::OpenFileDialog());
			this->textBox1 = (gcnew System::Windows::Forms::TextBox());
			this->SuspendLayout();
			//his->myStream =(gcnew System::IO::StreamReader(str));
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(40, 109);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(79, 28);
			this->button1->TabIndex = 0;
			this->button1->Text = L"Find";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
			// 
			// openFileDialog1
			// 
			this->openFileDialog1->FileName = L"openFileDialog1";
			this->openFileDialog1->Filter = L"Textfiles(*.txt)|*.txt|All files(*.*)|*.*";
			this->openFileDialog1->InitialDirectory = L"C:\\Users\\User\\Desktop\\TDrive\\toshiba\\doctor";
			this->openFileDialog1->FileOk += gcnew System::ComponentModel::CancelEventHandler(this, &Form1::openFileDialog1_FileOk);
			// 
			// textBox1
			// 
			this->textBox1->Location = System::Drawing::Point(158, 115);
			this->textBox1->Multiline = true;
			this->textBox1->Name = L"textBox1";
			this->textBox1->Size = System::Drawing::Size(283, 132);
			this->textBox1->TabIndex = 1;
			this->textBox1->TextChanged += gcnew System::EventHandler(this, &Form1::textBox1_TextChanged);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(590, 331);
			this->Controls->Add(this->textBox1);
			this->Controls->Add(this->button1);
			this->Name = L"Form1";
			this->Text = L"Form1";
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion

	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
			 {
				openFileDialog1->ShowDialog();
				//String^ str = openFileDialog1->FileName; //get the path of the selected file
				StreamReader^ sr = gcnew StreamReader(openFileDialog1->FileName); 
				String^ textfile = sr->ReadToEnd();
				myStream->Close();

			}
			 
	private: System::Void openFileDialog1_FileOk(System::Object^  sender, System::ComponentModel::CancelEventArgs^  e) 
			 {
				 String^ fileName=openFileDialog1->FileName;
			 }
	private: System::Void textBox1_TextChanged(System::Object^  sender, System::EventArgs^  e) 
			 {
			 }
	};
}

Edited 6 Years Ago by WaltP: Fixed CODE tags -- use the PREVIEW button!

Except you missed the critical part: textBox->Text = textfile; and the streamreader is not called mystream, so just do sr->Closed();

This question has already been answered. Start a new discussion instead.