wxTreeCtrl Topic is solved

If you are using wxDev-C++ for your wxWidgets design, please ask your questions here instead of in IDE Related.
Post Reply
Rachel
Earned a small fee
Earned a small fee
Posts: 22
Joined: Sat Jun 18, 2011 11:37 pm

wxTreeCtrl

Post by Rachel » Thu Jul 14, 2011 11:00 pm

Here's my question:
I want to create a dynamic wxTreeCtrl, where i append items during using my program. I'm saving text files with data, but when I save it I want to display it also in the Tree. Later when I click on an item I want to load the data in my GUi again.
Is there an event double click?
And how can I create an event for items that will be appended later?
Thanks for any help!

Rachel
Earned a small fee
Earned a small fee
Posts: 22
Joined: Sat Jun 18, 2011 11:37 pm

Re: wxTreeCtrl

Post by Rachel » Sun Jul 17, 2011 8:55 pm

if I press a button then a new item should ne added to the tree, everytime I press the button a new item should be added (with the name of a used textfile).
this is what I wrote:
in CreateGUIControls():
wxTreeItemId parentNodeIndex = WxTreeCtrl1->AddRoot(wxT("Class Folder"));

void buttonclick:
...getfilename
WxTreeCtrl1->AppendItem(parentNodeIndex, wxT(filename));

it says: `parentNodeIndex' was not declared in this scope

How does it work?

peteh
Earned a small fee
Earned a small fee
Posts: 16
Joined: Fri Jan 29, 2010 4:37 pm

Re: wxTreeCtrl

Post by peteh » Mon Jul 18, 2011 9:32 am

You declared and initialised 'parentNodeIndex' in 'CreateGUIControls()';
'parentNodeIndex' is only available in this function and therefore cannot be accessed from another function.

What you need to do is declare 'parentNodeIndex' in the private section of the class header

Code: Select all

wxTreeItemId parentNodeIndex;
then initialise it in CreateGUIControls()

Code: Select all

parentNodeIndex = WxTreeCtrl1->AddRoot(wxT("Class Folder"));
then use it in buttonclick

Code: Select all

WxTreeCtrl1->AppendItem(parentNodeIndex, wxT(filename));

Rachel
Earned a small fee
Earned a small fee
Posts: 22
Joined: Sat Jun 18, 2011 11:37 pm

Re: wxTreeCtrl

Post by Rachel » Mon Jul 18, 2011 2:11 pm

Thanks, that works.

Furthermore I only want to append the item, if it isn't already shown.
I tried to compare:
if (WxTreeCtrl1->GetItemText(parentNodeIndex) == fileName)
but its always false..
How can I compare the labels of the items with the string fileName?

peteh
Earned a small fee
Earned a small fee
Posts: 16
Joined: Fri Jan 29, 2010 4:37 pm

Re: wxTreeCtrl

Post by peteh » Mon Jul 18, 2011 4:30 pm

One way possible way:

Code: Select all


    wxTreeItemId item_id;
    wxTreeItemIdValue cookie;
    
       
            
    //get id of 1st child
    item_id = WxTreeCtrl1->GetFirstChild(parentNodeIndex, cookie );
    
    //Iterate thru valid items
    while(item_id.IsOk())
    {
        
if (WxTreeCtrl1->GetItemText(item_id).IsSameAs(fileName)) return;

        //Get id of next child
        item_id = WxTreeCtrl1->GetNextChild(parentNodeIndex, cookie);
               
    }
    
    WxTreeCtrl1->AppendItem(parentNodeIndex, wxT(fileName));

Rachel
Earned a small fee
Earned a small fee
Posts: 22
Joined: Sat Jun 18, 2011 11:37 pm

Re: wxTreeCtrl

Post by Rachel » Mon Jul 18, 2011 4:50 pm

okey, the filenames are appended correctly, but after WxTreeCtrl1->AppendItem(parentNodeIndex, wxT(fileName)); an ofstream to the specific filename is executed. So I need to jump from "return" to the line after AppendItem, instead of ending with return. Is this possible?


edit: I used instead of return, goto keyword. It works and is executing the ofstream.

thanks!

Post Reply