简体   繁体   中英

Trying to capture text from an edit box on button click and then display to another edit box

I am very new to Win32 gui coding and have been having quite a lot of difficulty with capturing user inputs.

Basically what I am trying to do is have the user enter some text into a textbox (call it textbox A), click a button and then have that text replicated in another textbox (call it textbox B). I am trying to use the GetWindowsText() function to copy the text in the from textbox A to a buffer and then when the button is clicked send a message to textbox B setting the text equal to the contents of the buffer. Simple right? ... The code from my callback is:

    case WM_COMMAND:
    switch(LOWORD(wParam))
    {
        case IDC_MAIN_BUTTON:
        {
            GetWindowText(hwnd_path, buffer, 5);
            SendMessage(hEdit,WM_SETTEXT,NULL,(LPARAM)buffer);
            MessageBox(NULL, buffer, TEXT("Edit Text"), MB_OK);
        }
        break;
    }
    break;

hwnd_path is the handle for textbox A, hEdit is the handle for textbox B and buffer has been declared as: TCHAR buffer[6] - these variables have been declared globally so they should all be in the scope of the callback function. I've added the messagebox just to double check the value of buffer.

Problem is that nothing appears in either textbox B or the messagebox upon clicking the button - it appears that buffer remains empty after the GetWindowText(hwnd_path, buffer, 5) command.

If anyone could offer any advice I'd be very grateful.

Jack

Use SetWindowText instead of calling SendMessage.

I suspect that your hEdit and/or hwnd_path handles are not what you think they are. Make sure these HWND variables are actually referencing your edit and text control handles when you created them.

Don't expect the dialog to instantly update after you call "MessageBox". It may take a few internal message posts to for the SetWindowText API to actually finish. MessageBox does pump messages internally, so this is probably ok.

Here's some sample code that does work. It was written within a DialogBox wndproc. If you are in a dialog box, replace the two calls to GetDlgItem with an assignment of the window handle returned when you created the edit and text field.

    case WM_COMMAND:
    {
        switch LOWORD(wParam)
        {
            case IDC_BUTTON1:
            {
                wchar_t szEditText[300] = {0};

                // If you aren't using DialogBox(), then replace these apis by assigneing hEdit and hText with the HANDLES of your created controls.
                HWND hEdit = GetDlgItem(hwnd, IDC_EDIT1);
                HWND hText = GetDlgItem(hwnd, IDC_TEXT1);

                GetWindowText(hEdit, szEditText, ARRAYSIZE(szEditText));
                SetWindowText(hText, szEditText);

                // If this Window was created with DialogBox, then return TRUE
                // Otherwise, return 0.
                return TRUE;
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM