Thread: Using GetWindowText

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    81

    Using GetWindowText

    I'm trying to get a number from an edit box and just have it multiplyed but since GetWindowText uses a character buffer i can't multiply it and if i use something like atoi() when i go to pass it back to the SetWindowText it tells me that it can't convert char to int. So how would i get an intiger from an edit box(i'm not using a dialog) do some mathmatical equation on it and return it to the SetWindoText to display it back in the edit box again?

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Here is a sample code.

    -----
    myEditBox.SetWindowText("2002");

    // Now you want to multiply 2002 by 2

    CString num;
    myEditBox.GetWindowText(&num);
    long temp = atol(&num) * 2;
    num.Format("%d", temp);
    myEditBox.SetWindowText(num);
    -----

    Kuphryn

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    81

    Still Doesn't Work

    I'm still getting the error Cannot convert 'int' to 'const char *' that was my original problem. I'm usinc borlands compliler 5.5 if that makes any difference.

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Oh. You're using Borland. I am not sure about Borland C++.

    Kuphryn

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    81

    anyone else

    anyone else got any ideas how to make this work?

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Code:
    #include <windows.h>
    #include <stdlib.h>
    
    
    
    
    LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
    
    char szClassName[] = "WindowsApp";
    HINSTANCE g_hInst;
    
    
    int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil)
    
    {
        HWND hwnd;               
    	MSG messages;            
    	WNDCLASSEX wincl; 
    	
    	g_hInst = hThisInstance;
    
        wincl.hInstance = hThisInstance;
        wincl.lpszClassName = szClassName;
        wincl.lpfnWndProc = WindowProc;      
        wincl.style = CS_DBLCLKS;               
        wincl.cbSize = sizeof(WNDCLASSEX);
        wincl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
        wincl.hCursor = LoadCursor(NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL;
        wincl.cbClsExtra = 0; 
        wincl.cbWndExtra = 0; 
    	wincl.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH);
    
        
        if(!RegisterClassEx(&wincl)) return 0;
    
       
        hwnd = CreateWindowEx(0,szClassName, "Poor Man's Calc",
    		WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
    		200,120, HWND_DESKTOP,NULL,            
               hThisInstance,NULL );    
    
       
        ShowWindow(hwnd, nFunsterStil);
    	UpdateWindow(hwnd);
       
        while(GetMessage(&messages, NULL, 0, 0))
        {
              TranslateMessage(&messages);
              DispatchMessage(&messages);
        }
    
        return messages.wParam;
    }
    
    
    LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, 
    							WPARAM wParam, LPARAM lParam)
    {
    	static HWND hEdit,hButton;
    	char str[10];
    	int x;
    
        switch (message)                
        {
               case WM_CREATE:            
               
    			   hEdit = CreateWindow("EDIT","",WS_CHILD | WS_VISIBLE,
    				   10,10,100,30,hwnd,NULL,g_hInst,NULL);
    
    			   hButton = CreateWindow("BUTTON","Multiply by 10",
    				   WS_CHILD | WS_VISIBLE,10,50,100,30,hwnd,NULL,g_hInst,NULL);
               break;
               
               case WM_COMMAND:
    			   if((HWND) lParam == hButton){
    				   GetWindowText(hEdit,str,10);
    				   if(str[0] == 0)
    					   break;
    				   x = atoi(str);
    				   if(x)
    					   SetWindowText(hEdit,itoa(x*10,str,10));
    				   else
    					   MessageBox(hwnd,"Error",NULL,NULL);
    			   }
               
               break;
               
               case WM_DESTROY:
               PostQuitMessage(0);        
               break;
               default:                   
               return DefWindowProc(hwnd, message, wParam, lParam);
        }
        return 0;
    }
    Use atoi and itoa....

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    You could use GetDlgItemInt()
    "There are three kinds of people in the world...
    Those that can count and those that can't."

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    81

    Thanks

    Thanks, that worked.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    81
    i have one more probles, it works fine, you enter the number and it gets multiplyed but when i certain numbers though it returns a value thats not it multiplies like i could enter 6 and it will return the letter c. any one have a fix for this, also it doesn't read a number like 1.2 it only multiplys the 1 in 1.2

    And is there a style or something that only allows numbers to be enter in an edit box?

  10. #10
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    use something like sprintf()

    After you get the number from the edit (use ES_NUMBER for numerical input only, or isdigit() on string)

    atoi() or atof(), make sure you cast correctly

    then sprintf() back to text to set into the edit.

    sprintf(sBuffer,"%02.2f",fNum);
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hung up on GetWindowText
    By Viper187 in forum Windows Programming
    Replies: 18
    Last Post: 09-06-2008, 08:30 AM
  2. GetWindowText() not working with Vista???
    By Abda92 in forum Windows Programming
    Replies: 10
    Last Post: 04-07-2008, 01:29 PM
  3. GetwindowText problem
    By spanker in forum Windows Programming
    Replies: 4
    Last Post: 03-27-2008, 10:25 AM
  4. GetWindowText doesn't return the text
    By Rune Hunter in forum Windows Programming
    Replies: 16
    Last Post: 09-23-2005, 04:49 PM
  5. GetWindowText -> string?
    By XSquared in forum Windows Programming
    Replies: 3
    Last Post: 08-27-2003, 03:58 PM