Thread: Multiline Edit Box Parser

  1. #1
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Multiline Edit Box Parser

    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:
    Code:
    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 here is the complete code thus far if needed.
    Last edited by The Brain; 10-30-2005 at 07:20 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Looking at the EM_LINELENGTH message, it looks like you might need to use EM_LINEINDEX to get the right value to use as the wparam argument.

    It's kind of confusing though, because msdn says:
    For multiline edit controls, the return value is the length, in TCHARs, of the line specified by the wParam parameter.
    But earlier it says the wParam parameter "Specifies the character index of a character in the line whose length is to be retrieved"
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Here is the solution to my problem..

    thanks anonytmouse
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  4. #4
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Have this function to read in user input from my edit box.. but it sometimes only reads in like 80-90% of a line.. what am I doing wrong..??!?


    Code:
    void EditBoxFileParser(HWND hwnd, HWND hEdit)
    {
         
         int iCount; 
         WORD iLength;          
         TCHAR **Lines;
         
         //Get Number of Lines in Edit Field
         iCount = SendMessage(hEdit, EM_GETLINECOUNT, 0, 0);
         
         //If Editbox is empty, exit function
         if(!iCount)
         
              return;
         
         Lines = new TCHAR*[iCount];
         
         //Populate 2D array, Lines[LineIndex][LineText]
         for(int i=0; i<iCount; i++)
         {     
              iLength = (WORD)SendMessage(hEdit, EM_LINELENGTH, i, 0);
              Lines[i] = new TCHAR[iLength+sizeof(WORD)+1];
              CopyMemory(Lines[i], &iLength, sizeof(WORD));
              SendMessage(hEdit, EM_GETLINE, i, (LPARAM)Lines[i]);
              Lines[i][iLength] = '\0';
         }   
         
        //Visually Verify Contents of the Lines[][] array
        HDC hdc;
        HFONT hFont;
        PAINTSTRUCT ps;
        RECT rect;
        
        rect.left = 250;
        rect.top  = 250;
        rect.right  = 600;
        rect.bottom = 500;   
        
        InvalidateRect(hwnd, &rect, TRUE);
        
        hdc = BeginPaint(hwnd, &ps);        
        hFont = (HFONT)GetStockObject(SYSTEM_FONT);  
               
         SelectObject(hdc, hFont);     
         
         for(int i=0, x=300, y=275; i<iCount; i++)
              
                 TextOut(hdc, x, y+=20, Lines[i], lstrlen(Lines[i]));
                 
         DeleteObject(hFont);
         EndPaint(hwnd, &ps);              
    }
    Last edited by The Brain; 11-01-2005 at 03:39 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    As mentioned by Jawib, you are not using EM_LINELENGTH correctly. The wParam argument specifies a character index rather than a line number. You can retrieve a character index for a given line with the EM_LINEINDEX message.
    Quote Originally Posted by MSDN EM_LINELENGTH
    wParam: Specifies the character index of a character in the line whose length is to be retrieved.

    Use the EM_LINEINDEX message to retrieve a character index for a given line number within a multiline edit control.

  6. #6
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    you guys are awesome...

    my problem was, "well.. i tried using both EM_LINELENGTH.. then switched it with EM_LINEINDEX.. no dice.." so i figured i must be missing something.. read over msdn like 10 times.. busted out charles petzold page #400.. thought about it awhile.. then it hit me like my ex g/f.. I have to use both together...

    Code:
    void EditBoxFileParser(HWND hwnd, HWND hEdit)
    {
         
         int iCount; 
         WORD iLength, iOffset;        
         TCHAR **Lines;
         
         //Get Number of Lines in Edit Field
         iCount = SendMessage(hEdit, EM_GETLINECOUNT, 0, 0);
         
         //If Editbox is empty, exit function
         if(!iCount)
         
              return;
         
         Lines = new TCHAR*[iCount];
         
         //Populate 2D array, Lines[LineIndex][LineText]
         for(int i=0; i<iCount; i++)
         {    iOffset = SendMessage(hEdit, EM_LINEINDEX, i, 0);
              iLength = (WORD)SendMessage(hEdit, EM_LINELENGTH, iOffset, 0);
              Lines[i] = new TCHAR[iLength+sizeof(WORD)+1];
              CopyMemory(Lines[i], &iLength, sizeof(WORD));          
              SendMessage(hEdit, EM_GETLINE, i, (LPARAM)Lines[i]);
              Lines[i][iLength] = '\0';
         }
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  7. #7
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Good to see my advice wasn't completely off-base (even though it didn't help your original problem )
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 02-13-2008, 02:59 PM
  2. edit box
    By beene in forum Windows Programming
    Replies: 3
    Last Post: 11-11-2006, 04:40 AM
  3. How to program a "back" button with MFC
    By 99atlantic in forum Windows Programming
    Replies: 3
    Last Post: 04-26-2005, 08:34 PM
  4. display a file in dropdown edit box
    By sunburnbyRA in forum Windows Programming
    Replies: 2
    Last Post: 03-10-2004, 01:58 PM
  5. Edit box question
    By learning110 in forum Windows Programming
    Replies: 6
    Last Post: 03-28-2003, 08:16 PM