I am attempting to create a GUI for a console C++ program in Visual C++. Apparently, it is like learning the language all over. Here is my problem:

I am attempting to write the contents of a textbox to a file, but I get the error "error C2228: left of '.Text' must have class/struct/union".

Here is my problem spot, i think:

private: System::Void addtxt_Click(System::Object^  sender, System::EventArgs^  e) {
				 std::ofstream fout;
				 fout.open("callie.txt");
					 fout<<txtbox.Text;
				 fout.close();
			 }

However, the entire source code is here, if you want to look: http://powermite.pastebin.com/f70e98790

It compiles and runs fine if I try to write a set string to the file, such as "Hello", but it won't take the contents of the text box...

I have searched and read nearly everything I could find on the error on Google, but it seems as though it is a different fix every time, none of which really apply to my situation.

Thanks in advance for any help!

What is the name of your textbox? Identifiers like that are case sensitive so if it's "Textbox" or "textBox" (more likely the latter) it's not going to work.

What is the name of your textbox? Identifiers like that are case sensitive so if it's "Textbox" or "textBox" (more likely the latter) it's not going to work.

The name of my text box is txtbox.

The name of my text box is txtbox.

Apologies it was worth a shot.

I think you need to have txtbox->Text.

It's that and you need to convert the String^ to a std::string. It might be a rough road. I know the .NET stuff from the C# side so forgive my difficulties with integrating managed and unmanaged stuff.

EDIT: I would look up how to use System::IO::StreamWriter as I I think it will make life a lot easier. It avoids all of the moving pointers from managed to unmanaged and the syntax is pretty straightforward.

Edited 6 Years Ago by jonsca: n/a

Apologies it was worth a shot.

I think you need to have txtbox->Text.

It's that and you need to convert the String^ to a std::string. It might be a rough road. I know the .NET stuff from the C# side so forgive my difficulties with integrating managed and unmanaged stuff.

EDIT: I would look up how to use System::IO::StreamWriter as I I think it will make life a lot easier. It avoids all of the moving pointers from managed to unmanaged and the syntax is pretty straightforward.

I tried to txtbox->Text. It compiles and opens the file, but doesn't
write the contents of txtbox.

I ran onto StreamWriter, but I would like to use my current source code as much as possible (which uses fstream). If you (or anyone else) are out of ideas, I guess I will look into it...

Thanks

You can look into turning String^ into std::string but it's a hassle to keep moving the data back and forth (juggling the pointers and shifting them between managed and unmanaged). You hit the nail on the head in your first post in that it really is a new language unto itself.

My best suggestion (and others may have better ones) is if you want to keep your existing code base to look into MFC (if you have the standard or higher edition of VS, it's not in the express) or Win32API (which is in the standard and express but takes more code to cover the basic windowing).

You can look into turning String^ into std::string but it's a hassle to keep moving the data back and forth (juggling the pointers and shifting them between managed and unmanaged). You hit the nail on the head in your first post in that it really is a new language unto itself.

My best suggestion (and others may have better ones) is if you want to keep your existing code base to look into MFC (if you have the standard or higher edition of VS, it's not in the express) or Win32API (which is in the standard and express but takes more code to cover the basic windowing).

I have the full VS, but MFC looks complex to me. I think I will go with the StreamWriter for now and see where that gets me.

Thanks for you help!

It's only 3 lines:

System::IO::StreamWriter ^ sw = gcnew System::IO::StreamWriter("Outfile.txt");
sw->Write(textBox1->Text);
sw->Close();

you can use a "using namespace System::IO;" to cut down on the syntax

(speaking of which there's a different context of the "using" keyword in .NET where if you wrap your readers/writers/( anything that need to be "dispose"d of properly) in one it takes care of everything including closing but I'm not sure if it functions the same in C++/CLI as it does in C# -- this snippet should do the job just fine)

Edited 6 Years Ago by jonsca: n/a

It's only 3 lines:

System::IO::StreamWriter ^ sw = gcnew System::IO::StreamWriter("Outfile.txt");
sw->Write(textBox1->Text);
sw->Close();

you can use a "using namespace System::IO;" to cut down on the syntax

Seems easy enough. I'll give it a try.

This article has been dead for over six months. Start a new discussion instead.