I've tried like 100 different ways but I can't figure this out. I need each of these variables initialized and defined before they are used. Here is the bit of code:

int i, a;
		char etxt[100];

		HWND hwndListBox = GetDlgItem(hEdit, IDC_LIST); 

		i = SendMessage(hwndListBox, LB_GETCOUNT, 0, 0); //gets the number of items in listbox
		for (a=0; a<i; a++)
		{
			SendMessage(hwndListBox, LB_GETTEXT, a, etxt[a]); //get all text

		}
		HWND hEdit = GetDlgItem(hwnd, etxt[a]);
		SaveTextFileFromListBox(hEdit, szFileName);

Recommended Answers

All 10 Replies

What variables do you want initialized?

>>SendMessage(hwndListBox, LB_GETTEXT, a, etxt[a]);
That's not correct, the last parameter is only passing a single character, not a pointer to a character array. There is how it should be coded SendMessage(hwndListBox, LB_GETTEXT, a, etxt); If you want to keep all the strings in the list box then declare etxt as a 2d array etxt[20][255]; , which is an array of 20 strings, and each string can contain up to 255 characters. If that is what you want then you don't have to change the SendMessage() function as I previously posted.

All I meant is that I need to define each variable before it's used.
For instance, right now, the program is saying that hEdit is undeclared when I go to compile it. But if I move it up above where it's used, a won't be defined.

