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. Again CListCtrl

Again CListCtrl

Scheduled Pinned Locked Moved C / C++ / MFC
question
16 Posts 5 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.
  • B Offline
    B Offline
    Bravoone_2006
    wrote on last edited by
    #1

    How can i get the result ? i have a CListCtrl m_list -------------------------- |Col1 | Col2| Col3 | -------------------------- | 10 | 2 | 1 | -------------------------- | 10 | 2 | 1 | -------------------------- all i want is to get the result from col3 = 2 m_list.GetItem(...); s1.Format(....); AfxMessageBox(s1); // =2 from (Col3) How Can i get the result?(from Col3);

    Bravoone

    N 1 Reply Last reply
    0
    • B Bravoone_2006

      How can i get the result ? i have a CListCtrl m_list -------------------------- |Col1 | Col2| Col3 | -------------------------- | 10 | 2 | 1 | -------------------------- | 10 | 2 | 1 | -------------------------- all i want is to get the result from col3 = 2 m_list.GetItem(...); s1.Format(....); AfxMessageBox(s1); // =2 from (Col3) How Can i get the result?(from Col3);

      Bravoone

      N Offline
      N Offline
      Naveen
      wrote on last edited by
      #2

      Bravoone_2006 wrote:

      How Can i get the result?(from Col3);

      as follows CString csText = GetItemText(1,2); AfxMessageBox(s1);

      nave

      B 1 Reply Last reply
      0
      • N Naveen

        Bravoone_2006 wrote:

        How Can i get the result?(from Col3);

        as follows CString csText = GetItemText(1,2); AfxMessageBox(s1);

        nave

        B Offline
        B Offline
        Bravoone_2006
        wrote on last edited by
        #3

        no you dont understand i need row1+row2 = s1(or can be more than 2 rows) How?

        Bravoone

        N 1 Reply Last reply
        0
        • B Bravoone_2006

          no you dont understand i need row1+row2 = s1(or can be more than 2 rows) How?

          Bravoone

          N Offline
          N Offline
          Naveen
          wrote on last edited by
          #4

          you mean you want to get the sum of integers in all columns of row1 and row2?

          nave

          B 1 Reply Last reply
          0
          • N Naveen

            you mean you want to get the sum of integers in all columns of row1 and row2?

            nave

            B Offline
            B Offline
            Bravoone_2006
            wrote on last edited by
            #5

            YES !!!!!!

            Bravoone

            N 1 Reply Last reply
            0
            • B Bravoone_2006

              YES !!!!!!

              Bravoone

              N Offline
              N Offline
              Naveen
              wrote on last edited by
              #6

              put it in a for loop int nsum = 0; for( int nRow = 0; nRow < m_lis.GetItemCount();nRow++) { for( int nColumn = 0; nColumn < 3;nColumn ++) { CString csText = GetItemText(nRow ,nColumn ); nsum += _atoi( csText); } }

              nave

              B 2 Replies Last reply
              0
              • N Naveen

                put it in a for loop int nsum = 0; for( int nRow = 0; nRow < m_lis.GetItemCount();nRow++) { for( int nColumn = 0; nColumn < 3;nColumn ++) { CString csText = GetItemText(nRow ,nColumn ); nsum += _atoi( csText); } }

                nave

                B Offline
                B Offline
                Bravoone_2006
                wrote on last edited by
                #7

                This is my problem is not working !why? CString csText = m_list1.GetItemText(nRow ,nColumn ); nsum += atoi( csText); m_sum.SetWindowText(csText);

                Bravoone

                1 Reply Last reply
                0
                • N Naveen

                  put it in a for loop int nsum = 0; for( int nRow = 0; nRow < m_lis.GetItemCount();nRow++) { for( int nColumn = 0; nColumn < 3;nColumn ++) { CString csText = GetItemText(nRow ,nColumn ); nsum += _atoi( csText); } }

                  nave

                  B Offline
                  B Offline
                  Bravoone_2006
                  wrote on last edited by
                  #8

                  Why is not working ? int nsum = 0; for( int nRow = 0; nRow < m_list1.GetItemCount();nRow++) { for( int nColumn = 0; nColumn < 2;nColumn++) { CString csText = m_list1.GetItemText(nRow ,nColumn ); nsum += atoi( csText); m_sum.SetWindowText(csText); } }

                  Bravoone

                  H realJSOPR N 3 Replies Last reply
                  0
                  • B Bravoone_2006

                    Why is not working ? int nsum = 0; for( int nRow = 0; nRow < m_list1.GetItemCount();nRow++) { for( int nColumn = 0; nColumn < 2;nColumn++) { CString csText = m_list1.GetItemText(nRow ,nColumn ); nsum += atoi( csText); m_sum.SetWindowText(csText); } }

                    Bravoone

                    H Offline
                    H Offline
                    Hamid Taebi
                    wrote on last edited by
                    #9

                    Bravoone_2006 wrote:

                    int nsum = 0;

                    Bravoone_2006 wrote:

                    m_sum.SetWindowText(csText);

                    Where you want to show csText?


                    WhiteSky


                    1 Reply Last reply
                    0
                    • B Bravoone_2006

                      Why is not working ? int nsum = 0; for( int nRow = 0; nRow < m_list1.GetItemCount();nRow++) { for( int nColumn = 0; nColumn < 2;nColumn++) { CString csText = m_list1.GetItemText(nRow ,nColumn ); nsum += atoi( csText); m_sum.SetWindowText(csText); } }

                      Bravoone

                      realJSOPR Offline
                      realJSOPR Offline
                      realJSOP
                      wrote on last edited by
                      #10

                      int nsum = 0;
                      for( int nRow = 0; nRow < m_list1.GetItemCount();nRow++)
                      {
                      for( int nColumn = 0; nColumn < 2;nColumn++)
                      {
                      CString csText = m_list1.GetItemText(nRow ,nColumn );
                      nsum += atoi( csText);
                      m_sum.SetWindowText(csText);
                      }
                      }

                      I would do it like this:

                      int CMyDialogBox::SumColumn(CListCtrl* pList, int nColumn)
                      {
                      // the first thing i'd do is make sure the specified column is within the
                      // range of available columns, but i'm goin g to leave that as an exercise
                      // for you

                      // declare and initialize some vars
                      int nSum = 0;
                      int nRows = pList->GetItemCount();
                      CString csText = "";
                      // if we actually have rows to process
                      if (nRows > 0)
                      {
                          // process each one
                          for (int nRow = 0; nRow < nRows; nRow++)
                          {
                              // retrieve the text from the current row and specified column
                              csText = pList->GetItemText(nRow ,nColumn );
                              // if the string isn't empty
                              if (!csText.IsEmpty())
                              {
                                  // establish a string a valid characters so we can ensure 
                                  // that the extracted string is in fact a number
                                  CString sNumbers = "0123456789";
                                  // assume we have a valid integer value
                                  bool bIsValid = true;
                                  // check each character in the exttracted string
                                  for (int i = 0; i < csText.GetLength(); i++)
                                  {
                                      // if we find the character in the validation string, 
                                      // the number might be valid
                                      bIsValid = (sNumbers.Find(csText.GetAt(i) < 0);
                                      // if the exttracted string wasn't valid 
                                      if (!bIsValid)
                                      {
                                          // get out of the loop
                                          break;
                                      }
                                  }
                                  // if the exttracted string WAS valid, add the value to nSum
                                  if (bIsValid)
                                  {
                                      nSum += atoi( csText);
                                  }
                              }
                          }
                      }
                      

                      }

                      void CMyDialog::SomeFunction()
                      {
                      // by the way, if you want to sum the second column, you need to
                      // specify 1 as the second paramter
                      int nResult = SumColumn(&m_list1, 1);
                      CString csText;

                      // turn nSum into a string - we can use the csText string we've 
                      // already been using
                      csText.Format("%d", nSum);
                      // put the csText string
                      
                      D H 2 Replies Last reply
                      0
                      • realJSOPR realJSOP

                        int nsum = 0;
                        for( int nRow = 0; nRow < m_list1.GetItemCount();nRow++)
                        {
                        for( int nColumn = 0; nColumn < 2;nColumn++)
                        {
                        CString csText = m_list1.GetItemText(nRow ,nColumn );
                        nsum += atoi( csText);
                        m_sum.SetWindowText(csText);
                        }
                        }

                        I would do it like this:

                        int CMyDialogBox::SumColumn(CListCtrl* pList, int nColumn)
                        {
                        // the first thing i'd do is make sure the specified column is within the
                        // range of available columns, but i'm goin g to leave that as an exercise
                        // for you

                        // declare and initialize some vars
                        int nSum = 0;
                        int nRows = pList->GetItemCount();
                        CString csText = "";
                        // if we actually have rows to process
                        if (nRows > 0)
                        {
                            // process each one
                            for (int nRow = 0; nRow < nRows; nRow++)
                            {
                                // retrieve the text from the current row and specified column
                                csText = pList->GetItemText(nRow ,nColumn );
                                // if the string isn't empty
                                if (!csText.IsEmpty())
                                {
                                    // establish a string a valid characters so we can ensure 
                                    // that the extracted string is in fact a number
                                    CString sNumbers = "0123456789";
                                    // assume we have a valid integer value
                                    bool bIsValid = true;
                                    // check each character in the exttracted string
                                    for (int i = 0; i < csText.GetLength(); i++)
                                    {
                                        // if we find the character in the validation string, 
                                        // the number might be valid
                                        bIsValid = (sNumbers.Find(csText.GetAt(i) < 0);
                                        // if the exttracted string wasn't valid 
                                        if (!bIsValid)
                                        {
                                            // get out of the loop
                                            break;
                                        }
                                    }
                                    // if the exttracted string WAS valid, add the value to nSum
                                    if (bIsValid)
                                    {
                                        nSum += atoi( csText);
                                    }
                                }
                            }
                        }
                        

                        }

                        void CMyDialog::SomeFunction()
                        {
                        // by the way, if you want to sum the second column, you need to
                        // specify 1 as the second paramter
                        int nResult = SumColumn(&m_list1, 1);
                        CString csText;

                        // turn nSum into a string - we can use the csText string we've 
                        // already been using
                        csText.Format("%d", nSum);
                        // put the csText string
                        
                        D Offline
                        D Offline
                        David Crow
                        wrote on last edited by
                        #11

                        John Simmons / outlaw programmer wrote:

                        NOTE: I didn't want to use the atoi function to determine the validity of the converted string because if it's not a number, it returns 0, which IS a number. It's also a VALID number, so you can't just assume that the return value is what you expect it to be. That's why I included code to check each character one at a time before trying to convert it to a numeric value.

                        So why not just use strtol()?


                        "A good athlete is the result of a good and worthy opponent." - David Crow

                        "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                        realJSOPR 1 Reply Last reply
                        0
                        • D David Crow

                          John Simmons / outlaw programmer wrote:

                          NOTE: I didn't want to use the atoi function to determine the validity of the converted string because if it's not a number, it returns 0, which IS a number. It's also a VALID number, so you can't just assume that the return value is what you expect it to be. That's why I included code to check each character one at a time before trying to convert it to a numeric value.

                          So why not just use strtol()?


                          "A good athlete is the result of a good and worthy opponent." - David Crow

                          "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                          realJSOPR Offline
                          realJSOPR Offline
                          realJSOP
                          wrote on last edited by
                          #12

                          DavidCrow wrote:

                          So why not just use strtol()?

                          Because like atoi, it returns ZERO if the string cannot be converted.

                          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                          -----
                          "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                          D 1 Reply Last reply
                          0
                          • realJSOPR realJSOP

                            DavidCrow wrote:

                            So why not just use strtol()?

                            Because like atoi, it returns ZERO if the string cannot be converted.

                            "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                            -----
                            "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

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

                            True, but unlike atoi(), it has an argument that tells if the entire string was looked at or not.


                            "A good athlete is the result of a good and worthy opponent." - David Crow

                            "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                            realJSOPR 1 Reply Last reply
                            0
                            • D David Crow

                              True, but unlike atoi(), it has an argument that tells if the entire string was looked at or not.


                              "A good athlete is the result of a good and worthy opponent." - David Crow

                              "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                              realJSOPR Offline
                              realJSOPR Offline
                              realJSOP
                              wrote on last edited by
                              #14

                              There's a bunch of different ways to get to the same point. He used atoi, so I did to. The point really isn't worth arguing, IMHO, but y'all go right ahead and talk amongst yourselves.

                              "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                              -----
                              "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                              1 Reply Last reply
                              0
                              • realJSOPR realJSOP

                                int nsum = 0;
                                for( int nRow = 0; nRow < m_list1.GetItemCount();nRow++)
                                {
                                for( int nColumn = 0; nColumn < 2;nColumn++)
                                {
                                CString csText = m_list1.GetItemText(nRow ,nColumn );
                                nsum += atoi( csText);
                                m_sum.SetWindowText(csText);
                                }
                                }

                                I would do it like this:

                                int CMyDialogBox::SumColumn(CListCtrl* pList, int nColumn)
                                {
                                // the first thing i'd do is make sure the specified column is within the
                                // range of available columns, but i'm goin g to leave that as an exercise
                                // for you

                                // declare and initialize some vars
                                int nSum = 0;
                                int nRows = pList->GetItemCount();
                                CString csText = "";
                                // if we actually have rows to process
                                if (nRows > 0)
                                {
                                    // process each one
                                    for (int nRow = 0; nRow < nRows; nRow++)
                                    {
                                        // retrieve the text from the current row and specified column
                                        csText = pList->GetItemText(nRow ,nColumn );
                                        // if the string isn't empty
                                        if (!csText.IsEmpty())
                                        {
                                            // establish a string a valid characters so we can ensure 
                                            // that the extracted string is in fact a number
                                            CString sNumbers = "0123456789";
                                            // assume we have a valid integer value
                                            bool bIsValid = true;
                                            // check each character in the exttracted string
                                            for (int i = 0; i < csText.GetLength(); i++)
                                            {
                                                // if we find the character in the validation string, 
                                                // the number might be valid
                                                bIsValid = (sNumbers.Find(csText.GetAt(i) < 0);
                                                // if the exttracted string wasn't valid 
                                                if (!bIsValid)
                                                {
                                                    // get out of the loop
                                                    break;
                                                }
                                            }
                                            // if the exttracted string WAS valid, add the value to nSum
                                            if (bIsValid)
                                            {
                                                nSum += atoi( csText);
                                            }
                                        }
                                    }
                                }
                                

                                }

                                void CMyDialog::SomeFunction()
                                {
                                // by the way, if you want to sum the second column, you need to
                                // specify 1 as the second paramter
                                int nResult = SumColumn(&m_list1, 1);
                                CString csText;

                                // turn nSum into a string - we can use the csText string we've 
                                // already been using
                                csText.Format("%d", nSum);
                                // put the csText string
                                
                                H Offline
                                H Offline
                                Hamid Taebi
                                wrote on last edited by
                                #15

                                Your answer was perfect.:-D


                                WhiteSky


                                1 Reply Last reply
                                0
                                • B Bravoone_2006

                                  Why is not working ? int nsum = 0; for( int nRow = 0; nRow < m_list1.GetItemCount();nRow++) { for( int nColumn = 0; nColumn < 2;nColumn++) { CString csText = m_list1.GetItemText(nRow ,nColumn ); nsum += atoi( csText); m_sum.SetWindowText(csText); } }

                                  Bravoone

                                  N Offline
                                  N Offline
                                  Naveen
                                  wrote on last edited by
                                  #16

                                  Bravoone_2006 wrote:

                                  m_sum.SetWindowText(csText);

                                  the SetWindowText() must be used after all the loop isn't? int nsum = 0; for( int nRow = 0; nRow < m_list1.GetItemCount();nRow++) { for( int nColumn = 0; nColumn < 2;nColumn++) { CString csText = m_list1.GetItemText(nRow ,nColumn ); nsum += atoi( csText); } } **TCHAR tcsum[50] _itoa( nsum, tcsum, 10 ); m_sum.SetWindowText(tcsum);**

                                  nave

                                  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