Thread: Edit Box Question

  1. #1
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644

    Edit Box Question

    After restarting my project (I fixed my memory leakage problems), I need some more help. Does anyone know how to clear the contents of a edit box when someone wants to start a new project?

    Thanks in advance

  2. #2
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    You can use SendMessage() to send the WM_SETTEXT message to the editbox. Just set the text to "".
    An application sends a WM_SETTEXT message to set the text of a window.

    WM_SETTEXT
    wParam = 0; // not used; must be zero
    lParam = (LPARAM)(LPCTSTR)lpsz; // address of window-text string


    Parameters

    lpsz

    Value of lParam. Points to a null-terminated string that is the window text.

    Return Values

    The return value is TRUE if the text is set. It is FALSE (for an edit control), LB_ERRSPACE (for a list box), or CB_ERRSPACE (for a combo box) if insufficient space is available to set the text in the edit control. It is CB_ERR if this message is sent to a combo box without an edit control.
    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  3. #3
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Originally posted by sean345
    You can use SendMessage() to send the WM_SETTEXT message to the editbox. Just set the text to "".


    - Sean
    Thank you. I'll see what happens

  4. #4
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Ok, I tried what you said (I think). Here's the code I did:
    Code:
    		case IDM_NEW:
    			//MessageBox(hwnd, "Option not in", NULL, NULL);
    			SendMessage(hwnd, WM_SETTEXT, 0, 0);
    			break;
    (IDM_NEW worked before [tested with a message box])....there's no errors, it just won't clear the edit box.

  5. #5
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    I think it should be:
    Code:
    SendMessage(hwnd, WM_SETTEXT, 0,(LPCTSTR)"");
    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  6. #6
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Originally posted by sean345
    I think it should be:
    Code:
    SendMessage(hwnd, WM_SETTEXT, 0,(LPCTSTR)"");
    - Sean
    Code:
    C:\Program Files\Microsoft Visual Studio\MyProjects\Window Code Test\Window Code Test.cpp(40) : error C2664: 'SendMessageA' : cannot convert parameter 4 from 'char *' to 'long'
    Tried what you had above, and that's the error I get (anyone know why it says long?)

  7. #7
    Registered User Dohojar's Avatar
    Join Date
    Feb 2002
    Posts
    115
    Because you are not sending the message to the right handle. You are just sending it to hwnd, your main window handle. You need to send the message to the editbox. like so:
    Code:
    SendMessage(GetDlgItem(hwnd,ID_OF_YOUR_EDITBOX)),WM_SETTEXT,0,(LPARM)"");
    Dohojar Moajbuj
    Time is the greatest teacher, too bad it kills all its students

  8. #8
    Registered User Dohojar's Avatar
    Join Date
    Feb 2002
    Posts
    115
    You got that error because the 4th parmater in SendMessage has to be cast to (LPARAM) not LPCTSTR
    Dohojar Moajbuj
    Time is the greatest teacher, too bad it kills all its students

  9. #9
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Originally posted by Dohojar
    Because you are not sending the message to the right handle. You are just sending it to hwnd, your main window handle. You need to send the message to the editbox. like so:
    Code:
    SendMessage(GetDlgItem(hwnd,ID_OF_YOUR_EDITBOX)),WM_SETTEXT,0,(LPARM)"");


    >>You got that error because the 4th parmater in SendMessage has to be cast to (LPARAM) not LPCTSTR
    Ok, thanks. That worked. While I'm on the edit box deal, how would you resize the edit box when someone maximizes it (or hwo do you make it fullscreen by default).

    >>After reading the SendMessage() on MSDN, I kinda thought that that is why<<

  10. #10
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Originally posted by Quantrizi
    how would you resize the edit box when someone maximizes it (or hwo do you make it fullscreen by default).
    Handle WM_SIZE message of the parent window. The lParam has the client dimensions of the parent window packed into it which can be used, if required, to position and size your control. Use LOWORD and HIWORD macros on the lParam value to retrieve these values. I believe that a search of this board should have explicit examples of how to use this message handler in this way if you need them.
    Last edited by Ken Fitlike; 08-10-2003 at 01:06 PM.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  11. #11
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Originally posted by Ken Fitlike
    Handle WM_SIZE message of the parent window. The lParam has the client dimensions of the parent window packed into it which can be used, if required, to position and size your control. Use LOWORD and HIWORD macros on the lParam value to retrieve these values. I believe that a search of this board should have explicit examples of how to do this if you need them.
    Would that also resize the edit box also?

  12. #12
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> or hwo do you make it fullscreen by default

    You correct your design/UI so that it is not necessary.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  13. #13
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Code:
    case WM_SIZE:
      {
      WORD cx,cy;
      cx=LOWORD(lParam);  /*parent width*/
      cy=HIWORD(lParam);  /*parent height*/
      
      /*move edit so that it fills parent's client area*/
      /* hEdit is handle to your edit control*/
      MoveWindow(hEdit, 0, 0, cx, cy, TRUE);
      return 0;
      }
    You could obviously alter the parameters to MoveWindow to adjust the size and position of your edit control. Some people prefer to use SetWindowPos instead of MoveWindow.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  14. #14
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Originally posted by Ken Fitlike
    Code:
    case WM_SIZE:
      {
      WORD cx,cy;
      cx=LOWORD(lParam);  /*parent width*/
      cy=HIWORD(lParam);  /*parent height*/
      
      /*move edit so that it fills parent's client area*/
      /* hEdit is handle to your edit control*/
      MoveWindow(hEdit, 0, 0, cx, cy, TRUE);
      return 0;
      }
    You could obviously alter the parameters to MoveWindow to adjust the size and position of your edit control. Some people prefer to use SetWindowPos instead of MoveWindow.
    Ok, thank you. That works perfectly, except that when I do, my status bar is like duplicated (it resizes correctly, but it sticks a copy of itself where it was before resize until I move the cursor down there.

    Also, if a user selects a button to insert something, I'd use WM_GETTEXT and WM_SETTEXT, wouldn't I (to add the text the user selected)?

  15. #15
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>my status bar is like duplicated (it resizes correctly, but it sticks a copy of itself where it was before resize until I move the cursor down there.<<

    Then adjust the size/position of your edit control to take into consideration any other controls, including your status bar, that occupy the client area of the parent window. Use GetClientRect to get the dimensions of the statusbar control.

    edit:
    >> Also, if a user selects a button to insert something, I'd use WM_GETTEXT and WM_SETTEXT, wouldn't I (to add the text the user selected)?<<

    Time to read up on edit controls.
    Last edited by Ken Fitlike; 08-10-2003 at 01:43 PM.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. edit box
    By beene in forum Windows Programming
    Replies: 3
    Last Post: 11-11-2006, 04:40 AM
  2. WS_HSCROLL in ES_READONLY edit box error
    By Homunculus in forum Windows Programming
    Replies: 4
    Last Post: 02-13-2006, 08:46 AM
  3. Multiline Edit Box Parser
    By The Brain in forum Windows Programming
    Replies: 6
    Last Post: 11-01-2005, 07:15 PM
  4. setting fixed floats in edit box
    By WaterNut in forum Windows Programming
    Replies: 4
    Last Post: 08-13-2004, 09:13 AM
  5. How do i make a "new line" with in an edit box?
    By Clyde in forum Windows Programming
    Replies: 3
    Last Post: 05-18-2002, 01:35 PM