Dialog Edit box v's WindowsText Problem

This is a discussion on Dialog Edit box v's WindowsText Problem within the Windows Programming forums, part of the Platform Specific Boards category; I have a dialog box containing 2 editbox's. I want to use the following function to save these 2 editbox's ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    4

    Dialog Edit box v's WindowsText Problem

    I have a dialog box containing 2 editbox's. I want to use the following function to save these 2 editbox's to 2 separate files.

    I have tried adding varios Dlg funtions to make it work, but nothing seems to happen.

    I found the following function in an example for a basic text editor. Can anyone suggest how I change this funtion to do the same for an editbox in a Dialog:

    Code:
    BOOL SaveFile(HWND hEdit, LPSTR pszFileName)
    {
       HANDLE hFile;
       BOOL bSuccess = FALSE;
    
       hFile = CreateFile(pszFileName, GENERIC_WRITE, 0, 0,
          CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
       if(hFile != INVALID_HANDLE_VALUE)
       {
          DWORD dwTextLength;
          dwTextLength = GetWindowTextLength(hEdit);
          if(dwTextLength > 0)// No need to bother if there's no text.
          {
             LPSTR pszText;
             pszText = (LPSTR)GlobalAlloc(GPTR, dwTextLength + 1);
             if(pszText != NULL)
             {
                if(GetWindowText(hEdit, pszText, dwTextLength + 1))
                {
                   DWORD dwWritten;
                   if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL))
                      bSuccess = TRUE;
                }
                GlobalFree(pszText);
             }
          }
          CloseHandle(hFile);
       }
       return bSuccess;
    }
    I apreciate the help...this is doing my head in!!

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Easiest way?

    Copy that function into your code.......

    before calling it;

    Code:
    HWND hTemp;
    
    //....whatever code in between 
    
    hTemp = GetDlgItem(hwnd,EDITBOX1);
    /*Where hwnd is the handle to the dialog and EDITBOX1 is the edit control ID*/
    
    SaveFile(hTemp,"My File.txt");
    Havent tested it but that should do it.....

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    4
    Thanks!

    I tried that and got an error when trying to compile about the HWND not being right.

    I think I'm calling it wrong or something. I have it in a WM_COMMAND switch:

    Code:
    switch (LOWORD(wParam)) 
    {
    	case IDSAVE:
    
    		HWND hTempPre;
    		hTempPre = GetDlgItem(hwndDlg,IDPRE);
    
    		SaveFile(hTempPre, "pre.bat");
    
    		HWND hTempPost;
    		hTempPost = GetDlgItem(hwndDlg,IDPROST);
    
    		SaveFile(hTempPost, "post.bat");
    		return 1;
    	case IDCANCEL:
    		return 1;
    	case IDCLOSE:
    		EndDialog(hwndDlg,0);
    		return 1;
    }
    break;
    I also tried it like:

    Code:
    	case IDSAVE:
    
    
    		SaveFile(GetDlgItem(hwndDlg,IDPRE), "pre.bat");
    
    
    		SaveFile(GetDlgItem(hwndDlg,IDPROST), "post.bat");
    		return 1;
    but that just does nothing....any ideas?

    Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parent of a BrowseForFolder dialog box
    By @nthony in forum Windows Programming
    Replies: 4
    Last Post: 01-08-2007, 01:54 PM
  2. Edit controls of a dialog from another dialog...
    By Snake in forum Windows Programming
    Replies: 9
    Last Post: 07-01-2005, 02:18 PM
  3. Dialog Box Problems
    By Morgul in forum Windows Programming
    Replies: 21
    Last Post: 05-31-2005, 05:48 PM
  4. How to program a "back" button with MFC
    By 99atlantic in forum Windows Programming
    Replies: 3
    Last Post: 04-26-2005, 08:34 PM
  5. Edit Control problem
    By Malek in forum Windows Programming
    Replies: 3
    Last Post: 06-16-2002, 01:12 AM

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