Thread: Combining multiple fields into one diaglog message

  1. #1
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001

    Combining multiple fields into one diaglog message

    I have three fields in a dialog box, I want them to be combined into one string when I add a string to the list in the middle of the window:
    int hourlen = GetWindowTextLength(GetDlgItem(hwnd, IDC_HOURS));
    						int minutelen = GetWindowTextLength(GetDlgItem(hwnd, IDC_MINUTES));
    						int secondlen = GetWindowTextLength(GetDlgItem(hwnd, IDC_SECONDS));
    						if(hourlen > 0 && minutelen > 0 && secondlen > 0)
    							// Now we allocate, and get the string into our buffer
    							int i;
    							char* hours = "";
    							char* minutes = "";
    							char* seconds = "";
    							hours = (char*)GlobalAlloc(GPTR, hourlen + minutelen + secondlen + 1);
    							GetDlgItemText(hwnd, IDC_HOURS, hours, hourlen + 1);
    							GetDlgItemText(hwnd, IDC_MINUTES, minutes, minutelen + 1);
    							GetDlgItemText(hwnd, IDC_SECONDS, seconds, secondlen + 1);
    							strcat(hours, minutes);
    							strcat(hours, seconds);
    							// Now we add the string to the list box however many times
    							// the user asked us to.
    							//int index = SendDlgItemMessage(hwnd, IDC_LIST, LB_ADDSTRING, 0, (LPARAM)buf);
    							SendDlgItemMessage(hwnd, IDC_LIST, LB_ADDSTRING, 0, (LPARAM)hours);
    							// Here we are associating the value nTimes with the item 
    							// just for the heck of it, we'll use it to display later.
    							// Normally you would put some more useful data here, such
    							// as a pointer.
    							//SendDlgItemMessage(hwnd, IDC_LIST, LB_SETITEMDATA, (WPARAM)index, (LPARAM)nTimes);
    							// Dont' forget to free the memory!
    							MessageBox(hwnd, "You didn't enter enough information!", "Warning", MB_OK);
    						MessageBox(hwnd, "Couldn't translate that number :(", "Warning", MB_OK);
    Only the hours field goes into the list.

  2. #2
    You're not actually allocating space for anything except the hours. "minutes" and "seconds" don't point to anything.

    BTW, why GlobalAlloc? These texts are going to be short? Just use new/delete, it's probably more memory efficient to let the compiler do the work.

  3. #3
    train spotter
    Join Date
    Aug 2001
    near a computer
    Just my two cents.......

    Why allocate the buffers? That is KISS (KeepItSimpleStupid)

    The memory is small. Have three smaller buffers and one bigger.

    I would use SendMessage() to set the text length in the edits with EM_LIMITTEXT to prevent the buffer overrun (on init).

    I would use the ES_NUMBER as well as sounds like only expecting numbers. Do you need error checking for the value?

    I also prefer sprintf() to strcat()

    sprintf( sLVText, "%s:%s:%s", sHours, sMinutes, sSeconds);


    "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. Combining multiple wav files into one
    By eam in forum Tech Board
    Replies: 3
    Last Post: 01-17-2005, 11:08 AM
  2. Modeless Dialogs in Multiple threads
    By MrGrieves in forum Windows Programming
    Replies: 0
    Last Post: 06-22-2004, 01:33 PM
  3. Replies: 1
    Last Post: 05-01-2003, 02:52 PM
  4. combining multiple strings into 1 string
    By Leeman_s in forum C++ Programming
    Replies: 3
    Last Post: 01-27-2003, 09:49 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM