如何打开关闭旧表单的新实例 [英] How to open a new instance of a form closing the old one

查看:63
本文介绍了如何打开关闭旧表单的新实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了两个表格



1. form1

2. form2



i made form1的ismdicontainer是真的

i在form1中创建了一个按钮我写了

以下代码打开form2



dim x as new form2

x.showDialog()



在form2中我设计了一个计算器我已经在该按钮(新计算)中添加了一个新按钮

我已经写了创建计算器的新实例

如下



dim x as new form2

x.showDialog()



它工作正常但它是没有关闭旧实例。

如果我这样做超过10或15次它会显示过载错误。

我想要的是如果我点击新的计算按钮它应该关闭旧的实例和开始新的实例



我尝试了什么:



i试了以下代码,但它没有关闭旧实例



me.close()

dim x as new form2

x.showDialog()



我也试过以下代码仍然没有关闭旧实例





dim x as new form2

x.showDialog()

me.close()

I have created two forms

1. form1
2. form2

i made form1 's ismdicontainer is true
i created a button in form1 and in that button i wrote
following codes to open form2

dim x as new form2
x.showDialog()

In form2 I have designed a calculator and i have put a new button
in that button (new calculation) i have written to create a new instance of the calculator
as follows

dim x as new form2
x.showDialog()

it is working fine but it is not closing the old instance.
if i do it more than 10 or 15 times it shows overload error.
what i want is that if i click on new calculation button it should close the old instance and start the new instance

What I have tried:

i have tried following codes but it is not closing old instance

me.close()
dim x as new form2
x.showDialog()

also i have tried following codes still it is not closing the old instance


dim x as new form2
x.showDialog()
me.close()

推荐答案

你无法做你想做的事。 Windows以层次结构打开,而不是链。



显示的第一个表单是窗口树的顶部。关闭它会关闭整个应用程序。



父窗口可以隐藏自己。如果有儿童窗户,它就无法自行关闭。

You cannot do what you want. Windows are opened in a hierarchy, not a chain.

The first form shown is the top of the window tree. Closing it closes your entire application.

The parent window can HIDE itself. It cannot close itself if there are child windows.
GrandParent Window
        |
        |
  Parent Window
        |
        |
   Child Window





如果您希望新的子窗口替换父窗口。父窗口不能创建子窗口。 GRANDPARENT窗口应创建新窗口以替换父窗口。



If you want the new child window to replace the parent. The parent window must not create the child window. The GRANDPARENT window should create the new window to replace the parent window.

       GrandParent Window
               |
     ----------+------------------
     |                           |
Parent Window          New Parent Window





哦!使用ShowDialog()显示的任何表单必须在其引用超出范围之前调用Dispose()。



Oh! And any form you show with ShowDialog() MUST have Dispose() called on it before its reference goes out of scope.

Dim x As New Form2
result = x.ShowDialog()
x.Dispose()


insted of me.close()

尝试

insted of me.close()
try
Me.Hide()


这篇关于如何打开关闭旧表单的新实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