How do I use an edit box?

This is a discussion on How do I use an edit box? within the Windows Programming forums, part of the Platform Specific Boards category; I'm extremely new to windows programming and am just getting my footing. So far I can make all kinds of ...

  1. #1
    The Earth is not flat. Clyde's Avatar
    Join Date
    Mar 2002
    Posts
    1,420

    How do I use an edit box?

    I'm extremely new to windows programming and am just getting my footing. So far I can make all kinds of boxes/buttons, etc.

    What i want to know is how to take the contents of an edit box as a string.

    I'll stick an edit box next to a button, then when the user presses the button I want to do something with the text thats been typed in. I can make the box and the button, and i can tell when the button is pressed, but I don't know how to turn the contents of the edit box into a string or a char array so I can do stuff with it.

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    The GetDlgItemText function retrieves the title or text associated with a control in a dialog box.

    Code:
    UINT GetDlgItemText(
    
        HWND hDlg,	// handle of dialog box
        int nIDDlgItem,	// identifier of control
        LPTSTR lpString,	// address of buffer for text
        int nMaxCount 	// maximum size of string
       );
    Parameters

    hDlg

    Identifies the dialog box that contains the control.

    nIDDlgItem

    Specifies the identifier of the control whose title or text is to be retrieved.

    lpString

    Points to the buffer to receive the title or text.

    nMaxCount

    Specifies the maximum length, in characters, of the string to be copied to the buffer pointed to by lpString. If the length of the string exceeds the limit, the string is truncated.
    "There are three kinds of people in the world...
    Those that can count and those that can't."

  3. #3
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    If you aren't using a dialog box, look up GetWindowTextLength and GetWindowText.

    int GetWindowTextLength(

    HWND hWnd // handle of window or control with text
    );
    Parameters

    hWnd

    Identifies the window or control.

    int GetWindowText(

    HWND hWnd, // handle of window or control with text
    LPTSTR lpString, // address of buffer for text
    int nMaxCount // maximum number of characters to copy

    );
    Parameters

    hWnd

    Identifies the window or control containing the text.

    lpString

    Points to the buffer that will receive the text.

    nMaxCount

    Specifies the maximum number of characters to copy to the buffer. If the text exceeds this limit, it is truncated.

  4. #4
    The Earth is not flat. Clyde's Avatar
    Join Date
    Mar 2002
    Posts
    1,420
    Hi, thanks for the replies, i'm using this code to create the edit box:

    Code:
    CreateWindowEx(WS_EX_CLIENTEDGE,                      //more or 'extended' styles
                           TEXT("EDIT"),                          //'class' of control to create
                           TEXT("EDIT CONTROL"),                  //the control caption
                           WS_CHILD|WS_VISIBLE|WS_BORDER,         //control style: how it looks
                           210,                                    //control position: left
                           10,                                    //control position: top
                           200,                                   //control width
                           30,                                    //control height
                           hwnd,                                  //parent window handle
                           NULL,                                  //control's ID
                           hInst,                                 //application instance
                           NULL);
    i don't think thats a dialog box, is it? If it is, how do i get an "identifier of control"?

    When i use the other function "GetWindowText(), it retrieves the text from menu bar at the top, not what is typed into the box.

    Thanks for your help, this stuff is confusing! So many many many different commands.

  5. #5
    Registered User cppdude's Avatar
    Join Date
    Jan 2002
    Posts
    62
    the class name is just "EDIT". Forget all the Text() stuff. I dont see what the point of it is.

  6. #6
    The Earth is not flat. Clyde's Avatar
    Join Date
    Mar 2002
    Posts
    1,420
    "the class name is just "EDIT". Forget all the Text() stuff. I dont see what the point of it is."

    Sorry, i don't understand what you're saying; You don't think i should use the functions the previous posters have suggested?

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    I believe he was talking about ur Window Class, (The bolded line in the example)

    Code:
    CreateWindowEx(WS_EX_CLIENTEDGE,                      //more or 'extended' styles
                           TEXT("EDIT"),                          //'class' of control to create
                           TEXT("EDIT CONTROL"),                  //the control caption
                           WS_CHILD|WS_VISIBLE|WS_BORDER,         //control style: how it looks
                           210,                                    //control position: left
                           10,                                    //control position: top
                           200,                                   //control width
                           30,                                    //control height
                           hwnd,                                  //parent window handle
                           NULL,                                  //control's ID
                           hInst,                                 //application instance
                           NULL);
    "There are three kinds of people in the world...
    Those that can count and those that can't."

  8. #8
    The Earth is not flat. Clyde's Avatar
    Join Date
    Mar 2002
    Posts
    1,420
    I'm still not following, what do I do with my windows class, to get the contents of the edit box into a character array or string?

  9. #9
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    Nothing, he was just telling you you dont need Text("EDIT) you only need "EDIT"

    Use the GetWindowText() function to get teh text from your edit box
    "There are three kinds of people in the world...
    Those that can count and those that can't."

  10. #10
    The Earth is not flat. Clyde's Avatar
    Join Date
    Mar 2002
    Posts
    1,420
    "Nothing, he was just telling you you dont need Text("EDIT) you only need "EDIT" "

    Aah gotcha

    "Use the GetWindowText() function to get teh text from your edit box"

    if i include this:

    Code:
    GetWindowText(hwnd, buffer, 16);

    The text in buffer is not the text from the edit box, but is the text from the window title. What am I doing wrong?

  11. #11
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Make sure the handle is the handle of the Edit.

  12. #12
    The Earth is not flat. Clyde's Avatar
    Join Date
    Mar 2002
    Posts
    1,420
    how do i find out the handle of the edit? I don't see a handle for the edit box in the code that created it.

    EDIT: Aha! I've done it, i created another handle called temp and made it equal to the CreateWindowEx() function.
    Last edited by Clyde; 05-15-2002 at 05:21 PM.

  13. #13
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>the class name is just "EDIT". Forget all the Text() stuff. I dont see what the point of it is.<<

    The point is UNICODE. If you are compiling for win2k/nt/xp and you #define UNICODE prior to #include <windows.h> then strings are wide (2bytes) and wide char versions of API fns, which are native to winnt/2k/xp are called instead of the ANSI ones which are native to win9x. TEXT and _T(#include <tchar.h> to use this one) macros ensure that text strings are of the right type depending on whether UNICODE is defined or not. In other words by using them you can compile either for UNICODE platforms (win nt/2k/xp) or win9x (ANSI). WinNT/2k/xp does have the ANSI functions but internally converts and calls the UNICODE ones.

  14. #14
    Registered User cppdude's Avatar
    Join Date
    Jan 2002
    Posts
    62
    cheers ken. I guess this means i am going to have to change stuff if i want my progs to run on win9x right?

  15. #15
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>I guess this means i am going to have to change stuff if i want my progs to run on win9x right?<<

    No. As I said,"WinNT/2k/xp does have the ANSI functions but internally converts and calls the UNICODE ones". All that happens when you don't have UNICODE #defined is that winnt/2k/xp internally converts to UNICODE.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 02-13-2008, 02:59 PM
  2. edit box
    By beene in forum Windows Programming
    Replies: 3
    Last Post: 11-11-2006, 04:40 AM
  3. edit box affecting displaying of text?
    By algi in forum Windows Programming
    Replies: 4
    Last Post: 05-04-2005, 04:28 PM
  4. How to program a "back" button with MFC
    By 99atlantic in forum Windows Programming
    Replies: 3
    Last Post: 04-26-2005, 09:34 PM
  5. display a file in dropdown edit box
    By sunburnbyRA in forum Windows Programming
    Replies: 2
    Last Post: 03-10-2004, 01:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21