Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Converting data from TextBox (String^) to string

Converting data from TextBox (String^) to string

Scheduled Pinned Locked Moved C / C++ / MFC
helpcsharpvisual-studiographicstutorial
17 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T transoft

    Don't use "System::String" instead use "std::string"

    I Offline
    I Offline
    iMikki
    wrote on last edited by
    #3

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

    1 Reply Last reply
    0
    • I iMikki

      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

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #4

      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 1 Reply Last reply
      0
      • _ _Superman_

        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 Offline
        I Offline
        iMikki
        wrote on last edited by
        #5

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

        _ 1 Reply Last reply
        0
        • I iMikki

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

          _ Offline
          _ Offline
          _Superman_
          wrote on last edited by
          #6

          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 1 Reply Last reply
          0
          • _ _Superman_

            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 Offline
            I Offline
            iMikki
            wrote on last edited by
            #7

            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 found

            So 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 :)

            _ 1 Reply Last reply
            0
            • I iMikki

              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 found

              So 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 :)

              _ Offline
              _ Offline
              _Superman_
              wrote on last edited by
              #8

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

              I 1 Reply Last reply
              0
              • _ _Superman_

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

                I Offline
                I Offline
                iMikki
                wrote on last edited by
                #9

                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 Reply Last reply
                0
                • I iMikki

                  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

                  _ Offline
                  _ Offline
                  _Superman_
                  wrote on last edited by
                  #10

                  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 1 Reply Last reply
                  0
                  • _ _Superman_

                    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 Offline
                    I Offline
                    iMikki
                    wrote on last edited by
                    #11

                    #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

                    _ 1 Reply Last reply
                    0
                    • I iMikki

                      #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

                      _ Offline
                      _ Offline
                      _Superman_
                      wrote on last edited by
                      #12

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

                      I 1 Reply Last reply
                      0
                      • _ _Superman_

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

                        I Offline
                        I Offline
                        iMikki
                        wrote on last edited by
                        #13

                        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 found

                        Is there any other known way to get text out such TextBox into a string? One made by Microsoft themselves? :p

                        _ 1 Reply Last reply
                        0
                        • I iMikki

                          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 found

                          Is there any other known way to get text out such TextBox into a string? One made by Microsoft themselves? :p

                          _ Offline
                          _ Offline
                          _Superman_
                          wrote on last edited by
                          #14

                          You have typed in Marsh**e**l. The correct spelling is Marsh**a**l. Do you see the difference?

                          «_Superman_» I love work. It gives me something to do between weekends.
                          Microsoft MVP (Visual C++)

                          I 1 Reply Last reply
                          0
                          • _ _Superman_

                            You have typed in Marsh**e**l. The correct spelling is Marsh**a**l. Do you see the difference?

                            «_Superman_» I love work. It gives me something to do between weekends.
                            Microsoft MVP (Visual C++)

                            I Offline
                            I Offline
                            iMikki
                            wrote on last edited by
                            #15

                            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

                            _ 1 Reply Last reply
                            0
                            • I iMikki

                              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

                              _ Offline
                              _ Offline
                              _Superman_
                              wrote on last edited by
                              #16

                              :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++)

                              I 1 Reply Last reply
                              0
                              • _ _Superman_

                                :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++)

                                I Offline
                                I Offline
                                iMikki
                                wrote on last edited by
                                #17

                                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:

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups