Solved

getting a string from a listbox

Posted on 1998-07-14
6
213 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
bobThere java chalenge 45 94
Expand LInux Boot partition remotly 3 96
sum67 challenge 35 110
Is there a simple front-end menu system. 9 105
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: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

830 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