vb.net:使用表单上的按钮打开另一个表单,不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24521845/
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 17:33:17  来源:igfitidea点击:

vb.net: Using a Button on a Form to open another form, doesn't work

vb.netforms

提问by user30643

I encounter the following scenarios when I try to use 2 forms. My workflow is as follows:

当我尝试使用 2 个表单时,我遇到了以下场景。我的工作流程如下:

(1) Load Form1.

(1) 负载Form1

(2) A click on button1on Form1closes Form1and opens Form2.

(2)单击button1Form1关闭Form1和打开Form2

Solution A: If I use the following code:

解决方案A:如果我使用以下代码:

Dim oForm As New Form2
oForm.ShowDialog()
Me.Close()

Then Form1will be under Form2(Form1still opens).

然后Form1将在Form2Form1仍然打开)下。

Solution B: If I use the following code:

解决方案 B:如果我使用以下代码:

Dim oForm As New Form2
oForm.Show()
Me.Close()

Then Form1closes and Form2opens, but Form1is not on the top layer.

然后Form1关闭和Form2打开,但Form1不在顶层。

I have looked through the solutions for this, most propose solution B, but for me, both solutions won't work the way I want. Can anybody tell me the reason?

我已经查看了解决方案,大多数人提出了解决方案 B,但对我来说,这两种解决方案都不会按我想要的方式工作。谁能告诉我原因?

回答by Tarak Bhavsar

Try

尝试

Dim oForm as New Form2

oForm.Show()

and on Load event of form2

和 form2 的 Load 事件

Form1.Hide()

回答by user3758070

Use form.bringtofront() if you want to see the opening Form in the front, I m little confused though about what you are trying to do

如果您想在前面看到打开的 Form,请使用 form.bringtofront(),不过我对您要执行的操作有点困惑

回答by Baumi

Try doing it this way:

尝试这样做:

Dim oForm As New Form2()

Me.Hide()
oForm.ShowDialog()

Me.Close()

回答by Louis van Tonder

I suspect your are building a log-in dialogue... if so, or something similar, try this..

我怀疑你正在建立一个登录对话......如果是这样,或者类似的东西,试试这个..

Open your main form first... (Form 2), have form2 showdialog(modally) form1... this will put form1 on top of form2.

首先打开您的主表单...(表单 2),让 form2 showdialog(模态)form1...这会将 form1 放在 form2 的顶部。

Add a property to form 1, that gets set depending on what happens there.. sucessfull login for instance.

向表单 1 添加一个属性,根据那里发生的情况设置该属性。例如,成功登录。

Close form 1 from its own methods... (after successful authentication), set the property before closing.

Close form 1 from its own methods...(认证成功后),关闭前设置属性。

On form2, read this property of form1, and then dispose form1, and decide what to do... if unsuccessful login, show the login form again, end app. If successful, just gracefully exit out of the method that showed form1. Your form 2 is now the only form open.

在form2上,读取form1的这个属性,然后处置form1,再决定怎么做……如果登录不成功,再次显示登录表单,结束app。如果成功,就优雅地退出显示 form1 的方法。您的表格 2 现在是唯一打开的表格。

Start with Form2

从 Form2 开始

Form2_load

Form2_load

dim f1 as new form1
f1.showdialog
if f1.someproperty = somevalue then

 ' do something here, for instance, pop the form again, if you did not get what you were lookign for...

end if

'gracefully let the function end and form2 is now the only open form..

'dispose of form1. form1's close call does not dispose it, because it was opened modally. (showdialog)

f1.dispose
f1 = nothing

in form1, depending on what you are doing, set the custom property and call me.close, this will exit the form, and run the next code in form2.

在form1中,根据你在做什么,设置自定义属性并调用me.close,这将退出表单,并在form2中运行下一个代码。

回答by MonauralMass371

try this:

尝试这个:

Dim oForm As New Form2

oForm.Show()

Me.Visible = False

You would to close your first form and this close your program. If you set him on invisible, he is not closed.

您将关闭您的第一个表单,这将关闭您的程序。如果您将他设置为隐形,则他不会关闭。

回答by user7777777

You wanted to close Form1and open Form2when Button1is pressed, right? This code is for Button1in Form1. You didn't tell much I don't know what exactly you are trying to do, but this is the EASIEST way to close a Formand open another Formby pressing a Button, always works for me

您想在按下时关闭Form1和打开,对吗?此代码用于in 。你没有说太多我不知道你到底想做什么,但这是通过按 a来关闭和打开另一个的最简单的方法,总是对我有用Form2Button1Button1Form1FormFormButton

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
     Me.Close()
     Form2.Show()
End Sub

回答by user10663225

Just go to form2and write the Form1.hide(). I tried to close form1but it closed my whole program.

只需去form2Form1.hide(). 我试图关闭,form1但它关闭了我的整个程序。

Public Class Form2
    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Form1.Hide()
    End Sub
End Class