Click here to Skip to main content
15,891,431 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionRe: Again CListCtrl Pin
Bravoone_200630-May-07 1:08
Bravoone_200630-May-07 1:08 
AnswerRe: Again CListCtrl Pin
Naveen30-May-07 1:13
Naveen30-May-07 1:13 
GeneralRe: Again CListCtrl Pin
Bravoone_200630-May-07 1:16
Bravoone_200630-May-07 1:16 
GeneralRe: Again CListCtrl Pin
Naveen30-May-07 1:22
Naveen30-May-07 1:22 
QuestionRe: Again CListCtrl Pin
Bravoone_200630-May-07 1:30
Bravoone_200630-May-07 1:30 
QuestionRe: Again CListCtrl Pin
Bravoone_200630-May-07 1:42
Bravoone_200630-May-07 1:42 
AnswerRe: Again CListCtrl Pin
Hamid_RT30-May-07 2:24
Hamid_RT30-May-07 2:24 
AnswerRe: Again CListCtrl [modified] Pin
#realJSOP30-May-07 3:10
mve#realJSOP30-May-07 3: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 into the edit control
    m_sum.SetWindowText(csText);
}


The code above allows you to debug easily, but at the same time, doesn't make any assumptions about the data you're trying to process. Your code blindly assumes that the data is in an expected format. For all you know, the selected column might contain alphabetic text instead of numbers. MY code performs the mundane (yet important) sanity checks before doing anything with the data.

Caveat: I typed all that in off the top of my head so there might be small compile issues, but certainly nothing you can't handle. On the other hand, if you can't handle it, maybe you should find another line of work.


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.



-- modified at 9:21 Wednesday 30th May, 2007


-- modified at 9:36 Wednesday 30th May, 2007


"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


QuestionRe: Again CListCtrl Pin
David Crow30-May-07 3:58
David Crow30-May-07 3:58 
AnswerRe: Again CListCtrl Pin
#realJSOP30-May-07 4:23
mve#realJSOP30-May-07 4:23 
GeneralRe: Again CListCtrl Pin
David Crow30-May-07 4:29
David Crow30-May-07 4:29 
GeneralRe: Again CListCtrl Pin
#realJSOP30-May-07 4:36
mve#realJSOP30-May-07 4:36 
GeneralRe: Again CListCtrl Pin
Hamid_RT30-May-07 8:49
Hamid_RT30-May-07 8:49 
AnswerRe: Again CListCtrl Pin
Naveen30-May-07 14:16
Naveen30-May-07 14:16 
Questioncallback Pin
nitin330-May-07 0:50
nitin330-May-07 0:50 
AnswerRe: callback Pin
Programm3r30-May-07 0:54
Programm3r30-May-07 0:54 
AnswerRe: callback Pin
Cedric Moonen30-May-07 0:55
Cedric Moonen30-May-07 0:55 
GeneralRe: callback Pin
nitin330-May-07 1:00
nitin330-May-07 1:00 
AnswerRe: callback Pin
Programm3r30-May-07 1:05
Programm3r30-May-07 1:05 
QuestionCreating ActiveX control Using another ActiveX control in MFC Pin
arun kumar kk30-May-07 0:46
arun kumar kk30-May-07 0:46 
AnswerRe: Creating ActiveX control Using another ActiveX control in MFC Pin
Programm3r30-May-07 0:57
Programm3r30-May-07 0:57 
GeneralRe: Creating ActiveX control Using another ActiveX control in MFC Pin
arun kumar kk30-May-07 1:06
arun kumar kk30-May-07 1:06 
QuestionEXCEL AUTOMATION, VC++ Pin
artihcv30-May-07 0:46
artihcv30-May-07 0:46 
AnswerRe: EXCEL AUTOMATION, VC++ Pin
Programm3r30-May-07 1:01
Programm3r30-May-07 1:01 
AnswerRe: EXCEL AUTOMATION, VC++ Pin
artihcv30-May-07 1:32
artihcv30-May-07 1:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.