Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Change from Cstring to double in listview vc++

Change from Cstring to double in listview vc++

Scheduled Pinned Locked Moved C / C++ / MFC
c++helptutorial
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lolici
    wrote on last edited by
    #1

    Hello everyone! I've used a list control to display data and an edit box to edit them. So I used this function:

    void CDataDialog::SetCell(HWND hWnd1,
    CString value, int nRow, int nCol)

    I want to make this change:

    void CDataDialog::SetCell(HWND hWnd1,
    double value, int nRow, int nCol)

    but I have problem with other functions such as GetDlgItemText which I don't know how to change. Here is the code:

    void CDataDialog::SetCell(HWND hWnd1,
    double value, int nRow, int nCol)
    {
    TCHAR szString[256];
    swprintf(szString, value, 0);

    //Fill the LVITEM structure with the 
    //values given as parameters.
    LVITEM lvItem;
    lvItem.mask = LVIF\_TEXT;
    lvItem.iItem = nRow;
    lvItem.pszText = szString;
    lvItem.iSubItem = nCol;
    if (nCol >0)
    	//set the value of listItem
    	::SendMessage(hWnd1, LVM\_SETITEM,
    	(WPARAM)0, (WPARAM)&lvItem);
    else
    	//Insert the value into List
    	ListView\_InsertItem(hWnd1, &lvItem);
    

    }

    CString CDataDialog::GetItemText(
    HWND hWnd, int nItem, int nSubItem) const
    {
    LVITEM lvi;
    memset(&lvi, 0, sizeof(LVITEM));
    lvi.iSubItem = nSubItem;
    CString str;
    int nLen = 128;
    int nRes;
    do
    {
    nLen *= 2;
    lvi.cchTextMax = nLen;
    lvi.pszText = str.GetBufferSetLength(nLen);
    nRes = (int)::SendMessage(hWnd,
    LVM_GETITEMTEXT, (WPARAM)nItem,
    (LPARAM)&lvi);
    } while (nRes == nLen - 1);
    str.ReleaseBuffer();
    return str;
    }

    void CDataDialog::OnClickList1(NMHDR *pNMHDR, LRESULT *pResult)
    {
    //LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast(pNMHDR);
    // TODO: Add your control notification handler code here
    //*pResult = 0;
    Invalidate();
    HWND hWnd1 = ::GetDlgItem(m_hWnd, IDC_LIST1);
    LPNMITEMACTIVATE temp = (LPNMITEMACTIVATE)pNMHDR;
    RECT rect;
    //get the row number
    nItem = temp->iItem;
    //get the column number
    nSubItem = temp->iSubItem;
    if (nSubItem == 0 || nSubItem == -1 || nItem == -1)
    return;
    //Retrieve the text of the selected subItem
    //from the list
    CString str = GetItemText(hWnd1, nItem,
    nSubItem);

    RECT rect1, rect2;
    // this macro is used to retrieve the Rectanle 
    // of the selected SubItem
    ListView\_GetSubItemRect(hWnd1, temp->iItem,
    	temp->iSubItem, LVIR\_BOUNDS, &rect);
    //Get the Rectange of the listControl
    ::GetWindowRect(temp->hdr.hwndFrom, &rect1);
    //Get the Rectange of the Dialog
    ::GetWindowRect(m\_hWnd, &rect2);
    
    int x = rect1.left - rect2.left;
    int y = rect1.top - rect2.top;
    
    if (nItem != -1)
    	::SetWindowPos(::GetD
    
    J V 2 Replies Last reply
    0
    • L lolici

      Hello everyone! I've used a list control to display data and an edit box to edit them. So I used this function:

      void CDataDialog::SetCell(HWND hWnd1,
      CString value, int nRow, int nCol)

      I want to make this change:

      void CDataDialog::SetCell(HWND hWnd1,
      double value, int nRow, int nCol)

      but I have problem with other functions such as GetDlgItemText which I don't know how to change. Here is the code:

      void CDataDialog::SetCell(HWND hWnd1,
      double value, int nRow, int nCol)
      {
      TCHAR szString[256];
      swprintf(szString, value, 0);

      //Fill the LVITEM structure with the 
      //values given as parameters.
      LVITEM lvItem;
      lvItem.mask = LVIF\_TEXT;
      lvItem.iItem = nRow;
      lvItem.pszText = szString;
      lvItem.iSubItem = nCol;
      if (nCol >0)
      	//set the value of listItem
      	::SendMessage(hWnd1, LVM\_SETITEM,
      	(WPARAM)0, (WPARAM)&lvItem);
      else
      	//Insert the value into List
      	ListView\_InsertItem(hWnd1, &lvItem);
      

      }

      CString CDataDialog::GetItemText(
      HWND hWnd, int nItem, int nSubItem) const
      {
      LVITEM lvi;
      memset(&lvi, 0, sizeof(LVITEM));
      lvi.iSubItem = nSubItem;
      CString str;
      int nLen = 128;
      int nRes;
      do
      {
      nLen *= 2;
      lvi.cchTextMax = nLen;
      lvi.pszText = str.GetBufferSetLength(nLen);
      nRes = (int)::SendMessage(hWnd,
      LVM_GETITEMTEXT, (WPARAM)nItem,
      (LPARAM)&lvi);
      } while (nRes == nLen - 1);
      str.ReleaseBuffer();
      return str;
      }

      void CDataDialog::OnClickList1(NMHDR *pNMHDR, LRESULT *pResult)
      {
      //LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast(pNMHDR);
      // TODO: Add your control notification handler code here
      //*pResult = 0;
      Invalidate();
      HWND hWnd1 = ::GetDlgItem(m_hWnd, IDC_LIST1);
      LPNMITEMACTIVATE temp = (LPNMITEMACTIVATE)pNMHDR;
      RECT rect;
      //get the row number
      nItem = temp->iItem;
      //get the column number
      nSubItem = temp->iSubItem;
      if (nSubItem == 0 || nSubItem == -1 || nItem == -1)
      return;
      //Retrieve the text of the selected subItem
      //from the list
      CString str = GetItemText(hWnd1, nItem,
      nSubItem);

      RECT rect1, rect2;
      // this macro is used to retrieve the Rectanle 
      // of the selected SubItem
      ListView\_GetSubItemRect(hWnd1, temp->iItem,
      	temp->iSubItem, LVIR\_BOUNDS, &rect);
      //Get the Rectange of the listControl
      ::GetWindowRect(temp->hdr.hwndFrom, &rect1);
      //Get the Rectange of the Dialog
      ::GetWindowRect(m\_hWnd, &rect2);
      
      int x = rect1.left - rect2.left;
      int y = rect1.top - rect2.top;
      
      if (nItem != -1)
      	::SetWindowPos(::GetD
      
      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #2

      A list control can not store double values without additional handling. To set the text of an item to a floating point value it must be converted. Read the documentation of the used sprintf, _sprintf_l, swprintf, _swprintf_l, __swprintf_l[^] function. So you have to use something like

      WCHAR szString[256];
      swprintf(szString, sizeof(szString) / sizeof(WCHAR), L"%G", value);

      But it would be better to use _snprintf_s, _snprintf_s_l, _snwprintf_s, _snwprintf_s_l[^] here:

      TCHAR szString[256];
      _sntprintf_t(szString, sizeof(szString) / sizeof(TCHAR), _T("%G"), value);

      To get a double value back when reading the cell text use _tstof (atof, _atof_l, _wtof, _wtof_l[^]):

      // double CDataDialog::GetItemText(HWND hWnd, int nItem, int nSubItem) const
      double CDataDialog::GetItemDouble(HWND hWnd, int nItem, int nSubItem) const
      {
      CString str = GetItemText(hWnd, nItem, nSubItem);
      return _tstof(str);
      }

      L 1 Reply Last reply
      0
      • J Jochen Arndt

        A list control can not store double values without additional handling. To set the text of an item to a floating point value it must be converted. Read the documentation of the used sprintf, _sprintf_l, swprintf, _swprintf_l, __swprintf_l[^] function. So you have to use something like

        WCHAR szString[256];
        swprintf(szString, sizeof(szString) / sizeof(WCHAR), L"%G", value);

        But it would be better to use _snprintf_s, _snprintf_s_l, _snwprintf_s, _snwprintf_s_l[^] here:

        TCHAR szString[256];
        _sntprintf_t(szString, sizeof(szString) / sizeof(TCHAR), _T("%G"), value);

        To get a double value back when reading the cell text use _tstof (atof, _atof_l, _wtof, _wtof_l[^]):

        // double CDataDialog::GetItemText(HWND hWnd, int nItem, int nSubItem) const
        double CDataDialog::GetItemDouble(HWND hWnd, int nItem, int nSubItem) const
        {
        CString str = GetItemText(hWnd, nItem, nSubItem);
        return _tstof(str);
        }

        L Offline
        L Offline
        lolici
        wrote on last edited by
        #3

        I did it but I got this: "no suitable constructor exists from double to ATL::CStringT wchar_t StrTraitMFC__DLL>>" on

        CString str = GetItemText(hWnd, nItem, nSubItem);

        If I don't turn data from string to double could I "pass" them in a text file after edit them on the list view? :)

        J 1 Reply Last reply
        0
        • L lolici

          I did it but I got this: "no suitable constructor exists from double to ATL::CStringT wchar_t StrTraitMFC__DLL>>" on

          CString str = GetItemText(hWnd, nItem, nSubItem);

          If I don't turn data from string to double could I "pass" them in a text file after edit them on the list view? :)

          J Offline
          J Offline
          Jochen Arndt
          wrote on last edited by
          #4

          Sorry, forget to rename it during copy & paste. Should be named GetItemDouble:.

          double CDataDialog::GetItemDouble(HWND hWnd, int nItem, int nSubItem) const
          {
          CString str = GetItemText(hWnd, nItem, nSubItem);
          return _tstof(str);
          }

          L 1 Reply Last reply
          0
          • J Jochen Arndt

            Sorry, forget to rename it during copy & paste. Should be named GetItemDouble:.

            double CDataDialog::GetItemDouble(HWND hWnd, int nItem, int nSubItem) const
            {
            CString str = GetItemText(hWnd, nItem, nSubItem);
            return _tstof(str);
            }

            L Offline
            L Offline
            lolici
            wrote on last edited by
            #5

            Thank you very much Sir!It was compiled fine!! I got a message about "Unhandled exception" when tried to write on second dialog but I must have made a mistake on code.Could I save editable data from list view on text file without converting them from string type?

            J 1 Reply Last reply
            0
            • L lolici

              Thank you very much Sir!It was compiled fine!! I got a message about "Unhandled exception" when tried to write on second dialog but I must have made a mistake on code.Could I save editable data from list view on text file without converting them from string type?

              J Offline
              J Offline
              Jochen Arndt
              wrote on last edited by
              #6

              Use the debugger to find out when the exception occurs and check the code lines that has been executed before. You can write strings directly to text files when using appropriate file functions. But those will be then Unicode text files (when having a Unicode build) and the file class does no perform implicit conversion to ANSI text.

              L 1 Reply Last reply
              0
              • L lolici

                Hello everyone! I've used a list control to display data and an edit box to edit them. So I used this function:

                void CDataDialog::SetCell(HWND hWnd1,
                CString value, int nRow, int nCol)

                I want to make this change:

                void CDataDialog::SetCell(HWND hWnd1,
                double value, int nRow, int nCol)

                but I have problem with other functions such as GetDlgItemText which I don't know how to change. Here is the code:

                void CDataDialog::SetCell(HWND hWnd1,
                double value, int nRow, int nCol)
                {
                TCHAR szString[256];
                swprintf(szString, value, 0);

                //Fill the LVITEM structure with the 
                //values given as parameters.
                LVITEM lvItem;
                lvItem.mask = LVIF\_TEXT;
                lvItem.iItem = nRow;
                lvItem.pszText = szString;
                lvItem.iSubItem = nCol;
                if (nCol >0)
                	//set the value of listItem
                	::SendMessage(hWnd1, LVM\_SETITEM,
                	(WPARAM)0, (WPARAM)&lvItem);
                else
                	//Insert the value into List
                	ListView\_InsertItem(hWnd1, &lvItem);
                

                }

                CString CDataDialog::GetItemText(
                HWND hWnd, int nItem, int nSubItem) const
                {
                LVITEM lvi;
                memset(&lvi, 0, sizeof(LVITEM));
                lvi.iSubItem = nSubItem;
                CString str;
                int nLen = 128;
                int nRes;
                do
                {
                nLen *= 2;
                lvi.cchTextMax = nLen;
                lvi.pszText = str.GetBufferSetLength(nLen);
                nRes = (int)::SendMessage(hWnd,
                LVM_GETITEMTEXT, (WPARAM)nItem,
                (LPARAM)&lvi);
                } while (nRes == nLen - 1);
                str.ReleaseBuffer();
                return str;
                }

                void CDataDialog::OnClickList1(NMHDR *pNMHDR, LRESULT *pResult)
                {
                //LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast(pNMHDR);
                // TODO: Add your control notification handler code here
                //*pResult = 0;
                Invalidate();
                HWND hWnd1 = ::GetDlgItem(m_hWnd, IDC_LIST1);
                LPNMITEMACTIVATE temp = (LPNMITEMACTIVATE)pNMHDR;
                RECT rect;
                //get the row number
                nItem = temp->iItem;
                //get the column number
                nSubItem = temp->iSubItem;
                if (nSubItem == 0 || nSubItem == -1 || nItem == -1)
                return;
                //Retrieve the text of the selected subItem
                //from the list
                CString str = GetItemText(hWnd1, nItem,
                nSubItem);

                RECT rect1, rect2;
                // this macro is used to retrieve the Rectanle 
                // of the selected SubItem
                ListView\_GetSubItemRect(hWnd1, temp->iItem,
                	temp->iSubItem, LVIR\_BOUNDS, &rect);
                //Get the Rectange of the listControl
                ::GetWindowRect(temp->hdr.hwndFrom, &rect1);
                //Get the Rectange of the Dialog
                ::GetWindowRect(m\_hWnd, &rect2);
                
                int x = rect1.left - rect2.left;
                int y = rect1.top - rect2.top;
                
                if (nItem != -1)
                	::SetWindowPos(::GetD
                
                V Offline
                V Offline
                Victor Nijegorodov
                wrote on last edited by
                #7

                Is there some very important reason for you to use in a MFC application plain Win32 macros/messages to handle List Control? What is wrong to use [CListCtrl Class](https://msdn.microsoft.com/en-us/library/hfshke78.aspx) methods?

                L 1 Reply Last reply
                0
                • V Victor Nijegorodov

                  Is there some very important reason for you to use in a MFC application plain Win32 macros/messages to handle List Control? What is wrong to use [CListCtrl Class](https://msdn.microsoft.com/en-us/library/hfshke78.aspx) methods?

                  L Offline
                  L Offline
                  lolici
                  wrote on last edited by
                  #8

                  Yes!As I mentioned above I am looking for a way to save all editable data in list view in a text file.

                  D 1 Reply Last reply
                  0
                  • J Jochen Arndt

                    Use the debugger to find out when the exception occurs and check the code lines that has been executed before. You can write strings directly to text files when using appropriate file functions. But those will be then Unicode text files (when having a Unicode build) and the file class does no perform implicit conversion to ANSI text.

                    L Offline
                    L Offline
                    lolici
                    wrote on last edited by
                    #9

                    Vielen Dank! :)

                    1 Reply Last reply
                    0
                    • L lolici

                      Yes!As I mentioned above I am looking for a way to save all editable data in list view in a text file.

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #10

                      lolici wrote:

                      ...I am looking for a way to save all editable data in list view in a text file.

                      What does that have to do with not using MFC?

                      "One man's wage rise is another man's price increase." - Harold Wilson

                      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                      "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups