goody11 -2 Junior Poster

I was working on an application where I have a listbox receive info. I then want the info to save to a txt file if the save button is clicked and I want it to load if the load button is clicked. I'm not sure on how to go about doing this. Could I get mabye some example code? Here is what I have (It's not everything but it's enough):

case WM_COMMAND:
			switch(LOWORD(wParam))
			{
				case IDC_ADD:
				{

					int nTimes = 1;

						// Then we get the string they entered
						// First we need to find out how long it is so that we can
						// allocate some memory
						int len = GetWindowTextLength(GetDlgItem(hwnd, IDC_TEXT));
						int len2 = GetWindowTextLength(GetDlgItem(hwnd, IDC_OTEXT));
						if(len > 0 && len2 > 0)
						{
							// Now we allocate, and get the string into our buffer

							int i;
							char* buf;

							buf = (char*)GlobalAlloc(GPTR, len + 1);
							GetDlgItemText(hwnd, IDC_TEXT, buf, len + 1);

							// Now we add the string to the list box however many times
							// the user asked us to.

							for(i = 0;i < nTimes; i++)
							{
								int index = SendDlgItemMessage(hwnd, IDC_LIST, LB_ADDSTRING, 0, (LPARAM)buf);
								// 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!
							GlobalFree((HANDLE)buf);



							buf = (char*)GlobalAlloc(GPTR, len2 + 1);
							GetDlgItemText(hwnd, IDC_OTEXT, buf, len2 + 1);

							// Now we add the string to the list box however many times
							// the user asked us to.

							for(i = 0;i < nTimes; i++)
							{
								int index = SendDlgItemMessage(hwnd, IDC_OLIST, LB_ADDSTRING, 0, (LPARAM)buf);
								// 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_OLIST, LB_SETITEMDATA, (WPARAM)index, (LPARAM)nTimes);
							}

							// Dont' forget to free the memory!
							GlobalFree((HANDLE)buf);
						}
						else 
						{
							MessageBox(hwnd, "You didn't enter anything!", "Warning", MB_OK);
						}

				}
				break;
				case IDC_REMOVE:
				{
					// When the user clicks the Remove button, we first get the number
					// of selected items

					HWND hList = GetDlgItem(hwnd, IDC_LIST);
					HWND hOList = GetDlgItem(hwnd, IDC_OLIST);
					int count = SendMessage(hList, LB_GETSELCOUNT, 0, 0);
					int count2 = SendMessage(hOList, LB_GETSELCOUNT, 0, 0);
					if(count != LB_ERR)
					{
						if(count != 0)
						{
							// And then allocate room to store the list of selected items.

							int i;
							int *buf = GlobalAlloc(GPTR, sizeof(int) * count);
							SendMessage(hList, LB_GETSELITEMS, (WPARAM)count, (LPARAM)buf);
							
							// Now we loop through the list and remove each item that was
							// selected.  

							// WARNING!!!  
							// We loop backwards, because if we removed items
							// from top to bottom, it would change the indexes of the other
							// items!!!

							for(i = count - 1; i >= 0; i--)
							{
								SendMessage(hList, LB_DELETESTRING, (WPARAM)buf[i], 0);
							}

							GlobalFree(buf);
						}
					}
					else
					{
						MessageBox(hwnd, "Error counting items :(", "Warning", MB_OK);
					}
				
					if(count2 != LB_ERR)
					{
						if(count2 != 0)
						{
							// And then allocate room to store the list of selected items.

							int i;
							int *buf = GlobalAlloc(GPTR, sizeof(int) * count2);
							SendMessage(hOList, LB_GETSELITEMS, (WPARAM)count2, (LPARAM)buf);
							
							// Now we loop through the list and remove each item that was
							// selected.  

							// WARNING!!!  
							// We loop backwards, because if we removed items
							// from top to bottom, it would change the indexes of the other
							// items!!!

							for(i = count2 - 1; i >= 0; i--)
							{
								SendMessage(hOList, LB_DELETESTRING, (WPARAM)buf[i], 0);
							}

							GlobalFree(buf);
						}
					}
					else
					{
						MessageBox(hwnd, "Error counting items :(", "Warning", MB_OK);
					}
				
				
				}
				break;
				case IDC_CLEAR:
					if(MessageBox(hwnd, "You are about to delete all of the words. Do you wish to continue?", "Warning", MB_ICONWARNING | MB_YESNO | MB_DEFBUTTON1) == IDYES)
					{
						SendDlgItemMessage(hwnd, IDC_LIST, LB_RESETCONTENT, 0, 0);
						SendDlgItemMessage(hwnd, IDC_OLIST, LB_RESETCONTENT, 0, 0);
					}


				break;
				case IDC_SAVE:
					//Code to save the info in my 2 listboxes to a .txt.
				break;
				case IDC_LOAD:
					//Code to load the txt file that contains the 2 listboxes.
				break;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.