Thread: displaying text and numbers in an edit control

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    11

    displaying text and numbers in an edit control

    Hi folks

    I'm still learning so this is probably a dumb question.

    I know how to use SetDlgItemText() and SetDlgItemInt() to display information in an edit control.....

    but what I want to do is append information to that already there - for instance I have an array of integers and I want to display them in a formatted manner (with comma's, spaces. tabs etc)

    Which API functions do I need to use to do this? Is there an easy way of finding out which function you need to do a job when you really don't know exactly what you're looking for (to save me asking you lot all the time)

    Thanks in advance
    dicky

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    If you want an API function there's wsprintf or you could just use sprintf to format a string prior to displaying it in your edit control.

    To append information in a single line edit control it's probably simpler to just produce the final, formatted string and send that to the edit control with one of the api functions you've mentioned. For multi-line edit controls it may be better to use EM_REPLACESEL (set the selection to the end of the edit control's contents with EM_SETSEL).
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Another solution is to a CString object to the edit control via data exchange. Make any change to the CString object and update the edit control via UpdateData().

    Kuphryn

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    11
    Ken
    Thanks for the info - it's a multiline edit control soyour advice may seem the most appropriate

    About EM_SETSEL it says this

    wParam
    Specifies the starting character position of the selection.
    lParam
    Specifies the ending character position of the selection.

    But I'm not sure how those positions are specified, are they in the form of line number and char position co-ordinates, or something like that?

    dicky

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    >> But I'm not sure how those positions are specified, are they in the form of line number and char position co-ordinates, <<
    The starting and ending character positions.
    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <stdarg.h>
    #include <tchar.h>
     
    /* 
    * Sample: AppendText(hwndEdit, "The count is %d\r\n", 23);
    * Note: Use "\r\n" instead of just "\n" to start a new line.
    */
    void AppendText(HWND hwnd, LPCTSTR szFormat, ...)
    {
    	TCHAR szText[4096];
    	va_list args;
    	int nTxtLen;
     
    	/* Move selection to end of text */
    	nTxtLen = GetWindowTextLength(hwnd);
    	SendMessage(hwnd, EM_SETSEL, nTxtLen, nTxtLen);
     
    	/* Format the text into the buffer */
    	va_start(args, szFormat);
    	_vsntprintf(szText, sizeof(szText) / sizeof(szText[0]), szFormat, args);
    	szText[(sizeof(szText) / sizeof(szText[0])) - 1] = TEXT('\0');
    	va_end(args);
     
    	/* Add the text line and scroll it into view */
    	SendMessage(hwnd, EM_REPLACESEL, FALSE, (LPARAM) szText);
    	SendMessage(hwnd, EM_SCROLLCARET, 0, 0);
    }
    Last edited by anonytmouse; 06-05-2004 at 08:15 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Automatic Rich Text Edit
    By Queatrix in forum Windows Programming
    Replies: 3
    Last Post: 12-27-2006, 02:18 PM
  2. Comparing numbers to a list of numbers held in a text file
    By jmajeremy in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2006, 07:56 AM
  3. Help displaying line numbers of a text document
    By Japatron in forum C Programming
    Replies: 4
    Last Post: 05-04-2006, 01:34 PM
  4. How to copy a word or numbers from text to other location
    By trancedeejay in forum C Programming
    Replies: 12
    Last Post: 02-09-2006, 06:43 AM
  5. Displaying an array value as text in windows
    By drb2k2 in forum C++ Programming
    Replies: 2
    Last Post: 04-08-2003, 04:26 AM