Win32 sums

This is a discussion on Win32 sums within the Windows Programming forums, part of the Platform Specific Boards category; Hi all, I am making the transition from console apps to GUIs, and have managed to create a windowed application ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    128

    Win32 sums

    Hi all,

    I am making the transition from console apps to GUIs, and have managed to create a windowed application which opens a dialog with a two text inpit spaces and an 'add' button plus a Listbox intended to show the result of an addition of the two previously entered numbers. Here is the code I have for the addition sun when 'add' is pressed:

    Code:
    case IDC_ADD: //Pressing the 'add' button.
    {
    BOOL bSuccess;
    int first = GetDlgItemInt(hwnd, IDC_TEXT, &bSuccess, FALSE); //Getting number from first box.
    int second = GetDlgItemInt(hwnd, IDC_TEXT2, &bSuccess, FALSE); //Getting number from second box.
    int result = first + second; //Adding those together.
    break;
    }
    Now, how to I get the result to be applied to the Listbox?

    Thanks.

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    287
    Well just use the listbox message LB_ADDSTRING to do it; that's described as:

    Code:
    LB_ADDSTRING  
    wParam=0;//not used; must be zero 
    lParam=(LPARAM)(LPCTSTR)lpsz;//address of string to add
    In addition you can select the last inserted value to ensure that it is visible using LB_SETCURSEL, specifying the last inserted index. Let me implement a sample code with it:

    Code:
    char bff[100];
    int li;
    
    sprintf(bff,"%d",result);
    li=SendMessage(listbox_handler,LB_ADDSTRING,0,(LPARAM)bff);
    SendMessage(listbox_handler,LB_SETCURSEL,(WPARAM)li,0);
    Hope that helps
    Niara

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    128
    Thanks - I used this code:

    Code:
    				case IDC_ADD: //Pressing the 'add' button.
    					{
    					 int len = GetWindowTextLength(GetDlgItem(hwnd, IDC_TEXT));
    					 int len2 = GetWindowTextLength(GetDlgItem(hwnd, IDC_TEXT2));
    
    				char* buf;
    				char* buf2;
    
    				buf = (char*)GlobalAlloc(GPTR, len + 1);
    				buf2 = (char*)GlobalAlloc(GPTR, len2 + 1);
                    GetDlgItemText(hwnd, IDC_TEXT, buf, len + 1);
    				GetDlgItemText(hwnd, IDC_TEXT2, buf2, len2 + 1);
                    Third=atof(buf)+atof(buf2);
                    char strFirst[100];
    				sprintf(strFirst,"%0.0f",Third);
    				SetDlgItemText(hwnd, IDC_TEXT3, strFirst);
    					 break;

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    287
    "...buf = (char*)GlobalAlloc(GPTR, len + 1);..."

    Also remember to call GlobalFree when you have finished to work with the mallocated buffers.

    Glad to help
    Niara

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    I would not dynamically alloc memory for this, much more complex than required.

    I would just use a static buffer and ensure the edit controls had the ES_NUMBER style.

    Code:
    [char szBuf[MAX_PATH] = {0};
    If I was worried that the text entered would exceed the buffer length I would call EM_SETLIMITTEXT in the WM_CREATE/WM_INITDIALOG msg (and limit it to MAX_PATH-1).
    "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

  6. #6
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    287
    I think the same as novacain except in one thing: ES_NUMBER won't let the user enter point, coma or apostrophe characters to notate a floating point value, while george7378 is expecting to get a textual value and convert it to float (atof).

    Niara

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. First time Creating Win32 application with UI
    By chiefmonkey in forum Windows Programming
    Replies: 9
    Last Post: 09-23-2009, 12:44 PM
  2. Win32 API or Win32 SDK?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 07-20-2005, 04:26 PM
  3. OLE Clipboard :: Win32 API vs. MFC
    By kuphryn in forum Windows Programming
    Replies: 3
    Last Post: 08-11-2002, 06:57 PM
  4. Thread Synchronization :: Win32 API vs. MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 08-09-2002, 10:09 AM
  5. Win32 API Tutorials?
    By c++_n00b in forum C++ Programming
    Replies: 9
    Last Post: 05-09-2002, 04:51 PM

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