Thread: Getting Integers from an Edit Control

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Seattle
    Posts
    30

    Getting Integers from an Edit Control

    Hi,

    I’m making a “map maker” type program and I’m having some trouble getting information from Dialog Box edit controls. I know how to use EM_GETLINE to get text from an edit control (which I previously used to also work with numbers by using an atoi() call and changing the chars in to integers). Unfortunately I ran in to problems when only entering one character in to the edit control but everything worked fine for anything >= 10. I figured I would never have a map as small as 9x9 so it didn’t really matter… unfortunately now I need to be able to enter single digits for a different Dialog Box.

    I’ve been searching for an EM_GETLINE type parameter to grab integers to but I’ve had no luck so I turned to where I usually turn when I hit a wall. I hope you guys can help.

    Here’s my non-working code right now.

    Code:
    LRESULT CALLBACK SelectMission(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	int  mission = 0;
    	WORD sizeOfInput;
    
    	DisplayInfo* Info = (DisplayInfo*)(DWORD_PTR)GetWindowLongPtr(hDlg, DWLP_USER);
    
    	HRESULT hr = -1;
    
    	UNREFERENCED_PARAMETER(lParam);
    	switch (message)
    	{
    	case WM_INITDIALOG:
    		SetWindowLongPtr(hDlg, DWLP_USER, static_cast<long>(lParam)); 
    		return TRUE;
    
    	case WM_COMMAND:
    		switch (LOWORD(wParam))
    		{
    		case IDCANCEL:
    			EndDialog(hDlg, LOWORD(wParam));
    			break;
    		case IDOK:
    			sizeOfInput = (WORD) SendDlgItemMessage(hDlg, 
                                                        IDC_MISSIONNUMBER, 
                                                        EM_LINELENGTH, 
                                                        (WPARAM) 0, 
                                                        (LPARAM) 0); 
    
                if (sizeOfInput == 0) 
                { 
                    MessageBox(hDlg, 
                               "No characters entered for Mission.", 
                               "Error", 
                               MB_OK); 
    
    		break;
                }
    
                //// Put the number of characters into first word of buffer. 
                //*((LPWORD)mission) = sizeOfInput; 
    
                // Get the characters. 
                SendDlgItemMessage(hDlg, 
                                   IDC_MISSIONNUMBER, 
                                   EM_GETLINE, 
                                   (WPARAM) 0,       // line 0 
                                   (LPARAM) mission); 
    
                //// Null-terminate the string. 
                //height[sizeOfInput] = 0; 
    
    			if(SUCCEEDED(hr))
    				EndDialog(hDlg, LOWORD(wParam));
    									
    			break;
    		}
    	}
    	return FALSE;
    }
    There’s some stuff left over from when I was trying to convert character strings to integers and then remembered that it wouldn’t work. Right now this compiles fine but of course it crashes when it reaches the last SendDlgItemMessage() because of my LPARAM being an integer.

    I’ve been looking through MSDN for an EM_ something that would help my problem but I haven’t found anything yet. Any help would be greatly appreciated.

    Thanks,
    -Peter
    Last edited by Peter5897; 09-16-2006 at 11:05 PM.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Can't you just use the FAQ's suggestion on converting strings to numbers? I'm sure there's a Windows way to do it however.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Location
    Seattle
    Posts
    30
    I tried sending a std::string in to the SendDlgItemMessage() but it won't compile because I can't convert it to an LPARAM. I do send in a character string with another dialog box I'm using and I used the atoi() function to convert the number to an integer but for some reason I can't get back a single digit input from the edit control without getting an error, anything greater than 9 works fine though so I kept it for that dialog box but now I need to be able to take in a single digit.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Location
    Seattle
    Posts
    30
    Alright I feel stupid but for some reason I got my original method of using atoi() working in this dialog box while it still doesn't work perfectly in the other one. How? I'm not sure but since it's working I won't complain and I'll move on to the next problem.

    Thanks.

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    EM_GETLINE, lParam needs to be a character buffer to recieve the line. You should probably do something like.

    Code:
    TCHAR * buffer;
    buffer = new TCHAR[sizeOfInput + 1];
    
    LRESULT text_length = SendDlgItemMessage(hDlg, 
                                   IDC_MISSIONNUMBER, 
                                   EM_GETLINE, 
                                   (WPARAM) 0,       // line 0 
                                   (LPARAM) buffer);
    
    buffer[text_length] = 0;
    int numInControl = _ttoi(buffer);
    Edited

    Or maybe GetDlgItemInt: http://msdn.microsoft.com/library/de...dlgitemint.asp
    Last edited by Tonto; 09-17-2006 at 01:30 AM.

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    EDIT: Too slow to post!

    If the edit control just contains one number, you can use the GetDlgItemInt function. Otherwise, you will need to convert manually.
    Code:
    char    buffer[50];
    WORD    buffer_size = sizeof(buffer) - 1;
    LRESULT text_length = 0;
    memcpy(buffer, &buffer_size, sizeof(WORD));
    
    text_length = SendDlgItemMessage(hDlg, 
                                     IDC_MISSIONNUMBER, 
                                     EM_GETLINE, 
                                     (WPARAM) 0,       // line 0 
                                     (LPARAM) buffer); 
    buffer[text_length] = '\0';
    
    /* Now convert to a number using one of the methods in the FAQ. */
    i = strtol(buffer, NULL, 10);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I send a text string to an edit control
    By marc74 in forum Windows Programming
    Replies: 5
    Last Post: 01-06-2005, 10:14 PM
  2. Restricting input to an edit control
    By bennyandthejets in forum Windows Programming
    Replies: 7
    Last Post: 10-05-2003, 01:10 AM
  3. endless edit control
    By ZerOrDie in forum Windows Programming
    Replies: 3
    Last Post: 03-21-2003, 02:51 AM
  4. Keeping focus on an edit control ...
    By Unregistered in forum Windows Programming
    Replies: 3
    Last Post: 02-19-2002, 02:12 AM
  5. please help visual c++ edit control boxes
    By alcoholic in forum C++ Programming
    Replies: 3
    Last Post: 02-05-2002, 02:39 PM