-1

I am new here and hope to learn a lot from a great community.

I am beginning to learn some GUI C++ pogramming, but only the basics. I have made one win32 GUI app before using mingw and creating my own resource and header file. All I need the program to do at this point is get text from a text box.

With mingw, I would use a callback function like this, using GetDigItem() to get user input from a text box when a certain button is clicked.

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
    case WM_LBUTTONDOWN:
         {
              char szFileName[MAX_PATH];
              HINSTANCE hInstance = GetModuleHandle(NULL);
              GetModuleFileName(hInstance, szFileName, MAX_PATH);
              MessageBox(hwnd, szFileName, "This program is:", MB_OK | MB_ICONINFORMATION);


         }
    break;

    case WM_CLOSE:
        DestroyWindow(hwnd);
    break;

    case WM_DESTROY:
        PostQuitMessage(0);
    break;

    case WM_COMMAND:
         switch(LOWORD(wParam))
             {
                 case ID_FILE_EXIT:
                      DestroyWindow(hwnd);
                      break;
                 case ID_STUFF_GO:
                      break;


                 case REG_BUTTON:
                 char NameInput[MAX_PATH];


                 // Gets inputted info from text boxes

                 // this is what I want to do in vc++
                 GetWindowText(GetDlgItem(hwnd, NAME_BOX), NameInput, MAX_PATH);

                 break;

    case WM_CREATE:
    {    

         CreateWindow (TEXT("EDIT"), TEXT("Name Here"), WS_VISIBLE | WS_CHILD | WS_BORDER, 10, 10, 230, 20, hwnd, (HMENU) NAME_BOX, NULL, NULL);
         CreateWindow (TEXT("EDIT"), TEXT("Number Here"), WS_VISIBLE | WS_CHILD | WS_BORDER, 10, 35, 230, 20, hwnd, (HMENU) SERIAL_BOX, NULL, NULL);
         CreateWindow (TEXT(button), TEXT(button1), WS_VISIBLE | WS_CHILD, 75, 60, 90, 20, hwnd, (HMENU) REG_BUTTON, NULL, NULL);

I've noticed the code is a little different in VC++. I created a button and text box from the Windows Form Designer. I have managed to manipulate inputted info, and have it displayed on the text box, but I need to get inputted info and have it returned as a char, std::string, etc. Here is the code in question that I have, which does not compile as is.

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
     {
         // Storage buffer for serial and name

          std::string test;

                      // Upon button click, this will transfer inputted text to the label next to it
                      label1->Text = textBox1->Text;

          // this does not compile            
                      test = textBox1->Text;

                      // Gets inputted info from text boxes
                      // This does not compile either
                      //GetWindowText(GetDlgItem(NULL, textBox1), NameInput, MAX_PATH);
                      //GetWindowText(GetDlgItem(NULL, textBox2), SerialInput, MAX_PATH);

Does anyone have any ideas on how to get the inputted information in text box to return as a char, char*, (std::)string, or other type for further manipulation? Thanks.

EDIT-

I figured out where in mingw I can use GetDigItem() to return user input, I do that in VC++ by using a System::String^ as indicated in the code below.

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
     {

                        label1->Text = textBox1->Text;

            //char SerialInput[MAX_PATH];
                        //test -> textBox1->Text

                        // The following compiles and works to retrieve input
                        // as a System::String^
                System::String^ userinput;
            textBox1->Text = userinput;

                        // However this yields 2 compiler error
                        // error C2065: 'marshal_as' : undeclared identifier
                        // error C2275: 'std::string' : illegal use of this type as an expression
                        std::string stdString = marshal_as<std::string>(newer);

                        // Forgive any spacing errors, this 4 space indent to repr code
                        // isn't working with my long expressions

The problem now seems to be getting the System::String^ to something I can work with. My attempts to convert it to an std::string aren't working as I showed, I also included in my stdafx.h. A char or anything would be great. Any ideas?

EDIT 2:

I need to convert a SYstem::String^ to whatever else I can (preferably std::string or char). Here is what I have tried, and the compiler errors recieved.

System::String^ userinput;
userinput = textBox1->Text;


// method 1
const __wchar_t __pin * str1 = PtrToStringChars(userinput);

// error recieved
error C4980: '__pin' : use of this keyword requires /clr:oldSyntax command line option

// method 2
char* str2 = (char*)(void*)Marshal::StringToHGlobalAnsi(str);

// error recieved
error C2653: 'Marshal' : is not a class or namespace name
error C3861: 'StringToHGlobalAnsi': identifier not found

// method 3
// #include <atlstr.h>
// CString str3(userinput); 

// error recieved
fatal error C1083: Cannot open include file: 'atlstr.h': No such file or directory
// Where can I get this header file from?

I feel like there's something obvious I"m not doing...

P.S - It's very misleading of Microsoft to call this Visual C++, as this is not C++!

P.S.S - For any c++ coders reading this, VC++ has standards and languages all of it's own, completely different than traditional C++ and even the win api. It's over complication at it's finest - do yourself and your clients a favor and stick to QT for win guis!

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • Hope, you understand that, Qt calls lead to the Windows API, which for many reasons must be a C style API. – Mikhail Aug 19 '15 at 02:22

1 Answers1

0

You should indicate which framework you're using (e.g., MFC or WTL), and give the specific compiler error rather than simply saying it doesn't compile. Without more information, I'm guess this is the problem:

std::string test;
test = textBox1->Text;

The Text field is probably a wide character string. (Windows uses UTF-16 internally, so--on Windows--a std::wstring is a string of 16-bit character values. Most Windows functions handle surrogates to access characters beyond the BMP. On other OSes, a std::wstring is often a string of 32-bit character values.)

You cannot assign a wide character string to a std::string without a conversion. Your choices are to use a wide string (e.g., std::wstring) or to apply a conversion.

For conversions, Windows has a WideCharToMultibyte function you could use. C++ has std::codecvt.

Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175
  • I tried doing something like (short version) `std::wstring = textBox1->Text` but it only wants to return input as a System::String^ – Christian Villalobos Feb 04 '13 at 22:44
  • I'm using WTL, and I've gotten a lot of compiler errors, but I'd really like to know the standard way to retrieve text in VC++ in this code context. Thanks for your help. – Christian Villalobos Feb 04 '13 at 22:52