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.
  • _ _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