newbie question

If you are using wxDev-C++ for your wxWidgets design, please ask your questions here instead of in IDE Related.
Post Reply
Russ S
In need of some credit
In need of some credit
Posts: 2
Joined: Thu Aug 12, 2010 12:08 am

newbie question

Post by Russ S »

I've been learning c++. I got through some consul apps just fine & now it's time to do the gui thing. I downloaded wxWidgets and the Dev C++ package. All well and good. Just to put my toes in the water, I've created (well the designer created) a form with a button & listbox. In the button click event I am opening a text file & getting each line. Now I'd like to insert those lines into the listbox. At this point, I've hit a wall. I just have not found a sample that shows how to do this. I'm sure it's not a big deal, just not seeing how.

Code: Select all

void Project1Dlg::LoadClick(wxCommandEvent& event)
{
  string line;
  ifstream myfile ("testfile.txt"); 
  if (myfile.is_open())
  {
    while (! myfile.eof() )
    {
      getline (myfile,line);     
      WxListBox1->InsertItems(line,0);  // this is the line that I'm not doing correctly.
    }
    myfile.close();
  }
}
WxListBox1 is the name the designer gave to the listbox.

Any help would be appreciated.

TIA,
Russ
Taamalus
Experienced Solver
Experienced Solver
Posts: 86
Joined: Wed Dec 09, 2009 3:31 pm
Location: Toronto

Post by Taamalus »

Hello! I think this link should help a little. I am new too, and those tutorials helped me, hopefully they will help you too.

:)
... time waits for no one
Hank
jfouche
Super wx Problem Solver
Super wx Problem Solver
Posts: 442
Joined: Tue May 06, 2008 4:52 pm
Location: France

Post by jfouche »

wxListBox::InsertItems insert multiple string. Moreover, wxString is not fully compatible with std::string (at least in 2.8.x branch, which is the stable one).

There are 2 solutions :
1 - You create a string array, fill it, and then insert it in your list box :

Code: Select all

wxArrayString items;
while (! myfile.eof() )
{
  getline (myfile,line);     
  items.push_back(wxString::FromUtf8(line.c_str()));
}
WxListBox1->InsertItems(items,0);
2 - You want to insert strings while reading the file :

Code: Select all

while (! myfile.eof() )
{
  getline (myfile,line);     
  WxListBox1->Append(wxString::FromUtf8(line.c_str()));
}
This solution is not as efficient as the first one.
Jérémie
Russ S
In need of some credit
In need of some credit
Posts: 2
Joined: Thu Aug 12, 2010 12:08 am

Post by Russ S »

Thanks to both for the quick reply. I'll check out the link. I tried the suggested code and got the following error:

C:\Dev-Cpp\Russ Tests\Project1Dlg.cpp `FromUtf8' is not a member of `wxString'

Am I missing a library?

Thanks,
Russ
Taamalus
Experienced Solver
Experienced Solver
Posts: 86
Joined: Wed Dec 09, 2009 3:31 pm
Location: Toronto

Post by Taamalus »

Russ S wrote:Thanks to both for the quick reply. I'll check out the link. I tried the suggested code and got the following error:

C:\Dev-Cpp\Russ Tests\Project1Dlg.cpp `FromUtf8' is not a member of `wxString'

Am I missing a library?

Thanks,
Russ
:) No! the member is FromUTF8 not FromUtf8. I'm better at links .

btw the reason I post links, they are always there, I'm not. cheers - Hank!
Post Reply