Solved

getting a string from a listbox

Posted on 1998-07-14
6
220 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
 Database Backup and Recovery Best Practices

Join Percona’s, Architect, Manjot Singh as he presents Database Backup and Recovery Best Practices (with a Focus on MySQL) on Thursday, July 27, 2017 at 11:00 am PDT / 2:00 pm EDT (UTC-7). In the case of a failure, do you know how long it will take to restore your database?

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

623 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