Converting data from TextBox (String^) to string
-
Hello people, I am trying to make an application with in- and output. Input by textboxes and output by writing it to a file. I succeeded to get text from a TextBox^ and put it in a RichTextBox^. No problem. I succeeded to use fstream to 'print' data (string) to a file. Now I want to combine these by using a vector. The problem I am facing is that if I do: fout << textBox->Text; that I get the following error:
1< d:\stageappvc\terminalapp\terminalapp\Form1.h(185) : error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'System::String ^' (or there is no acceptable conversion)
1< C:\Program Files\Microsoft Visual Studio 9.0\VC\include\ostream(653): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)If I want to write the
textBox->Text
to my vector. I get the following error:
1>d:\stageappvc\terminalapp\terminalapp\Form1.h(194) : error C2664: 'std::vector<_Ty>::push_back' : cannot convert parameter 1 from 'System::String ^' to 'const std::string &'
I tried to find out what this String^ actually is - but I could not find an accurate answer. I also tried:
textBox->Text.toString()
which failed too. Is anyone able to help me with this? I tried to find examples of how to put TextBox data in a string, but I couldn't find anything useful... Kind regards, Mikki Weesenaar
-
Hello people, I am trying to make an application with in- and output. Input by textboxes and output by writing it to a file. I succeeded to get text from a TextBox^ and put it in a RichTextBox^. No problem. I succeeded to use fstream to 'print' data (string) to a file. Now I want to combine these by using a vector. The problem I am facing is that if I do: fout << textBox->Text; that I get the following error:
1< d:\stageappvc\terminalapp\terminalapp\Form1.h(185) : error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'System::String ^' (or there is no acceptable conversion)
1< C:\Program Files\Microsoft Visual Studio 9.0\VC\include\ostream(653): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)If I want to write the
textBox->Text
to my vector. I get the following error:
1>d:\stageappvc\terminalapp\terminalapp\Form1.h(194) : error C2664: 'std::vector<_Ty>::push_back' : cannot convert parameter 1 from 'System::String ^' to 'const std::string &'
I tried to find out what this String^ actually is - but I could not find an accurate answer. I also tried:
textBox->Text.toString()
which failed too. Is anyone able to help me with this? I tried to find examples of how to put TextBox data in a string, but I couldn't find anything useful... Kind regards, Mikki Weesenaar
-
I have: std::vector textCode; private: System::Windows::Forms::RichTextBox^ textBox; private: System::Windows::Forms::TextBox^ invoerVeld; textBox->Text += invoerVeld->Text + "\r\n"; < works fine textCode.push_back(textBox->Text); < no-go textCode.push_back(invoerVeld->Text); 1>d:\stageappvc\terminalapp\terminalapp\Form1.h(194) : error C2664: 'std::vector<_Ty>::push_back' : cannot convert parameter 1 from 'System::String ^' to 'const std::string &' 1>d:\stageappvc\terminalapp\terminalapp\Form1.h(195) : error C2664: 'std::vector<_Ty>::push_back' : cannot convert parameter 1 from 'System::String ^' to 'const std::string &' So I think I cannot just use string in stead of String. Its a native thing of the TextBox and RichTextBox. I guess... Or if Im wrong, correct me :)
-
Hello people, I am trying to make an application with in- and output. Input by textboxes and output by writing it to a file. I succeeded to get text from a TextBox^ and put it in a RichTextBox^. No problem. I succeeded to use fstream to 'print' data (string) to a file. Now I want to combine these by using a vector. The problem I am facing is that if I do: fout << textBox->Text; that I get the following error:
1< d:\stageappvc\terminalapp\terminalapp\Form1.h(185) : error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'System::String ^' (or there is no acceptable conversion)
1< C:\Program Files\Microsoft Visual Studio 9.0\VC\include\ostream(653): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)If I want to write the
textBox->Text
to my vector. I get the following error:
1>d:\stageappvc\terminalapp\terminalapp\Form1.h(194) : error C2664: 'std::vector<_Ty>::push_back' : cannot convert parameter 1 from 'System::String ^' to 'const std::string &'
I tried to find out what this String^ actually is - but I could not find an accurate answer. I also tried:
textBox->Text.toString()
which failed too. Is anyone able to help me with this? I tried to find examples of how to put TextBox data in a string, but I couldn't find anything useful... Kind regards, Mikki Weesenaar
I got this from MSDN -
String^ c = gcnew String("Change Me");
std::string str1 = (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
std::wstring str2 = (const wchar_t*)(Marshal::StringToHGlobalUni(s)).ToPointer();«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) -
I got this from MSDN -
String^ c = gcnew String("Change Me");
std::string str1 = (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
std::wstring str2 = (const wchar_t*)(Marshal::StringToHGlobalUni(s)).ToPointer();«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) -
I dont see the relation between the first line and the other two. And I need it otherwise. I have a String^ (coming from textBox->Text) and I want to store this in a string (which I have in a vector).
The first line creates a String^. I believe you already have that in textBox->Text. The second line converts that to a string. The third line converts that to a wstring. What else do you not see?
«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) -
The first line creates a String^. I believe you already have that in textBox->Text. The second line converts that to a string. The third line converts that to a wstring. What else do you not see?
«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++)I guess I need the second then. Currently I have
std::string str1 = (const char*)(Marshel::StringToHGlobalAnsi(textBox->Text)).ToPointer();
But it gives me the following errors:
1>d:\stageappvc\terminalapp\terminalapp\Form1.h(200) : error C2653: 'Marshel' : is not a class or namespace name
1>d:\stageappvc\terminalapp\terminalapp\Form1.h(200) : error C2228: left of '.ToPointer' must have class/struct/union
1> type is ''unknown-type''
1>d:\stageappvc\terminalapp\terminalapp\Form1.h(200) : error C3861: 'StringToHGlobalAnsi': identifier not foundSo I tried to #include , but that didn't work either. How can I use this function? And by the way - thanks for your fast replies :)
-
I guess I need the second then. Currently I have
std::string str1 = (const char*)(Marshel::StringToHGlobalAnsi(textBox->Text)).ToPointer();
But it gives me the following errors:
1>d:\stageappvc\terminalapp\terminalapp\Form1.h(200) : error C2653: 'Marshel' : is not a class or namespace name
1>d:\stageappvc\terminalapp\terminalapp\Form1.h(200) : error C2228: left of '.ToPointer' must have class/struct/union
1> type is ''unknown-type''
1>d:\stageappvc\terminalapp\terminalapp\Form1.h(200) : error C3861: 'StringToHGlobalAnsi': identifier not foundSo I tried to #include , but that didn't work either. How can I use this function? And by the way - thanks for your fast replies :)
Typo It should be
Marshal
. You got it wrong there. Look at my posting.«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) -
Typo It should be
Marshal
. You got it wrong there. Look at my posting.«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++)1>d:\stageappvc\terminalapp\terminalapp\Form1.h(7) : fatal error C1083: Cannot open include file: 'Marshal': No such file or directory :p Yeh - I corrected it but copied the wrong stuff I saw... But with A its still not working :( I am using Visual C++ Express Edition. Downloaded the package yesterday :p
-
1>d:\stageappvc\terminalapp\terminalapp\Form1.h(7) : fatal error C1083: Cannot open include file: 'Marshal': No such file or directory :p Yeh - I corrected it but copied the wrong stuff I saw... But with A its still not working :( I am using Visual C++ Express Edition. Downloaded the package yesterday :p
I suspect some mix-up in your code. You do not have to include any file called
Marshal
. And you can use this class only in managed applications.«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) -
I suspect some mix-up in your code. You do not have to include any file called
Marshal
. And you can use this class only in managed applications.«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++)#pragma once
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
//#include <Windows.h>namespace TerminalApp {
using namespace std; 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> //Variabele waar alle code in komt te staan// std::vector<string> textCode; public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); // // } protected: /// <summary> /// Clean up any resources being used. /// </summary> ~Form1() { if (components) { delete components; } } private: System::Windows::Forms::Button^ exitButton; private: System::Windows::Forms::Button^ addText; private: System::Windows::Forms::RichTextBox^ textBox; private: System::Windows::Forms::Button^ saveButton; private: System::Windows::Forms::TextBox^ invoerVeld; private: System::Windows::Forms::Button^ voegTextButton; 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->exitButton = (gcnew System::Windows::Forms::Button());
this->addText = (gcnew System::Windows::Forms::Button());
this->textBox = (gcnew System::Windows::Forms::RichTextBox());
this->saveButton = (gcnew System::Windows::Forms::Button());
this->invoerVeld = (gcnew System::Windows::Forms::TextBox());
this->voegTextButton = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// exitButton
//
this->exitButton->Location = System -
#pragma once
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
//#include <Windows.h>namespace TerminalApp {
using namespace std; 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> //Variabele waar alle code in komt te staan// std::vector<string> textCode; public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); // // } protected: /// <summary> /// Clean up any resources being used. /// </summary> ~Form1() { if (components) { delete components; } } private: System::Windows::Forms::Button^ exitButton; private: System::Windows::Forms::Button^ addText; private: System::Windows::Forms::RichTextBox^ textBox; private: System::Windows::Forms::Button^ saveButton; private: System::Windows::Forms::TextBox^ invoerVeld; private: System::Windows::Forms::Button^ voegTextButton; 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->exitButton = (gcnew System::Windows::Forms::Button());
this->addText = (gcnew System::Windows::Forms::Button());
this->textBox = (gcnew System::Windows::Forms::RichTextBox());
this->saveButton = (gcnew System::Windows::Forms::Button());
this->invoerVeld = (gcnew System::Windows::Forms::TextBox());
this->voegTextButton = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// exitButton
//
this->exitButton->Location = SystemTry adding this line -
using namespace System::Runtime::InteropServices;
«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) -
Try adding this line -
using namespace System::Runtime::InteropServices;
«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++)1>d:\stageappvc\terminalapp\terminalapp\Form1.h(200) : error C2653: 'Marshel' : is not a class or namespace name
1>d:\stageappvc\terminalapp\terminalapp\Form1.h(200) : error C2228: left of '.ToPointer' must have class/struct/union
1> type is ''unknown-type''
1>d:\stageappvc\terminalapp\terminalapp\Form1.h(200) : error C3861: 'StringToHGlobalAnsi': identifier not foundIs there any other known way to get text out such TextBox into a string? One made by Microsoft themselves? :p
-
1>d:\stageappvc\terminalapp\terminalapp\Form1.h(200) : error C2653: 'Marshel' : is not a class or namespace name
1>d:\stageappvc\terminalapp\terminalapp\Form1.h(200) : error C2228: left of '.ToPointer' must have class/struct/union
1> type is ''unknown-type''
1>d:\stageappvc\terminalapp\terminalapp\Form1.h(200) : error C3861: 'StringToHGlobalAnsi': identifier not foundIs there any other known way to get text out such TextBox into a string? One made by Microsoft themselves? :p
You have typed in
Marsh**e**l
. The correct spelling isMarsh**a**l
. Do you see the difference?«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) -
You have typed in
Marsh**e**l
. The correct spelling isMarsh**a**l
. Do you see the difference?«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) -
How strange - because I copied it directly from your post - since first of all I hate typing and secondly its always better to copy paste :p But it is working now - let me test the output etc. Thanks in advance <3
:thumbsup: But I really think you should not be driving now. :)
«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) -
:thumbsup: But I really think you should not be driving now. :)
«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++)No Im not driving lol. It works! *cheering the hell outta it* Adding text to the TextBox and with a button its sending the data to my vector string which can be read and send to the output :-\ Thank you very very much! I really needed this line! <33 :rose: