Link to home
Start Free TrialLog in
Avatar of navigator010897
navigator010897

asked on

Edit Boxes, pressing Return to process.

Hi,

I'm in my stupid mode again, and don't see what I am doing wrong.

In my app, I have an edit box - single line.  I need to be able to enter text and then press the Return key to "submit" the text (kinda like a chat program)

I found in the MSDN a little thing about setting the default button and all, but that didn't seem to help.

every time I press Enter all I get is a ding sound.

In spy, it appears that when I press Enter - the message is going to the edit box, but isn't being passed on to the main dialog box in any way.

Can anyone give some tips on this rather simple issue.

Short and sweet again:
1 edit box.
Needs to notify the main program when Enter is pressed (auto push on a default button would be fine if I can get it to work)

What it's doing:
When enter is pressed, the program (actually windows AFAIK) dings
Avatar of cookre
cookre
Flag of United States of America image

Where's the focus after the ding?
Does it stay in the box?
Any other controls present?
Avatar of navigator010897
navigator010897

ASKER

Focus remains within the edit box.

There are 4 buttons, 3 functional, 1 hidden and disabled - in place for future functionality, an edit box, and a list box (listbox acts as display window for output).

I have set the default button action to the "speak" button, which is how I have to tell the program right now to process what is in the edit box.

Derive a class from CEdit, Use its OnKeyDown function, from there u can post message to the parent window. ie PostMessage to Dialog.

GOOD LUCK
Roshmon:  I am not using C++, nor MFC - this is pure win32 =(  I don't know enough about MFC to even attempt such a feat anyway
ASKER CERTIFIED SOLUTION
Avatar of Roshan Davis
Roshan Davis
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of StevenB
1) To prevent the messagebeep you have to set the keypress to character 0 in the appropriate handler.

2) To activate some other event handler (such as a button click) you need to call it explicitly in the keypress handler of the edit control. The Default property of the button will not come into play when an edit control has focus.

In Delphi this process would look something like this:

procedure TForm1.Edit1KeyPress(Sender: TObject;
  var Key: Char);
begin
  If (Key = #13) then begin
    if Button1.Enabled then Button1Click(Sender);
    Key := #0;
  end
  else inherited;
end;

If you want help converting this code into some other language then let me know.
Subclass the edit control.  

In response to WM_GETDLGCODE, return DLGC_WANTALLKEYS.

Now handle WM_CHAR normally, and intercept VK_RETURN, or what have you.  In your case, when VK_RETURN is received, you probably want to do a SendMessage(BM_CLICK) to your "speak" button.

In PowerBASIC (DDT style), the subclass callback this could look something like this:

CALLBACK FUNCTION SubClassEditProc
    IF CBMSG = %WM_GETDLGCODE THEN
        FUNCTION = %DLGC_WANTALLKEYS
        EXIT FUNCTION
    ELSEIF CBMSG = %WM_CHAR THEN
        IF CBWPARAM = %VK_TAB THEN
            '... process TAB CONTROL here
        ELSEIF CBWPARAM = %VK_RETURN THEN
            '... process ENTER here or swallow with EXIT FUNCTION
        END IF
    END IF
    ' Pass the message to the original window procedure
    FUNCTION = CallWindowProc(oldProc, CBHNDL, CBMSG, CBWPARAM, CBLPARAM)
END FUNCTION



Er, probably better to make that a PostMessage(BM_CLICK) rather than a SendMessage().  <smile>
I am still working on a program that needs this - I haven't been able to do a conversion yet from the PB to C, but hope to in the next few days - assuming your answer is correct, I'll leave a grade then.  I'm sorry for the delay, unfortunately your post came during a family emergency and I'm running a bit behind.
This did provide the information I needed in order to create my answer.  A little more info on subclassing would have made it a bit easier, but I was able to fix my problem.