Solved

getting a string from a listbox

Posted on 1998-07-14
6
202 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Windows Drag & Drop Location 2 85
Expand macro to ask for filename column 8 35
ORA-01403: no data found 43 83
fizzArray2 challenge 1 61
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

759 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

19 Experts available now in Live!

Get 1:1 Help Now