hEdit is probably a handle to a dialog boxl that must have been previously allocated. It must have been declared and initialized in your program somewhere. I suspect you should not be using hEdit because hEdit is probably an edit box control in that dialog box. Use the HWND that was returned by CreateWindow() (I think that's the function) when the dialog box was created.

nope just in the code I posted.

You can't just code random variable names and expect the program to work.

Well any suggestions on how I could get it to work?

If that's all you have coded then forget it because your program is missing hundreds of lines of code. See this tutorial.

No there's a whole lot more; here's the shortest part I can give to you that's compilable:

header:

//{{NO_DEPENDENCIES}}
// Microsoft Developer Studio generated include file.
// Used by ctl_one.rc
//
#define IDD_MAIN                        101
#define IDC_TEXT                        1000
#define IDC_OTEXT                       1001
#define IDC_LIST                        1002
#define IDC_OLIST                       1011
#define IDC_ADD                         1003
#define IDC_SHOWCOUNT                   1006
#define IDC_SAVE						1008

// Next default values for new objects
// 
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE        102
#define _APS_NEXT_COMMAND_VALUE         40001
#define _APS_NEXT_CONTROL_VALUE         1007
#define _APS_NEXT_SYMED_VALUE           101
#endif
#endif

resource file:

//Microsoft Developer Studio generated resource script.
//
#include "resource.h"

#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#ifndef __BORLANDC__
#include "winres.h"
#endif

/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS

/////////////////////////////////////////////////////////////////////////////
// English (U.S.) resources

#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
#ifdef _WIN32
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
#pragma code_page(1252)
#endif //_WIN32

#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//

1 TEXTINCLUDE DISCARDABLE 
BEGIN
    "resource.h\0"
END

2 TEXTINCLUDE DISCARDABLE 
BEGIN
    "#ifndef __BORLANDC__\r\n"
    "#include ""winres.h""\r\n"
    "#endif\r\n"
    "\0"
END

3 TEXTINCLUDE DISCARDABLE 
BEGIN
    "\0"
END

#endif    // APSTUDIO_INVOKED


/////////////////////////////////////////////////////////////////////////////
//
// Dialog
//

IDD_MAIN DIALOG DISCARDABLE  0, 0, 300, 302
STYLE DS_MODALFRAME | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Spanish Word Editor"
FONT 8, "MS Sans Serif"
BEGIN
    LTEXT           "Add Spanish",IDC_STATIC,7,10,56,8
    EDITTEXT        IDC_TEXT,50,7,60,14,ES_AUTOHSCROLL
    LTEXT           "Add English",IDC_STATIC,112,10,56,8    
    EDITTEXT        IDC_OTEXT,151,7,60,14,ES_AUTOHSCROLL
    LISTBOX         IDC_LIST,7,25,103,106,LBS_NOINTEGRALHEIGHT | 
                    LBS_EXTENDEDSEL | WS_VSCROLL | WS_TABSTOP
    LISTBOX         IDC_OLIST,112,25,103,106,LBS_NOINTEGRALHEIGHT | 
                    LBS_EXTENDEDSEL | WS_VSCROLL | WS_TABSTOP
    PUSHBUTTON      "&Add",IDC_ADD,220,30,50,14
    PUSHBUTTON      "&Remove",IDC_REMOVE,220,47,50,14
    PUSHBUTTON      "&Clear",IDC_CLEAR,220,63,50,14
    PUSHBUTTON      "&Save",IDC_SAVE,220,80,50,14
    PUSHBUTTON      "&Load",IDC_LOAD,220,96,50,14
END


/////////////////////////////////////////////////////////////////////////////
//
// DESIGNINFO
//

#ifdef APSTUDIO_INVOKED
GUIDELINES DESIGNINFO DISCARDABLE 
BEGIN
    IDD_MAIN, DIALOG
    BEGIN
        LEFTMARGIN, 7
        RIGHTMARGIN, 200
        VERTGUIDE, 145
        VERTGUIDE, 150
        TOPMARGIN, 7
        BOTTOMMARGIN, 149
    END
END
#endif    // APSTUDIO_INVOKED

#endif    // English (U.S.) resources
/////////////////////////////////////////////////////////////////////////////

the cpp file:

#include <windows.h>
#include "resource.h" 
#include <new>


BOOL SaveTextFileFromListBox(HWND hEdit, LPCTSTR pszFileName)
{
	HANDLE hFile;
	BOOL bSuccess = FALSE;

	hFile = CreateFile(pszFileName, GENERIC_WRITE, 0, NULL,
		CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
	    if(hFile != INVALID_HANDLE_VALUE)
    {
        DWORD dwTextLength;

        dwTextLength = GetWindowTextLength(hEdit);
        // No need to bother if there's no text.
        if(dwTextLength > 0)
        {
            char* pszText;
            DWORD dwBufferSize = dwTextLength + 1;

            try
            {
                pszText = new char[dwBufferSize];
                if(GetWindowText(hEdit,pszText, dwBufferSize))
                {
                    DWORD dwWritten;

                    if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL))
                        bSuccess = TRUE;
                }
                delete[] pszText;
            }
            catch(...)
            {
                // allocation failed
            }
		}
    }
	return bSuccess;
}
void DoFileSave(HWND hwnd)
{

	OPENFILENAME ofn;
	char szFileName[MAX_PATH] = "";

	ZeroMemory(&ofn, sizeof(ofn));

	ofn.lStructSize = sizeof(OPENFILENAME);
	ofn.hwndOwner = hwnd;
	ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
	ofn.lpstrFile = szFileName;
	ofn.nMaxFile = MAX_PATH;
	ofn.lpstrDefExt = "txt";
	ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
	
	if(GetSaveFileName(&ofn))
	{
		int i, a;
		char etxt[100];

		HWND hwndListBox = GetDlgItem(hEdit, IDC_LIST); 

		i = SendMessage(hwndListBox, LB_GETCOUNT, 0, 0); //gets the number of items in listbox
		for (a=0; a<i; a++)
		{
			SendMessage(hwndListBox, LB_GETTEXT, a, etxt[a]); //get all text

		}
		HWND hEdit = GetDlgItem(hwnd, etxt[a]);
		SaveTextFileFromListBox(hEdit, szFileName);
	}
}

and for the record I've already read that entire tutorial before.

well from what i can see you have an hEdit in the SaveTextFileFromListBox function but you are not passing it into your DoFileSave function so the program doesn't know what it is i.e. not defined. try passing it into the function and see what happens

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.