Hey!

I am a newbie in windows form application in c++, so please guide me!

As a practice I have created a new project in windows form using visual C++.

I have added a button and listbox!

Now say I click on button 1 and it opens a dialog box to select the file from a directory and once selected brings the content of the file into the list box.

How can I do this?

If somebody could direct me to a tutorial or any material which could help me understand and learn windows form would be great.

Also I have googled for "windows form application in c++" and very little has comeup!

Thanks

Recommended Answers

All 17 Replies

>Also I have googled for "windows form application in c++" and very little has comeup!
Really. None of the 35 million hits was even close to relevant? I find that hard to believe, especially when the first page looks both relevant and useful.

>How can I do this?
Handle the click event for your button. Inside the event handler, you'll create an object of the OpenFileDialog class. Once you have the path of the selected file, you can use one (or more) of the many stream I/O classes to load the contents into a string. Then copy that string into some kind of textbox control.

commented: Odd that they have no trouble finding forums though.... +29

I haven't done Windows Forms for one or two years, but with the help of google... I managed to make this small example for you.

System::Void button_Click(System::Object^  sender, System::EventArgs^  e) {
  OpenFileDialog ^openFile = gcnew OpenFileDialog();

  if ( openFile->ShowDialog() == Windows::Forms::DialogResult::OK ) {
    list->Items->Add( openFile->FileName );
  }

}

Search better next time.

commented: Very Helpful +1

I haven't done Windows Forms for one or two years, but with the help of google... I managed to make this small example for you.

System::Void button_Click(System::Object^  sender, System::EventArgs^  e) {
  OpenFileDialog ^openFile = gcnew OpenFileDialog();

  if ( openFile->ShowDialog() == Windows::Forms::DialogResult::OK ) {
    list->Items->Add( openFile->FileName );
  }

}

Search better next time.

Thanks for your help!

Search better next time.

Yeah! I prob need to improve my googling skills.:)

{
    list->Items->Add( openFile->FileName );
  }

Could you please explain this part of the code as it is giving me errors:

Error:

Error 1 error C2227: left of '->Add' must point to class/struct/union/generic type
Error 2 error C2227: left of '->Items' must point to class/struct/union/generic type

Thanks

>Also I have googled for "windows form application in c++" and very little has comeup!
Really. None of the 35 million hits was even close to relevant? I find that hard to believe, especially when the first page looks both relevant and useful.

>How can I do this?
Handle the click event for your button. Inside the event handler, you'll create an object of the OpenFileDialog class. Once you have the path of the selected file, you can use one (or more) of the many stream I/O classes to load the contents into a string. Then copy that string into some kind of textbox control.

Thanks Narue for your help!

I had been to this but it kept complaining about the STREAM...... but again my knowledge is limited at the moment that I didn't know how to fix it!;)

> Could you please explain this part of the code as it is giving me errors:

Don't just copy and paste what I wrote, use your head :) Change the name list to what ever you called your listbox. But I just re-read your question, and realized that's not what you want. But adding the entire contents of a file to one line in a list box doesn't seem logical, but if that's what you need, learn how to open a file and read the data.

Hope this helps.

commented: Well said :) +2

> Could you please explain this part of the code as it is giving me errors:

Don't just copy and paste what I wrote, use your head :) Change the name list to what ever you called your listbox. But I just re-read your question, and realized that's not what you want. But adding the entire contents of a file to one line in a list box doesn't seem logical, but if that's what you need, learn how to open a file and read the data.

Hope this helps.

Sorry William but my heads away at the moment!;)

Anyway! changed the name of the listbox!

But errors:

error C2065: 'listBox1_SelectedIndexChanged' : undeclared identifier
error C3350: 'System::EventHandler' : a delegate constructor expects 2 argument(s)

As far as I know there are two arguments!

Please educate!

Thanks

The error is at this line!

this->listBox1->SelectedIndexChanged += gcnew System::EventHandler(this, &Form1::listBox1_SelectedIndexChanged);

Attach or post the whole code please? It will be faster than figuring this out with small snippets of your code.

Sorry!

Right here is all the code from Form1.h

#pragma once


namespace form1 {

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

	/// <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^  Open;
	private: System::Windows::Forms::ListBox^  listBox1;


	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->Open = (gcnew System::Windows::Forms::Button());
			this->listBox1 = (gcnew System::Windows::Forms::ListBox());
			this->SuspendLayout();
			// 
			// Open
			// 
			this->Open->Location = System::Drawing::Point(32, 105);
			this->Open->Name = L"Open";
			this->Open->Size = System::Drawing::Size(75, 23);
			this->Open->TabIndex = 0;
			this->Open->Text = L"Open";
			this->Open->UseVisualStyleBackColor = true;
			this->Open->Click += gcnew System::EventHandler(this, &Form1::Open_Click);
			// 
			// listBox1
			// 
			this->listBox1->FormattingEnabled = true;
			this->listBox1->Location = System::Drawing::Point(152, 33);
			this->listBox1->Name = L"listBox1";
			this->listBox1->Size = System::Drawing::Size(120, 95);
			this->listBox1->TabIndex = 1;
			this->listBox1->SelectedIndexChanged += gcnew System::EventHandler(this, &Form1::listBox1_SelectedIndexChanged);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(284, 264);
			this->Controls->Add(this->listBox1);
			this->Controls->Add(this->Open);
			this->Name = L"Form1";
			this->Text = L"Form1";
			this->ResumeLayout(false);

		}
#pragma endregion
	private: System::Void Open_Click(System::Object^  sender, System::EventArgs^  e) {
				 OpenFileDialog ^openFile = gcnew OpenFileDialog();
				  if ( openFile->ShowDialog() == Windows::Forms::DialogResult::OK ) {
					listBox1->Items->Add( openFile->FileName );
				  }
			 }
	};
}

I have no idea what's causing that error, the button event compiled and ran fine for me.
The only thing I can suggest is, try removing line 84 and see if it works.

Right......read a few tutorials......

Am now able to open a dialog box!!!!!!!:)

Here is how I did it.....

1. First added a openFileDialog1 from the toolbox!

2. Then on the click event of the button added the following code!

private: System::Void Open_Click(System::Object^  sender, System::EventArgs^  e) {
				 openFileDialog1->ShowDialog();
			 }
commented: that's the spirit ; ) +8

I have no idea what's causing that error, the button event compiled and ran fine for me.
The only thing I can suggest is, try removing line 84 and see if it works.

No probs William......I'll read the few of the tutorials again until I know what I am doing!

Only thing I have to do now is bring the contents of the file into the textbox!

Thanks for all the guidance!:-)

Mate tell me If I am right!

Once the user clicks on the file......

if ( openFileDialog1->ShowDialog() == Windows::Forms::DialogResult::OK ) {
				 richTextBox1->Text( openFileDialog1->FileName );
				 }

basically want the contents of the file into the richTextBox1

openFileDialog1->FileName

This isn't the file contents, it's the file path. Try something like this.

IO::File::ReadAllText( openFile->FileName )

Hope this helps.

openFileDialog1->FileName

This isn't the file contents, it's the file path. Try something like this.

IO::File::ReadAllText( openFile->FileName )

Hope this helps.

Thanks again mate,

Just wanted to ask how will it know where to output the text?

>Just wanted to ask how will it know where to output the text?
If you want to output it to your richTextBox1, simply do this:

richTextBox1->Text = IO::File::ReadAllText( openFile->FileName );

Hope this helps.

>Just wanted to ask how will it know where to output the text?
If you want to output it to your richTextBox1, simply do this:

richTextBox1->Text = IO::File::ReadAllText( openFile->FileName );

Hope this helps.

Thanks William you have been very helpful!:)

It has taught me alot!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.