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
vb.net: Using a Button on a Form to open another form, doesn't work
提问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 button1
on Form1
closes Form1
and opens Form2
.
(2)单击button1
在Form1
关闭Form1
和打开Form2
。
Solution A: If I use the following code:
解决方案A:如果我使用以下代码:
Dim oForm As New Form2
oForm.ShowDialog()
Me.Close()
Then Form1
will be under Form2
(Form1
still opens).
然后Form1
将在Form2
(Form1
仍然打开)下。
Solution B: If I use the following code:
解决方案 B:如果我使用以下代码:
Dim oForm As New Form2
oForm.Show()
Me.Close()
Then Form1
closes and Form2
opens, but Form1
is 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 Form1
and open Form2
when Button1
is pressed, right? This code is for Button1
in 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 Form
and open another Form
by pressing a Button
, always works for me
您想在按下时关闭Form1
和打开,对吗?此代码用于in 。你没有说太多我不知道你到底想做什么,但这是通过按 a来关闭和打开另一个的最简单的方法,总是对我有用Form2
Button1
Button1
Form1
Form
Form
Button
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Close()
Form2.Show()
End Sub
回答by user10663225
Just go to form2
and write the Form1.hide()
. I tried to close form1
but it closed my whole program.
只需去form2
写Form1.hide()
. 我试图关闭,form1
但它关闭了我的整个程序。
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Form1.Hide()
End Sub
End Class