Solved

getting a string from a listbox

Posted on 1998-07-14
6
208 Views
Last Modified: 2013-11-20
I'm trying to copy the different strings in a listbox into a CString object, and not having much luck.
i'm using the CListBox member function CListBox::GetText, which has the following syntax:
        int GetText( int nIndex, LPTSTR lpszBuffer ) const;

But when I use it in a for loop, putting the index of the loop into the <int nIndex> place, i only get the first string. This is my code:

            for (i=0; i<count; i++)
            {
                  int tempLen = m_versionListBox.GetTextLen(i);
                  char *tempBuf = new char[tempLen];
                  m_versionListBox.GetText(i, tempBuf);      // copy to tempBuf
                  CString tempString(tempBuf, tempLen);

                  int debugInt = m_versionListBox.GetCurSel();
                  m_versionListBox.AddString(tempString.SpanExcluding(_T(".ams")));
            }

i can't figure out what is wrong. (the idea is that I'm trying to copy the values out of the listbox, remove the file extensions from the strings, and copy them back)
The call to GetText(...,...) seems to return the wrong value. Everytime it copies the string from index 0 in the listbox into tempBuf, that seems to be the only problem with the loop.

Help!
0
Comment
Question by:nicholso
  • 3
  • 3
6 Comments
 
LVL 7

Accepted Solution

by:
psdavis earned 100 total points
ID: 1319117
A) First, try it in a little easier format...

for( i = 0; i < count; i ++ )
{
   CString strTemp;
   m_versionListBox.GetText( strTemp );

   ...

B) Since you are re-adding the information back to the listbox, (especially if sort is selected) then the list becomes corrupted.  Try loading them all into an array, removing all the records, then putting them back.  (Or removing the sort)

C) Are you using the .Directory thing?  Try the new CFileFind class.  You will get a list of files that you can strip the files off of then add them one at a time to the listbox and makes this whole thing mute.

Phillip

0
 

Author Comment

by:nicholso
ID: 1319118
Your idea about CFileFind works great, except the while loop quits one file too early (i.e. the function FindNextFile() returns a 0 at the top of the loop which should grab the last file name.

If you know what's up with that, I would appreciate it. This is my code. Thanks so much!

m_versionListBox.ResetContent();      // prepare to populate the listbox
CFileFind fileFind;
CString tempFileName;
fileFind.FindFile(_T("*.ams"), 0);      // search for files with *.ams extension
while (fileFind.FindNextFile() != 0)
{
      tempFileName = fileFind.GetFileTitle();            // get title of file
      m_versionListBox.AddString(tempFileName);      // add to listbox
}

0
 
LVL 7

Expert Comment

by:psdavis
ID: 1319119
// The most common mistake with any Find/Next combination is to put the FindNext inside of the while loop.  The last file found will return a false even though the file is correct.

    CFileFind finder;
    BOOL bWorking = finder.FindFile("*.*");
    while (bWorking)
    {
        bWorking = finder.FindNextFile();
        cout << (LPCTSTR) finder.GetFileName() << endl;
    }


0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:nicholso
ID: 1319120
Your idea about CFileFind works great, except the while loop quits one file too early (i.e. the function FindNextFile() returns a 0 at the top of the loop which should grab the last file name.

If you know what's up with that, I would appreciate it. This is my code. Thanks so much!

m_versionListBox.ResetContent();      // prepare to populate the listbox
CFileFind fileFind;
CString tempFileName;
fileFind.FindFile(_T("*.ams"), 0);      // search for files with *.ams extension
while (fileFind.FindNextFile() != 0)
{
      tempFileName = fileFind.GetFileTitle();            // get title of file
      m_versionListBox.AddString(tempFileName);      // add to listbox
}

0
 
LVL 7

Expert Comment

by:psdavis
ID: 1319121
Thanks for the grade!! Your second comment is confusing though.  Is everything OK?


0
 

Author Comment

by:nicholso
ID: 1319122
yeah, I just hit paste again by accident. thanks so much, it works fine. ;-)
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Strange date stored 22 118
How to determine file rename from EventLog ? 2 49
if loop in java 3 139
Fibonacci challenge 11 120
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now