Thread: using GetDlgItemInt

  1. #1
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052

    using GetDlgItemInt

    I was looking for version of GetDlgItemText to read an integer from an edit box and found GetDlgItemInt. This is what the msdn library says:
    The GetDlgItemInt function translates the text of a specified control in a dialog box into an integer value.
    Does this mean that I should use GetDlgItemInt and then GetDlgItemText to read it into a variable?

  2. #2
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    Is there anything in Programming Windows 5th Edition that I could look up that could help me?

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    223

    GetDlgItemInt

    GetDlgItemInt seems a bit complex since it returns an unsigned number... I suppose you can always cast it and somehow convert it to a signed number if that is what you need... What I like about it is that it will notify you if the translation took place..

    i.e.


    Code:
    BOOL bWorked = FALSE;
    UINT  nValue = 0;
    
    nValue = GetDlgItemInt( IDC_EDIT1,  &bWorked );
    
    if( bWorked == TRUE )
    {
    
        CString s;
        s.Format("Value retrieved: %d", nValue);
        MessageBox( s );
    }
    
    
    .....
    
    Personally I find it much easier to do this...
    
    CString sValue;
    int nValue;
    GetDlgItemText(IDC_EDIT1, &sValue);
    
    nValue = atoi( sValue.GetBuffer( sValue.GetLength() );
    sValue.ReleaseBuffer();
    
    
    .....
    
    
    of course this will only work from withing the dialog class...
    otherwise you must use
    
    MyDialog.GetDlgItemText(.....)... only in modeless
    if modal you must wait for the return value
    
    if( MyDialog.DoModal() == IDOK )
    {
            MyDialog.m_sMyVariable;
    
          ......
    }


    or better yet....

    map it to the dialog using the class wizard....
    zMan

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Win32(no offence intended to ZMan's fine mfc example):

    Use GetDlgItemInt to return the numerical contents of an edit control (make sure the control has ES_NUMBER style to avoid confusion).

    If you want to get the contents as a string then use GetDlgItemText.

    eg.
    Code:
    BOOL bSuccess;
    BOOL bSigned=FALSE; //TRUE if you want signed int only returned
    int nNumericContents=GetDlgItemInt(hDlg,IDC_NUM_EDIT,&bSuccess,bSigned);
    or, using GetDlgItemText:
    Code:
    TCHAR chBuffer[20];
    int nNumCharsCopied=GetDlgItemText(hDlg,IDC_NUM_EDIT,chBuffer,20);
    Where, in both cases, hDlg is the handle of the parent wnd, IDC_NUM_EDIT is the id for the edit cntrl. The return value from GetDlgItemText is shown for demo purposes only; you can use it if you wish.

    Hope that helps.

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    223

    MFC Example

    I am so used to working only with MFC that I assume everyone else is using it....

    Thanks for keeping me straight, Ken.

    Out of curiosity what is the reason that so many of you chose not to use the MFC?... Just wondering.
    zMan

  6. #6
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    >>Thanks for keeping me straight, Ken.

    Sure, no problem

    Hmm...methinks this is going to cause even more confusion

  7. #7
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>Sure, no problem

    Most gracious of you, -KEN-

    >>Out of curiosity what is the reason that so many of you chose not to use the MFC?...

    Don't know about anyone else and can't speak for them either but mfc is a compiler specific library and I like to play around with different compilers. Also I don't like the imposition of someone else's idea of what a class wrapper should or shouldn't be. I hate not being able to have WinMain staring me in the face. Code bloat that isn't my code bloat....

    I better stop now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GetDlgItemInt not working
    By Brigs76 in forum C Programming
    Replies: 3
    Last Post: 04-24-2005, 09:09 PM
  2. getting a Double with GetDlgItemInt
    By Iamien in forum Windows Programming
    Replies: 4
    Last Post: 09-17-2003, 02:40 AM
  3. GetDlgItemInt returning only 0
    By Isometric in forum Windows Programming
    Replies: 10
    Last Post: 12-13-2001, 08:31 PM