Can't edit Edit control in DialogBox
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Can't edit Edit control in DialogBox

  1. #1
    Join Date
    Dec 2007
    Location
    France
    Posts
    229

    Can't edit Edit control in DialogBox

    This is a DialogBox without resource.

    The OK button is working and I can get the text from the Edit box but I cannot write to it.


    Code:
    #include <windows.h>
    #include <iostream>
    using namespace std;
    
    #define IDE_OK    111
    #define IDE_EDIT 112
    
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    LRESULT CALLBACK DialogProc(HWND, UINT, WPARAM, LPARAM);
    
    void CreateDialogBox(HWND);
    void RegisterDialogClass(HWND);
    
    HINSTANCE ghInstance;
    
    int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
                        LPSTR lpCmdLine, int nCmdShow )
    {
        MSG  msg ;
        HWND hwnd;
    
        WNDCLASS wc = {0};
    
        wc.lpszClassName = TEXT( "Window" );
        wc.hInstance     = hInstance ;
        wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
        wc.lpfnWndProc   = WndProc;
    
        RegisterClass(&wc);
        hwnd = CreateWindow( wc.lpszClassName, TEXT("Window"),
                             WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                             100, 100, 250, 150, NULL, NULL, hInstance, NULL);
    
        ghInstance = hInstance;
    
        while( GetMessage(&msg, NULL, 0, 0))
        {
            DispatchMessage(&msg);
        }
        return (int) msg.wParam;
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        switch(msg)
        {
        case WM_CREATE:
            RegisterDialogClass(hwnd);
            CreateWindow(TEXT("button"), TEXT("Show dialog"),
                         WS_VISIBLE | WS_CHILD ,
                         20, 50, 95, 25,
                         hwnd, (HMENU) 1, NULL, NULL);
            break;
    
        case WM_COMMAND:
            CreateDialogBox(hwnd);
            break;
    
        case WM_DESTROY:
        {
            PostQuitMessage(0);
            return 0;
        }
        }
        return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    
    LRESULT CALLBACK DialogProc(HWND hwnd, UINT msg,
                                WPARAM wParam,
                                LPARAM lParam)
    {
        char Pass[255];
        switch(msg)
        {
        case WM_CREATE:
            CreateWindow(TEXT("button"), TEXT("Ok"),
                         WS_VISIBLE | WS_CHILD ,
                         50, 50, 80, 25,
                         hwnd, (HMENU) IDE_OK, NULL, NULL);
    
            CreateWindow(TEXT("edit"), "pass",
                         WS_CHILD | WS_VISIBLE | WS_BORDER,
                         50, 20, 80, 25,
                         hwnd, (HMENU) IDE_EDIT, NULL, NULL);
            break;
    
        case WM_COMMAND:
            if (wParam==IDE_OK)
            {
          
                GetDlgItemText(hwnd, IDE_EDIT, Pass, 260);
          
            }
            break;
    
        case WM_CLOSE:
            DestroyWindow(hwnd);
            break;
    
        }
        return (DefWindowProc(hwnd, msg, wParam, lParam));
    }
    
    void RegisterDialogClass(HWND hwnd)
    {
        WNDCLASSEX wc = {0};
        wc.cbSize           = sizeof(WNDCLASSEX);
        wc.lpfnWndProc      = (WNDPROC) DialogProc;
        wc.hInstance        = ghInstance;
        wc.hbrBackground    = GetSysColorBrush(COLOR_3DFACE);
        wc.lpszClassName    = TEXT("DialogClass");
        RegisterClassEx(&wc);
    }
    
    void CreateDialogBox(HWND hwnd)
    {
        CreateWindowEx(WS_EX_DLGMODALFRAME | WS_EX_TOPMOST,
                       TEXT("DialogClass"), TEXT("Dialog Box"),
                       WS_VISIBLE | WS_SYSMENU | WS_CAPTION,
                       200, 200, 200, 150,
                       hwnd, NULL, ghInstance, NULL);
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,466

    Re: Can't edit Edit control in DialogBox

    Quote Originally Posted by MasterDucky View Post
    ... I can get the text from the Edit box but I cannot write to it.
    What do you mean by "cannot write"?
    Just set the input focus to this edit control and type in the text. Or use SetDlgItemText...
    Victor Nijegorodov

  3. #3
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,544

    Re: Can't edit Edit control in DialogBox

    Your message loop isn't correct. See http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx

    Code:
    BOOL bRet;
    
    while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
    { 
        if (bRet == -1)
        {
            // handle the error and possibly exit
        }
        else
        {
            TranslateMessage(&msg); 
            DispatchMessage(&msg); 
        }
    }
    You need TranslateMessage() to handle chars etc.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  4. #4
    Join Date
    Dec 2007
    Location
    France
    Posts
    229

    Re: Can't edit Edit control in DialogBox

    Quote Originally Posted by 2kaud View Post
    Your message loop isn't correct. See http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
    That was it. Thank you 2kaud that made my day!

    @Victor
    No, it wasn't working apparently because of this. Thanks though.

  5. #5
    Join Date
    Oct 2012
    Posts
    2

    Re: Can't edit Edit control in DialogBox

    There is, however, no need to check for failure, see: When will GetMessage return -1?

  6. #6
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,544

    Re: Can't edit Edit control in DialogBox

    Quote Originally Posted by ReneK View Post
    There is, however, no need to check for failure, see: When will GetMessage return -1?
    IMO I'd stick with what Microsoft recommends on their official site for failure check. However, I usually code it as
    Code:
    BOOL bRet;
    
    while ((bRet = GetMessage(&msg, NULL, 0, 0 )) > 0)
    { 
            TranslateMessage(&msg); 
            DispatchMessage(&msg); 
    }
    
    If (bRet) {
         //Deal with error   
    }
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center