Hi there,

I am currently attempting to enhance an old program to use Windows Forms in C++. From what Ive read it would be preferable to use Visual Basic or C#; however in its current state, combined with my lack of experience in C#/VB this would be a real struggle to attempt at this stage.

Hopefully by outlining an example it will be possible to convey my current issue.

Essentially I have opened a string stream and passed its contents into a std::string. Originally this was to be output in the console screen. By implementing Windows forms in my programme I now wish to create a button that when clicked opens the saveFileDialog allowing me to save this string as a text file.

This is where I curently am at:

#include <iostream>
#include <sstream>

class test
	std::string write();

#include "stdafx.h"
#include "string.h"

std::string test::write()
	std::ostringstream output;
	output << "HELLO" << std::endl;
	output << "HELLO AGAIN" << std::endl;
	std::string s2 = output.str();
	return s2;

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e){
			 test a;
			 SaveFileDialog ^ saveFileDialog1 = gcnew SaveFileDialog();
			 saveFileDialog->Filter = "Text file (*.txt)|*.txt";
			 saveFileDialog->Title = "Save a text file";



I hope it can be seen that I have a seperate string.ccp and corresponding header file to define the string contents which is a requirement of my programme.

Unless what I have done is flawed in some way I think all I need is a point in the direction as to what code to place in Form1.h as there is very little information around the internet for this sort of thing.

One piece that I did find used fsteam instead of sstrem though im not sure if this would make a significant difference?

Thank you very much in advance for looking at this, your time is much appreciated.


SaveFileDialog() doesn't save anything -- it's just a dialog box that lets you nagivate to the folder where you want the file saved and give the file a name. After SaveFileDialog() returns to your program your program must get the complete path/filename from the SaveFileDialog class, open the file for writing, and write the data.

Here is an example written for C#, but C++ is probably similar if not identical.

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.