0

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("✓"));
Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
JanČi
  • 3
  • 2

1 Answers1

0

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.)

sergiol
  • 4,122
  • 4
  • 47
  • 81
Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
  • Paul Sanders - Thank you. – JanČi Jul 12 '22 at 14:29
  • @JanČi Any time. – Paul Sanders Jul 12 '22 at 14:29
  • 1
    Better yet: Don't use `_T` (and friends) at all. Just be specific about your string types, and use `CStringW` (or `CStringA`, if you must). That doesn't change the immediate issue, but at least you can ask the *real* question now: How to convert an integer to its string representation. – IInspectable Jul 12 '22 at 14:57