saving layout of TreeView control

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,
I have a windows form with a TreeView control.
When I first open the tree all nodes are colapsed. Then, I expand some of
them and close the form.
When I call Form.showDialog() once again all nodes are collapsed again.
I would like the layout to be saved (nodes which I expanded should stay
expanded). I don't change the ImageList, Scrollable, CheckBoxes or
StateImageList properties of the control between closing and reopening the
form.

any ideas?

thanks,
 
Once you close the form with the tree view, the form object and all the
objects it contained are destroyed and the memory is reused. What you could
do is write some code such that when the form is closing you save the state
of the tree view to a file like xml for example. You should keep it simple:
ie for each node in the tree check if it is expanded or collapsed and store
that in the xml file. When the form is opened, it should check this file to
see how to present the tree when it loads.

Adrian.
 
I was afraid this was the only solution...
Thank you very much for the help!
--
dshemesh


Adrian Voicu said:
Once you close the form with the tree view, the form object and all the
objects it contained are destroyed and the memory is reused. What you could
do is write some code such that when the form is closing you save the state
of the tree view to a file like xml for example. You should keep it simple:
ie for each node in the tree check if it is expanded or collapsed and store
that in the xml file. When the form is opened, it should check this file to
see how to present the tree when it loads.

Adrian.
--
[Please mark my answer if it was helpful to you]




dshemesh said:
Hello,
I have a windows form with a TreeView control.
When I first open the tree all nodes are colapsed. Then, I expand some of
them and close the form.
When I call Form.showDialog() once again all nodes are collapsed again.
I would like the layout to be saved (nodes which I expanded should stay
expanded). I don't change the ImageList, Scrollable, CheckBoxes or
StateImageList properties of the control between closing and reopening the
form.

any ideas?

thanks,
 
Just to make sure:
When I close the form, I still hold a pointer to it, so it's not totally
destroyed. When I reopen it, I call showDialog() for the same Form instance I
closed before.
Is this still the case?
--
dshemesh


Adrian Voicu said:
Once you close the form with the tree view, the form object and all the
objects it contained are destroyed and the memory is reused. What you could
do is write some code such that when the form is closing you save the state
of the tree view to a file like xml for example. You should keep it simple:
ie for each node in the tree check if it is expanded or collapsed and store
that in the xml file. When the form is opened, it should check this file to
see how to present the tree when it loads.

Adrian.
--
[Please mark my answer if it was helpful to you]




dshemesh said:
Hello,
I have a windows form with a TreeView control.
When I first open the tree all nodes are colapsed. Then, I expand some of
them and close the form.
When I call Form.showDialog() once again all nodes are collapsed again.
I would like the layout to be saved (nodes which I expanded should stay
expanded). I don't change the ImageList, Scrollable, CheckBoxes or
StateImageList properties of the control between closing and reopening the
form.

any ideas?

thanks,
 
If you close the form, the pointer will point to nothing as the object will
be destroyed. What you could do however is hide the form with the Hide()
method. Later when you want to show it again just use the Show() method.

If you want however to retain the layout of the tree view even after you
close your application and turn off the computer, you can save the layout
structure in a local xml file like the example I gave earlier, otherwise if
you are not concerned about that you can just hide the form!

Adrian.
--
[Please mark my answer if it was helpful to you]




dshemesh said:
Just to make sure:
When I close the form, I still hold a pointer to it, so it's not totally
destroyed. When I reopen it, I call showDialog() for the same Form instance I
closed before.
Is this still the case?
--
dshemesh


Adrian Voicu said:
Once you close the form with the tree view, the form object and all the
objects it contained are destroyed and the memory is reused. What you could
do is write some code such that when the form is closing you save the state
of the tree view to a file like xml for example. You should keep it simple:
ie for each node in the tree check if it is expanded or collapsed and store
that in the xml file. When the form is opened, it should check this file to
see how to present the tree when it loads.

Adrian.
--
[Please mark my answer if it was helpful to you]




dshemesh said:
Hello,
I have a windows form with a TreeView control.
When I first open the tree all nodes are colapsed. Then, I expand some of
them and close the form.
When I call Form.showDialog() once again all nodes are collapsed again.
I would like the layout to be saved (nodes which I expanded should stay
expanded). I don't change the ImageList, Scrollable, CheckBoxes or
StateImageList properties of the control between closing and reopening the
form.

any ideas?

thanks,
 
ok.
Thanks!
--
dshemesh


Adrian Voicu said:
If you close the form, the pointer will point to nothing as the object will
be destroyed. What you could do however is hide the form with the Hide()
method. Later when you want to show it again just use the Show() method.

If you want however to retain the layout of the tree view even after you
close your application and turn off the computer, you can save the layout
structure in a local xml file like the example I gave earlier, otherwise if
you are not concerned about that you can just hide the form!

Adrian.
--
[Please mark my answer if it was helpful to you]




dshemesh said:
Just to make sure:
When I close the form, I still hold a pointer to it, so it's not totally
destroyed. When I reopen it, I call showDialog() for the same Form instance I
closed before.
Is this still the case?
--
dshemesh


Adrian Voicu said:
Once you close the form with the tree view, the form object and all the
objects it contained are destroyed and the memory is reused. What you could
do is write some code such that when the form is closing you save the state
of the tree view to a file like xml for example. You should keep it simple:
ie for each node in the tree check if it is expanded or collapsed and store
that in the xml file. When the form is opened, it should check this file to
see how to present the tree when it loads.

Adrian.
--
[Please mark my answer if it was helpful to you]




:

Hello,
I have a windows form with a TreeView control.
When I first open the tree all nodes are colapsed. Then, I expand some of
them and close the form.
When I call Form.showDialog() once again all nodes are collapsed again.
I would like the layout to be saved (nodes which I expanded should stay
expanded). I don't change the ImageList, Scrollable, CheckBoxes or
StateImageList properties of the control between closing and reopening the
form.

any ideas?

thanks,
 
dshemesh said:
Just to make sure:
When I close the form, I still hold a pointer to it, so it's not totally
destroyed. When I reopen it, I call showDialog() for the same Form instance I
closed before.
Is this still the case?

It depends. First, you don't hold a "pointer". You have a reference.
The reference remains valid as long as you have a variable assigned to
it. If you have custom managed data in your form class, they remain
valid as long as you keep the reference, assuming you haven't done
anything special to dispose that data (and you shouldn't have).

What may be released when the form is closed is the underlying Windows
objects (and in particular the window's handle, and the handles of any
controls in the window). This is otherwise described as disposing the form.

However, there's an exception to this: when you call Form.ShowDialog(),
closing the form does _not_ release the handles. That is, the Form is
not disposed. The main reason that this happens is so that you can
retrieve values from the controls in the form after returning from the
ShowDialog() method. But it also means that you can keep the form
around and reuse it. When using ShowDialog(), closing the form really
just hides it, just as the suggested workaround, and you can call
ShowDialog() again on the same form reference.

Hope that helps.

Pete
 
Back
Top