brahle

Hello!
I would like to read some text from a file and then write it in a list box. I have read the data to a vector< string >, but I don't know how can I add a string (or a char*) to a list box. Can someone please help me



Re: Visual C++ Language List Box

Giovanni Dicanio

Hi,

to add a string to a ListBox, if you are in MFC context, you can use the CListBox::AddString method.

http://msdn2.microsoft.com/en-us/library/2cekde96(VS.80).aspx

If you are using Win32 APIs directly, you can send the LB_ADDSTRING message to the listbox.

http://msdn2.microsoft.com/en-us/library/ms671405.aspx

Please also note that std:: string usually stores Ansi strings, while it could be better to use Unicode with Windows GUI (I consider Ansi strings as an obsolete string type of the past; today strings should always be Unicode, IMHO).

While I like STL containers like vector and map, I prefer using ATL/MFC CString class for strings. CString will work fine with other ATL and MFC GUI classes, while STL string requires some more code.

If you read the string in a std:: string class, you can convert it to a CString, using a CString conversion constructor, and then pass the CString instance to AddString method. e.g.

Code Snippet

CListBox * pMyListBox;

string s; // To add to list-box

CString str( s.c_str ); // Convert to Unicode, if necessary

pMyListBox->AddString( str );

Giovanni





Re: Visual C++ Language List Box

brahle

First of all, thanks for the suggestions! However, the problem is that I ran to difficulties when trying to implement them.

I have been using the System::Windows::Forms::ListBox and not CListBox. After some research, I found out that the CListBox is located in the header "afxwin.h". However, I was unable to include it. The compiler says that the file does not exist. I am using Visual C++ 2005 Express, and I have installed PSDK for Windows Vista. The same happens for CString (which is defined in "Afx.h").

What should do to be able to use those headers





Re: Visual C++ Language List Box

Giovanni Dicanio

Hi,

so you are using the Express edition: I'm sorry, but I think that the Express edition does neither support MFC nor ATL.

However, you could use the pure Win32 SDK API, as you have installed the PSDK...

If you are using WinForms, as I understand from your post: if I were using C# I would add the new item to the listbox .Items collection, using the .Add method, e.g.: myListBox.Items.Add( "the string..." );

But I've never done C++/CLI. I don't know the C++/CLI syntax, maybe it could be something like myListBox->Items->Add( ... ) ...

Giovanni





Re: Visual C++ Language List Box

brahle

Hi!

All of the time I was using the
myListBox->Items->Add() method. However it can take only a parameter of a type System:: Object^. The problem is that I do not know how to convert an object of type std:: string (or char*, wchar_t* or System:: String) to an object of type System:: Object^. I noticed I can insert System:: String^, but I do not know how can I convert something to that type (for example, std:: string).

To be more specific, I would like to be able to do something like this:
Code Snippet

String^ s;
string tmp;
ListBox myListBox;

tmp = "some string";
s = tmp; // doesn't work
myListBox->Items->Add( s );







Re: Visual C++ Language List Box

brahle

Okay, so this is what I did to finally solved it:

string toInsert;
ListBox^ myListBox;

toInsert = "string to insert";

myListBox->Items->Add( String( toInsert.c_str() ).Clone() );