Clinton Portis 211 Practically a Posting Shark

Trying my hand at extracting user entered data from a multiline edit box... my strategy is to first, get the number of total lines from the edit box.. and the length of each line from the edit box.. and create a dynamic 2D array that is NULL terminated at the second dimension.. then read in the edit box line at a time..

Came up with a little TextOut( ) loop just as a method to visually verify if my 2d array was loaded correctly.. and made a WM_COMMAND case to respond to the, "Add Count" pushbutton and call the EditBoxFileParser( ) function... which should load and display the contents of the Lines[][] 2d array.

Please take a look when ye' get a chance.. at runtime, the user should be able to enter stuff in the edit box.. then click the, "Add Count" pushbutton.. and a display of the edit box conents should appear somewhere off to the right side of the screen..

At this point, nothing happens when I enter stuff in the edit box and click the "add count" button..


Here is my editbox file parser function:

void EditBoxFileParser(HWND hwnd, HWND hEdit)
{
     
     int iCount, iLength;
     TCHAR **Lines;
     
     //Get Number of Lines in Edit Field
     iCount = SendMessage(hEdit, EM_GETLINECOUNT, 0, 0);
     
     Lines = new TCHAR*[iCount];
     
     //Populate 2D array - Lines[LineIndex][LineText]
     for(int i=0; i<iCount; i++)
     {     
          iLength = SendMessage(hEdit, EM_LINELENGTH, i, 0);
          Lines[i] = new TCHAR[iLength+1];
          SendMessage(hEdit, EM_GETLINE, i, (LPARAM)Lines[i]);
          Lines[i][iLength+1] = '\0';
     }
     

     //Visually verify the Lines[][] 2D array
     HDC hdc;
     PAINTSTRUCT ps;          
     HFONT hFont;
     
     hdc = BeginPaint(hwnd, &ps);
     hFont = (HFONT)GetStockObject(SYSTEM_FONT);
     SelectObject(hdc, hFont);
     
     for(int i=0, x=200, y=200; i<iCount; i++)
          
             TextOut(hdc, x, y+=10, Lines[i], lstrlen(Lines[i]));
             
     DeleteObject(hFont);
     EndPaint(hwnd, &ps);
     
}

And is the complete code thus far if needed.

Dani AI

Generated

For : the approach is close, but a few concrete Win32 details stop the routine from returning the expected text. The main problems are (1) EM_LINELENGTH is being called with a line number instead of a character index (use EM_LINEINDEX first), (2) EM_GETLINE requires the first WORD of the destination buffer to contain the buffer size in characters, (3) an off‑by‑one when terminating the buffer (Lines[i][iLength+1] overruns — use [iLength]), (4) BeginPaint/EndPaint must only be used in WM_PAINT (use GetDC/ReleaseDC or update a control), and (5) do not call DeleteObject on a stock font. Also free allocated buffers or, better, use std::vector/std::basic_string to avoid leaks.

A corrected pattern using EM_LINEINDEX/EM_LINELENGTH/EM_GETLINE and safe temporary storage:

void EditBoxFileParser(HWND hwnd, HWND hEdit)
{
    int lineCount = (int)SendMessage(hEdit, EM_GETLINECOUNT, 0, 0);
    if (lineCount <= 0) return;

    std::vector<std::basic_string<TCHAR>> lines;
    for (int i = 0; i < lineCount; ++i) {
        int startChar = (int)SendMessage(hEdit, EM_LINEINDEX, i, 0);
        int len = (int)SendMessage(hEdit, EM_LINELENGTH, startChar, 0);

        std::vector<TCHAR> buf(len + 1);
        ((LPWORD)buf.data())[0] = (WORD)(len + 1);   // required by EM_GETLINE
        SendMessage(hEdit, EM_GETLINE, (WPARAM)i, (LPARAM)buf.data());
        buf[len] = TEXT('\0');

        lines.emplace_back(buf.data());
    }

    HDC hdc = GetDC(hwnd);
    HFONT old = (HFONT)SelectObject(hdc, GetStockObject(SYSTEM_FONT));
    int y = 200;
    for (const auto &s : lines)
        TextOut(hdc, 200, y += 16, s.c_str(), (int)s.length());
    SelectObject(hdc, old);
    ReleaseDC(hwnd, hdc);
}

Alternatively, call GetWindowText/GetWindowTextLength to pull the entire edit buffer and split on CR/LF — often simpler and avoids EM_GETLINE quirks. For display, consider updating a read‑only multiline control or invalidating and letting WM_PAINT render, rather than calling BeginPaint from WM_COMMAND.

Quick checklist: confirm the handle hEdit is the multiline control (ES_MULTILINE), verify the button’s WM_COMMAND is reached, output iCount/len via OutputDebugString or MessageBox to confirm values, and always restore selected GDI objects and free memory (or use RAII containers).

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.