Thread: Numbers as only valid input in edit control

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    54

    Numbers as only valid input in edit control

    Im having issues with allowing the users only to enter a number in an edit control. I tried using IsDigit(value) but has trouble when the number when negative.

    Is there a valid way of allowing the user to only enter numbers but without limiting to positive? I wanted to avoid having a second option they would have to check for negative / positive.

    Code:
    switch (message)
    	{
    	case WM_INITDIALOG:
    		SetDlgItemInt(hDlg, IDC_XVECTOR, theDoc.hBall.sBallVec.cx, TRUE);
    		SetDlgItemInt(hDlg, IDC_YVECTOR, theDoc.hBall.sBallVec.cy, TRUE);
    		return TRUE;
    
    	case WM_COMMAND:
    		if (LOWORD(wParam) == IDOK) 
    		{
    			LONG tempX = GetDlgItemInt(hDlg, IDC_XVECTOR, FALSE, TRUE);
    			LONG tempY = GetDlgItemInt(hDlg, IDC_YVECTOR, FALSE, TRUE);
    			
    			if(tempX < -10)
    				theDoc.hBall.sBallVec.cx = -10;
    			else if(tempX > 10)
    				theDoc.hBall.sBallVec.cx = 10;
    			else
    				theDoc.hBall.sBallVec.cx = tempX;
    
    			if(tempY < -10)
    				theDoc.hBall.sBallVec.cy = -10;
    			else if(tempY > 10)
    				theDoc.hBall.sBallVec.cy = 10;
    			else
    				theDoc.hBall.sBallVec.cy = tempY;
    
    			EndDialog(hDlg, LOWORD(wParam));
    			InvalidateRect(hDlg, NULL, TRUE);
    			return TRUE;
    		}
    		else if(LOWORD(wParam) == IDCANCEL)
    		{
    			EndDialog(hDlg, LOWORD(wParam));
    			return TRUE;
    		}
    		break;
    	}
    	return FALSE;

    thanks

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Have you considered subclassing the edit control to only allow input of 0 thru 9 and the - character?

  3. #3
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    set its window style to ES_NUMBER

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    set its window style to ES_NUMBER
    Nope, I was thinking more along the lines of this example

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  3. Buttons + Edit Control
    By jay kay in forum Windows Programming
    Replies: 6
    Last Post: 03-09-2005, 05:36 PM
  4. 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
  5. Rich edit control example Win API
    By Echidna in forum Windows Programming
    Replies: 1
    Last Post: 09-17-2001, 02:12 AM