PsySpooky

Hey all

can you help me on how to parse a text file content into a readonly textbox

I use the openFileDialog method to choose the txt file i whant to show in the textbox


ty



Re: Visual C++ Express Edition parse a file content into a textbox

scor7910

char inBuffer[1024] = {0,}; //Buffer For File content.

CEditBox editBox; //I guess textbox is CEdit

HANDLE hFile; //File Handle returned by openFileDialog

BOOL bResult = ReadFile(hFile, &inBuffer, nBytesToRead, &nBytesRead, NULL) ;
// Check for end of file.
if (bResult && nBytesRead == 0, )
{
editBox.SetWindowText(inBuffer);

}






Re: Visual C++ Express Edition parse a file content into a textbox

PsySpooky

i put it in my textbox:

private: System::Void textBox1_TextChanged(System:Surprisebject^ sender, System::EventArgs^ e) {
char inBuffer[1024] = {0,}; //Buffer For File content.

CEditBox editBox; //I guess textbox is CEdit

HANDLE hFile; //File Handle returned by openFileDialog

BOOL bResult = ReadFile(hFile, &inBuffer, nBytesToRead, &nBytesRead, NULL) ;
// Check for end of file.
if (bResult && nBytesRead == 0, )
{
editBox.SetWindowText(inBuffer);

but i get various errors....
why






Re: Visual C++ Express Edition parse a file content into a textbox

scor7910

Oh my...

I thought your developement environment is C++ language, but C# I guess..

What you want code sample included in Book "Professional C#"






Re: Visual C++ Express Edition parse a file content into a textbox

Bite Qiu - MSFT

It is a c++ question but using Windows form, not MFC.

Maybe this is what you are asking for:

Code Snippet

using namespace System;

using namespace System::IO;

using namespace System::ComponentModel;

using namespace System::Collections;

using namespace System::Windows::Forms;

using namespace System::Data;

using namespace System::Drawing;

using namespace EnvDTE;

public ref class Form1 : public System::Windows::Forms::Form

{

public:

Form1(void)

{

InitializeComponent();

}

protected:

~Form1()

{

if (components)

{

delete components;

}

}

private: System::Windows::Forms::Button^ button1;

private: System::Windows::Forms::TextBox^ textBox1;

//private: System::

protected:

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->textBox1 = (gcnew System::Windows::Forms::TextBox());

this->SuspendLayout();

//

// button1

//

this->button1->Location = System::Drawing::Point(104, 7);

this->button1->Name = L"button1";

this->button1->Size = System::Drawing::Size(75, 23);

this->button1->TabIndex = 0;

this->button1->Text = L"button1";

this->button1->UseVisualStyleBackColor = true;

this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);

//

// textBox1

//

this->textBox1->Location = System::Drawing::Point(36, 41);

this->textBox1->Multiline = true;

this->textBox1->Name = L"textBox1";

this->textBox1->ReadOnly = true;

this->textBox1->ScrollBars = System::Windows::Forms::ScrollBars::Both;

this->textBox1->Size = System::Drawing::Size(228, 206);

this->textBox1->TabIndex = 1;

//

// Form1

//

this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);

this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;

this->ClientSize = System::Drawing::Size(292, 273);

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) {

Stream^ s;

OpenFileDialog^ dlg = gcnew OpenFileDialog();

if( (dlg->ShowDialog()) == System::Windows::Forms::DialogResult::OK ){

StreamReader^ sr = gcnew StreamReader( dlg->FileName );

if( sr!=nullptr ){

String^ str;

while( (str=sr->ReadLine()) != nullptr ){

textBox1->AppendText(str);

}

}

}

}

};

Note: there are windows form forums that might better helped on such issues, I'd recommend you have a try there when you get further windows form questions.






Re: Visual C++ Express Edition parse a file content into a textbox

Bamirasta

Hey,

I read your code and understood it pretty much, but i'm stuck at the point where i want to use the file. So far i managed to get the filename + path in the Textbox with this code:

if( (openFileDialog1->ShowDialog()) == System::Windows::Forms:Big SmileialogResult:SurpriseK ){

String^ sr = gcnew String( openFileDialog1->FileName );

textBox1->AppendText(sr);

But now it only shows the file path in the Textbox. How can i use this file . How do i get this file in memory. Eventually i want to upload the file to a server. I'm using Visual C++ 2005 Express Edition.

Thanks