How can i get an integer from an edit box?

This is a discussion on How can i get an integer from an edit box? within the Windows Programming forums, part of the Platform Specific Boards category; Ok, I have an edit box, and number gets put in it, I want to put that number into an ...

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

    How can i get an integer from an edit box?

    Ok, I have an edit box, and number gets put in it, I want to put that number into an int.

    Since GetWindowText() only works with character arrays, i made a char array, used the GetWindowText function, then made my int variable equal to the first bit of the char array (int number = array[0])

    but it didn't work. Any suggestions?
    Last edited by Clyde; 05-19-2002 at 05:52 AM.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Try GetDlgItemInt:
    Code:
    int nRetVal=GetDlgItemInt(hDlg,nCntrlID,0,0);
    where hDlg is the wnd or dlg handle, nCntrlID is the edit control identifier. This will return unsigned integers; if you want signed integers then set the last parameter of the function to TRUE (1). If you want to test for function success then supply the address of a BOOL variable as the third parameter, which will be set to TRUE or FALSE depending on the success or failure of the function.

  3. #3
    The Earth is not flat. Clyde's Avatar
    Join Date
    Mar 2002
    Posts
    1,420
    Thanks Ken, unfortuneately it's not working, i think the interger always becomes zero, and if i use the third parameter, it reveals that the function has failed.

    I'm not sure what i'm doing wrong here.

    I have created an edit box, then deactivated it:

    Code:
    temp5 = CreateWindowEx(WS_EX_CLIENTEDGE,                      //more or 'extended' styles
                           TEXT("EDIT"),                          //'class' of control to create
                           NULL,                  //the control caption
                           WS_CHILD|WS_VISIBLE|WS_BORDER,         //control style: how it looks
                           90,                                    //control position: left
                           190,                                    //control position: top
                           25,                                   //control width
                           25,                                    //control height
                           hWnd,                                  //parent window handle
                           HMENU(105),                                  //control's ID
                           hInst,                                 //application instance
                           NULL);    
                           
                           EnableWindow(temp5, FALSE);
    Then i have a check box that when clicked activates the edit box (this seems to work fine; when i check the box the edit box goes "ungrey", when i remove the check it greys up again);

    I have this code:

    Code:
    id1 = GetDlgCtrlID (temp5);
    word3=GetDlgItemInt(temp5,id1,point,0);
    
    if (test == false)
    							MessageBox( NULL,
                        TEXT("Didn't Work"),
                        TEXT("ERROR"), 
                        MB_OK|MB_ICONERROR);
    Word3 is a static int, and point is a static int* pointing at test.

    When i run the program word3 is always 0 (atleast it seems to be always zero) and i always get the message box coming saying that the function has failed.

    Any suggestions?

    Edit: I've managed to get it working, i went back to using GetWindowText() then used atoi() to convert the char array into an integer, though I'm still curious as to why when i used GetDlgCtrlID () it didn't work.
    Last edited by Clyde; 05-19-2002 at 09:02 AM.

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Not easy to be sure with the small amount of code presented, but, "temp5" would appear to be the handle of the edit box control, you are passing that as the first parameter to GetDlgItemInt(). GetDlgItemInt() wants the handle to the dialog box as it's first parameter. I suspect that call to be failing, it returns 0 on failure, with the third parameter indicating an error occurred, (you need to pass an address there, I can't see from what you have supplied if this is the case in your program). Certainly, the test you are doing does not involve any of the relevent variables, so again, unless you are doing something in the cdoe you have not shown, this will not work.

    A call to GetLastError() should tell you what's going on.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    The Earth is not flat. Clyde's Avatar
    Join Date
    Mar 2002
    Posts
    1,420
    "GetDlgItemInt() wants the handle to the dialog box as it's first paramete"

    Aah, i see, well i don't have a dialog box (i think) you're right i was passing in the handle of the edit box.

    I was using a pointer to an int for the 3rd parameter.

    Thanks for your help Adrian

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    There is a function in C which does this sort of conversion, you know. It's, of course, atoi(). Just call GetWindowText on the char[] variable, and convert it to an int with atoi(). Also, if GetWindowText returns a pointer to the string, you could even do:

    int num = atoi( GetWindowText( hEdit, buff, len ) );
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    The Earth is not flat. Clyde's Avatar
    Join Date
    Mar 2002
    Posts
    1,420
    Yea thats how i got it to work in the end

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  2. WS_HSCROLL in ES_READONLY edit box error
    By Homunculus in forum Windows Programming
    Replies: 4
    Last Post: 02-13-2006, 07:46 AM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09:43 AM
  4. How to program a "back" button with MFC
    By 99atlantic in forum Windows Programming
    Replies: 3
    Last Post: 04-26-2005, 08:34 PM
  5. display a file in dropdown edit box
    By sunburnbyRA in forum Windows Programming
    Replies: 2
    Last Post: 03-10-2004, 12:58 PM

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