locked
How to add a newline in CString?

    Question

  • I have three integers - int t_red,t_green,t_blue;

    I need a CString variable with value as = "t_red \n t_green \n t_blue";

    ---

    CString t;
    t.Format(_T("%d \r\n %d \r\n %d "), t_red,t_green,t_blue);
    GetDlgItem(IDC_TEXT)->SetWindowText(t);

    ---

    But this is not working.

    Any suggestions?

    Thursday, July 21, 2011 7:55 PM

Answers

  • On 7/27/2011 5:25 PM, Ankit JC wrote:

    I am using a static control..

         CONTROL         "-",IDC_TEXT,"Static",SS_SIMPLE | WS_BORDER,36,34,19,16

    I want to do the following 2 things -

    1) Make it multiline.

    Drop SS_SIMPLE style.

    2) Change the font of it.

    CWnd::SetFont


    Igor Tandetnik

    • Marked as answer by Ankit JC Wednesday, July 27, 2011 10:31 PM
    Wednesday, July 27, 2011 9:29 PM
  • I guess SS_SIMPLE is not a correct style for a static text if you want to display multiple lines.

    If you use the resource editor, right click on the text, select the menu "Properties", and set the "Simple" property to false.

    Or you can modify the RC file directly and remove SS_SIMPLE style :

       CONTROL   "-",IDC_TEXT,"Static",WS_BORDER,36,34,100,30
    

     

    Also, make sure the text control is large enough to display multiple lines, so the text is not clipped.

     

     

     



    • Marked as answer by Ankit JC Wednesday, July 27, 2011 10:31 PM
    Wednesday, July 27, 2011 9:45 PM
  • In OnInitDialog :

     

    	m_pFont = new CFont();
    	m_pFont->CreatePointFont(200, _T("Arial"));
    	GetDlgItem(IDC_TEXT)->SetFont(m_pFont, TRUE);
    
    

    Declare a CFont * member variable in your dialog class, and delete the font object in the dialog box destructor.

     

    There are other more complicated CreateFont methods that let you specify more font style (bold, underline...)

    • Marked as answer by Ankit JC Thursday, July 28, 2011 12:03 AM
    Wednesday, July 27, 2011 10:42 PM

All replies

  • On 7/21/2011 3:55 PM, Ankit JC wrote:

    CString t;
    t.Format(_T("%d \r\n %d \r\n %d "), t_red,t_green,t_blue);
    GetDlgItem(IDC_TEXT)->SetWindowText(t);

    ---

    But this is not working.

    Not working in what way? What happens, and how does it differ from your expectations?


    Igor Tandetnik

    Thursday, July 21, 2011 8:06 PM
  • I have three integers - int t_red,t_green,t_blue;

    I need a CString variable with value as = "t_red \n t_green \n t_blue";

    In native C++ you'd need to have an equivalent string declared to
    lookup a cosmetic name for the enumeration value.

    C# and other such languages do it behind the scene - but obviously
    carry the baggage as an overhead if you never make use of it.

    Dave

    Thursday, July 21, 2011 8:41 PM
  • @ Igor ..

    Instead of printing the values in newline it prints in the same line and the "\r\n" is replaced by a garbage character (like a box).

     

    @ David ..

    yes.. I just wrote it down to explain what i want the output to be.

    What i meant writing by that is, I would like to see the values displayed one below the other..

    for eg.

    255

    128

    255

     

     

    Thursday, July 21, 2011 9:18 PM
  • Hi,

    Does your text control support multi-line? If not it will not work as you expected.

    RenJie

    Thursday, July 21, 2011 9:29 PM
  • Hey RenJie..

    I am sorry, but i donno what this mean..
    It sounds important.. will you please tell me in detail?

    Thursday, July 21, 2011 9:36 PM
  • In the resource editor open the Properties of the edit control. Turn on the Multiline property.

     

    Thursday, July 21, 2011 10:41 PM
  • Hello..

    Do you mean Resource view window?

    I do not see ay edit control over there.

    Plz excuse me i am pretty new to VS2010.

    Friday, July 22, 2011 12:57 AM
  • Use the resource view window to open the dialog template, so you can see the dialog's controls. For each control you can display and edit its properties.

     

    Friday, July 22, 2011 2:30 AM
  • Hello..

    I am using a static control..

     

    IDD_DIALOG1 DIALOGEX 0, 0, 339, 284
    STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
    CAPTION "Left MacroBlock Information"
    FONT 10, "MS Shell Dlg", 0, 0, 0x0
    BEGIN
        LTEXT           "LEFT MACRO BLOCK",IDC_STATIC,38,9,119,8,SS_NOPREFIX
        LTEXT           "ABC",IDC_STATIC,42,21,282,9
        CTEXT           "0",IDC_STATIC,24,37,8,8

        CONTROL         "-",IDC_TEXT,"Static",SS_SIMPLE | WS_BORDER,36,34,19,16
        CONTROL         "-",IDC_TEXT3,"Static",SS_SIMPLE | WS_BORDER,72,34,19,16

    END

     

    Now I am setting the text through program as-

    CString t;
    t.Format(_T("%d \r\n %d \r\n %d "), t_red,t_green,t_blue);
    GetDlgItem(IDC_TEXT)->SetWindowText(t);

    -----

    I want to do the following 2 things -

    1) Make it multiline.

    2) Change the font of it.

    Help required.

    Wednesday, July 27, 2011 9:25 PM
  • On 7/27/2011 5:25 PM, Ankit JC wrote:

    I am using a static control..

         CONTROL         "-",IDC_TEXT,"Static",SS_SIMPLE | WS_BORDER,36,34,19,16

    I want to do the following 2 things -

    1) Make it multiline.

    Drop SS_SIMPLE style.

    2) Change the font of it.

    CWnd::SetFont


    Igor Tandetnik

    • Marked as answer by Ankit JC Wednesday, July 27, 2011 10:31 PM
    Wednesday, July 27, 2011 9:29 PM
  • I guess SS_SIMPLE is not a correct style for a static text if you want to display multiple lines.

    If you use the resource editor, right click on the text, select the menu "Properties", and set the "Simple" property to false.

    Or you can modify the RC file directly and remove SS_SIMPLE style :

       CONTROL   "-",IDC_TEXT,"Static",WS_BORDER,36,34,100,30
    

     

    Also, make sure the text control is large enough to display multiple lines, so the text is not clipped.

     

     

     



    • Marked as answer by Ankit JC Wednesday, July 27, 2011 10:31 PM
    Wednesday, July 27, 2011 9:45 PM
  • 1) Thanks!! That does it. :) Cant tell you how happy i am!

    2) How and where to use this SetFont.. I read a lot about it.. but just got more confused!
    Wednesday, July 27, 2011 10:29 PM
  • Thanks buddy! That solved my problem!
    Wednesday, July 27, 2011 10:30 PM
  • In OnInitDialog :

     

    	m_pFont = new CFont();
    	m_pFont->CreatePointFont(200, _T("Arial"));
    	GetDlgItem(IDC_TEXT)->SetFont(m_pFont, TRUE);
    
    

    Declare a CFont * member variable in your dialog class, and delete the font object in the dialog box destructor.

     

    There are other more complicated CreateFont methods that let you specify more font style (bold, underline...)

    • Marked as answer by Ankit JC Thursday, July 28, 2011 12:03 AM
    Wednesday, July 27, 2011 10:42 PM