vb.net 如何将表单重置为其原始状态?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14889411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:40:44  来源:igfitidea点击:

How to reset a form to its original state?

vb.netwinforms

提问by hiren soni

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?

我正在开发一个 vb.net Windows 应用程序。在这个我有一个 Windows 窗体,它有几个文本框和几个控件。现在加载表单后,我在该表单中创建和删除了几个控件。我在表单上还有一个重置按钮,因此用户可以单击它并获得表单的原始设计。我试图调用 InitializeComponents() 方法。但它并不反映表格上的任何变化。如何在单击按钮时获得我的原始设计?

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.

好的,我需要修改我的问题。我不想重新设计我的所有控件,而只想重新设计我修改过的树视图。

回答by Cody Gray

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 onlystate of the form.

不会在任何地方跟踪 Windows 窗体的“原始”状态/设置。您在运行时所做的修改(添加、调整大小、删除等控件)都是实时发生的,并成为表单的当前和唯一状态。

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 defaultstate, 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 initialinitialization 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.

编辑:呼叫InitializeComponents()不是一个完美的解决方案。不叫它是有原因的ReinitializeComponents()。它旨在在首次创建和显示表单时进行初始初始化。它不是设计为由客户端代码在其他地方再次调用。无论如何,如果你这样做,它就会把事情搞砸。

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 Queueor Stackcollection to hold them after they are deleted, and pop them back out when you want to restore them to the TreeView.

如果树节点是你唯一担心保留的东西,为什么不声明一个类级成员来保存你删除的那些?您可以在删除后使用 aQueueStackcollection 之类的东西来保存它们,并在要将它们恢复到 TreeView 时将它们弹出。

回答by Ruben_PH

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

resetform(YourFormName)

回答by hiren soni

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.

我刚刚工作,现在已经完成了。我将一个变量作为树节点并分配了原始树视图的克隆。最后只需将该变量添加到我的树视图中。