Solved

getting a string from a listbox

Posted on 1998-07-14
6
216 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

734 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