Thread: Default Button Problem

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    27

    Default Button Problem

    Ok, so I'm making an application and am putting the controls into the main window normally, rather than as a dialog box, for various reasons.

    As you can see in the attachment, my Send button is set to BS_DEFPUSHBUTTON style. It's all being created when a user opens a new tab, and appears in the tab:

    Code:
    UserTab[NewID].hEdit = CreateWindowEx(WS_EX_CLIENTEDGE,WC_EDIT,0,WS_VISIBLE | WS_CHILD | WS_TABSTOP | ES_AUTOVSCROLL | ES_MULTILINE,15,(rcClient.bottom * 0.7) + 40,(rcClient.right * 0.73),48,hwnd,NULL,hInstance,0);
    SendMessage(UserTab[NewID].hEdit,WM_SETFONT,(WPARAM)hFont,(LPARAM)0);
    
    UserTab[NewID].hSend = CreateWindowEx(0,WC_BUTTON,"Send",WS_VISIBLE | WS_CHILD | WS_TABSTOP | BS_DEFPUSHBUTTON,(rcClient.right * 0.7) + 30,(rcClient.bottom * 0.7) + 40,75,23,hwnd,(HMENU)IDOK,hInstance,0);
    SendMessage(UserTab[NewID].hSend,WM_SETFONT,(WPARAM)hFont,(LPARAM)0);
    
    UserTab[NewID].hSend = CreateWindowEx(0,WC_BUTTON,"Cancel",WS_VISIBLE | WS_CHILD | WS_TABSTOP | BS_PUSHBUTTON,(rcClient.right * 0.7) + 30,(rcClient.bottom * 0.7) + 65,75,23,hwnd,(HMENU)IDCANCEL,hInstance,0);
    SendMessage(UserTab[NewID].hSend,WM_SETFONT,(WPARAM)hFont,(LPARAM)0);
    However, no matter what I do, when I press Enter in the edit control, it doesn't automate me pressing IDOK. I've tried passing the parent window DM_SETDEFID messages pointing to the IDOK button, but NOTHING'S HAPPENING <___<

    Help? I've been working on this code for hours now to no avail whatsoever, including scouring through every MSDN article on it and ramming a billion searches into Google and Ask, absolutely nothing works thusfar =\

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    142
    You need to subclass edit controls and put some code that looks like this:

    Code:
    if (wParam == VK_RETURN)  {
            
       if (iHaveDefaultButton)  {
          SendMessage (parentHwnd, WM_COMMAND, MAKELONG(defaultButtonID, BN_CLICKED), (LPARAM)btnHwnd);
          return (0);
       }
    }

  3. #3
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    UserTab[NewID].hSend = CreateWindowEx(0,WC_BUTTON,"Send",...

    UserTab[NewID].hSend = CreateWindowEx(0,WC_BUTTON,"Cancel",...
    I hope that's just a copy and paste to the forum error and not the reason for that rapidly developing baldspot

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    27
    Yes, yes it is, there was a formatting problem when I first posted, so I went edited and copypasted certain parts together, didn't realise that was wrong xD

    It's correct in the main program so meh

    And thanks idelovski, that worked perfectly :P

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    142
    Glad I could help.

    But there's more if you're not using standard dialogs.

    You need to check if current edit box doesn't require Return key. Somehow determine if it's a multiline edit box. Next, even combo boxes and any other control can have focus so you need to put return key checks in those controls too. Then you need to handle Tab key. Then, there's that mouse wheel; then, ...

    Find Raymond Chen's articles about dialogs (or buy his book).
    Last edited by idelovski; 12-23-2008 at 12:07 PM.

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    The reason for this behaviour is the interaction between

    ES_AUTOVSCROLL | ES_MULTILINE styles.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. elliptical button
    By geek@02 in forum Windows Programming
    Replies: 0
    Last Post: 11-21-2006, 02:15 AM
  2. Dialog Box Problems
    By Morgul in forum Windows Programming
    Replies: 21
    Last Post: 05-31-2005, 05:48 PM
  3. advanced problem
    By Tclord in forum C++ Programming
    Replies: 1
    Last Post: 02-07-2005, 02:37 AM
  4. Window won't display on button command!?
    By psychopath in forum Windows Programming
    Replies: 6
    Last Post: 06-22-2004, 08:12 PM
  5. problem with answer going to default?
    By Patrick1234 in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2002, 09:11 AM