Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use a variable inside a _T wrapper ? (MFC Dialog app C++)

I want to generate 17 service names in the List Control. How can i use a formatted string variable inside a _T wrapper ?

// TODO: Add extra initialization here

#define MAX_VALUE 17

    int numberOfService = 0;
    CString StringServiceName;

    StringServiceName.Format(_T("Sense Counter %d"), numberOfService);

    for (numberOfService; numberOfService < MAX_VALUE; numberOfService++) {

        int nIndex = m_List.InsertItem(0, _T("")); //This variable i want to use in a _T wrapper - StringServiceName.Format(_T("Sense Counter %d"), numberOfService)

    }

    m_List.InsertColumn(0, _T("Názov služby"), LVCFMT_LEFT,150);
    m_List.InsertColumn(1, _T("Status"), LVCFMT_LEFT, 90);
    m_List.InsertColumn(2, _T(""), LVCFMT_LEFT, 90);

    int nIndex = m_List.InsertItem(0, _T("Sense Counter 1"));
    m_List.SetItemText(nIndex, 1, _T("Running"));
    m_List.SetItemText(nIndex, 2, _T("✓"));

    nIndex = m_List.InsertItem(1, _T("Sense Counter 2"));
    m_List.SetItemText(nIndex, 1, _T("Stopped"));
    m_List.SetItemText(nIndex, 2, _T("✓"));

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can’t – _T is just a macro to generate narrow or wide string constants, depending on whether you’re compiling for Unicode or not.

I’m not over-familiar with CString, but perhaps you meant this:

for (numberOfService; numberOfService < MAX_VALUE; numberOfService++) {
    StringServiceName.Format(_T("Sense Counter %d"), numberOfService)
    int nIndex = m_List.InsertItem(0, (LPCTSTR) StringServiceName);
}

(The cast may not be necessary here, although Microsoft recommends it – try without.)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading