Uys of Spades
Uys of Spades

Reputation: 93

How is this variable not defined?

Here is a snippet to my code currently. This makes absolutely no sense. It says that hEditEW under the CASE COMMAND is not defined where it is explicitly defined above. This is probably a really simple fix, but I have having one heck of a time solving it.

case WM_CREATE:
         {
                  HWND hWndButton = CreateWindowEx(NULL, "Button", "Click to Convert", WS_TABSTOP|WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
                      200, 370, 150, 50, hWnd, (HMENU)IDC_BUTTON_ONE, NULL, NULL);

                  HWND hEditEW = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "", WS_CHILD|ES_MULTILINE| WS_VISIBLE |
                      ES_AUTOHSCROLL | ES_AUTOVSCROLL, 25, 50, 220, 300, hWnd, (HMENU)IDC_EDITOR_BOX_EAST_WEST, NULL, NULL);

                  HWND hEditNS = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "", WS_CHILD | ES_MULTILINE | WS_VISIBLE |
                      ES_AUTOHSCROLL | ES_AUTOVSCROLL, 300, 50, 220, 300, hWnd, (HMENU)IDC_EDITOR_BOX_EAST_WEST, NULL, NULL);
         }
    break;

case WM_COMMAND:
       {
                   switch (LOWORD(wParam))
                   {
                   case IDC_BUTTON_ONE:
                   {
                                          LPWSTR buffer[256];
                                          SendMessage(hEditEW,
                                              WM_GETTEXT,
                                              sizeof(buffer) / sizeof(buffer[0]),
                                              reinterpret_cast<LPARAM>(buffer));
                                          HANDLE hFile = CreateFile("C:\\test.txt", GENERIC_READ,
                                              0, NULL, CREATE_NEW, FILE_FLAG_OVERLAPPED, NULL); 

                   }

                       break;
                   }
                   break;
      }

Upvotes: 0

Views: 237

Answers (3)

yizzlez
yizzlez

Reputation: 8805

I don't know WinApi, but hEditEW went out of scope:

case WM_CREATE:
{
     HWND hEditEW = ....
} //end of scope

Simple fix: put hEditEW in an upper scope:

HWND hEditEW;
case WM_CREATE:
{ .....

Upvotes: 0

knowndead
knowndead

Reputation: 80

hEditEW is local put HWND hEditNS; on the top and replace

WND hEditNS = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "", WS_CHILD | ES_MULTILINE | WS_VISIBLE |
                      ES_AUTOHSCROLL | ES_AUTOVSCROLL, 300, 50, 220, 300, hWnd, (HMENU)IDC_EDITOR_BOX_EAST_WEST, NULL, NULL);

with

hEditNS = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "", WS_CHILD | ES_MULTILINE | WS_VISIBLE |
                      ES_AUTOHSCROLL | ES_AUTOVSCROLL, 300, 50, 220, 300, hWnd, (HMENU)IDC_EDITOR_BOX_EAST_WEST, NULL, NULL);

remove WND everywhere that should fix it if you put HWND hEditNS; at the top of the class/function

Upvotes: 0

Luchian Grigore
Luchian Grigore

Reputation: 258608

You define it in the scope after case WM_CREATE and attempt to use it in the scope after case WM_COMMAND. That won't work - different scopes.

You don't even need a switch to demonstrate the problem:

{
   HWND hEditEW = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "", WS_CHILD|ES_MULTILINE| WS_VISIBLE |
                      ES_AUTOHSCROLL | ES_AUTOVSCROLL, 25, 50, 220, 300, hWnd, (HMENU)IDC_EDITOR_BOX_EAST_WEST, NULL, NULL);

}  //in scope up to here
{
   SendMessage(hEditEW,
      WM_GETTEXT,
      sizeof(buffer) / sizeof(buffer[0]),
      reinterpret_cast<LPARAM>(buffer));
}

Upvotes: 5

Related Questions