hiren soni
hiren soni

Reputation: 174

How to reset a form to its original state?

I am working on a vb.net windows application. In this I have a windows form which has few textboxes and few controls on it. Now after loading the form I create and remove few controls in that form. I also have a reset button on the form so user can click on it an can get the original design of the form. I tried to call InitializeComponents() method. But It doesn't reflect any changes on the form. How to get my original design on button click?

Alright, I need to modify my question. I don't want to redesign my all the controls but only the treeview which I have modified.

Upvotes: 1

Views: 39705

Answers (3)

hiren soni
hiren soni

Reputation: 174

I just worked around and its done now. I took a variable as treenode and assigned with Clone of the original treeview. Finally just add that variable to my treeview.

Upvotes: 0

Cody Gray
Cody Gray

Reputation: 244692

The "original" state/settings of a Windows Form is not tracked anywhere. The modifications you make at runtime (adding, resizing, removing, etc. controls) all happen in real-time and become the current and only state of the form.

You could keep track of the original settings yourself and then write code to loop through all of the controls and restore them, but that would be a lot of work.

The easier solution is to just destroy the current form and replace it with a newly-created one. Of course, when a new form is created, it will have the default state, which is exactly what you want.


Edit: Calling InitializeComponents() is not a perfect solution. There's a reason it isn't called ReinitializeComponents(). It is designed to do the initial initialization when the form is first created and displayed. It was not designed to be called again elsewhere by client code. If you do so anyway, it's going to clobber things up.

If tree nodes are the only thing you're worried about preserving, why not declare a class-level member to hold the ones you delete? You can use something like a Queue or Stack collection to hold them after they are deleted, and pop them back out when you want to restore them to the TreeView.

Upvotes: 2

Ruben_PH
Ruben_PH

Reputation: 1812

Public Sub resetform(ByVal form_name as Form)  
    form_name.dispose  
    form_name.show
End Sub

call the resetform on your button click event

resetform(YourFormName)

Upvotes: 1

Related Questions