vb.net VB.NET中如何管理多个表单

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

How to manage the multiple forms in VB.NET

vb.netforms

提问by Ericton

I have five forms in vb.net.

我在 vb.net 中有五种形式。

  1. Welcome Form
  2. Home Form
  3. LogIn Form
  4. Work1 Form
  5. Work2 Form

And one Enumeration:

和一个枚举:

  Public Enum OpenForm
      F_WORK1=1
      F_WORK2=2
  End Enum

And I declare all its object in Module:

我在模块中声明了它的所有对象:

  Public frmHome As Home
  Public frmLogin As LogIn
  Public frmWork1 As Work1
  Public frmWork2 As Work2

And then, when Welcome Form load:

然后,当欢迎表单加载时:

 Me.Hide()
 frmHome = New Home
 frmHome.show()

And in Home Form, there are two buttons. And Home Form, I don't close it. I keep open it until the application close.

在 Home Form 中,有两个按钮。还有 Home Form,我不关闭它。我一直打开它直到应用程序关闭。

  1. First button, use to open the Work1 Form. And when user click it, it will open LogIn Form and user must input username and password:

    frmLogin = New LogIn(OpenForm.F_WORK1)

    frmLogin.showDialog()

  2. Second button, use to open the Work2 Form. and when user click it, it will open LogIn Form and user must input username and password:

    frmLogin = new LogIn(OpenForm.F_WORK2)

    frmLogin.showDialog()

  1. 第一个按钮,用于打开 Work1 表单。当用户点击它时,它会打开登录表单,用户必须输入用户名和密码:

    frmLogin = 新登录(OpenForm.F_WORK1)

    frmLogin.showDialog()

  2. 第二个按钮,用于打开 Work2 窗体。当用户点击它时,它会打开登录表单,用户必须输入用户名和密码:

    frmLogin = 新登录(OpenForm.F_WORK2)

    frmLogin.showDialog()

In LogIn Form, there is one button named btnLogin. And here is the LogIn Form code:

在登录表单中,有一个名为 btnLogin 的按钮。这是登录表单代码:

 Private frm As OpenForm 

 Public Sub New(f as OpenForm)
    InitailizeComponent()
    frm = f
 End Sub

And when user click btnLogin:

当用户单击 btnLogin 时:

 If frm=OpenForm.F_WORK1 Then
    frmWork1 = New Work1()
    frmWork1.showDialog(frmHome)
    frmLogin.Close()
 ElseIf frm=OpenForm.F_WORK2 Then
    frmWork2 = New Work2()
    frmWork2.showDialog(frmHome)
    frmLogin.Close()
 End If

Then Work1 or Wor2 Form opened. But LogIn Form doesn't close.

然后 Work1 或 Wor2 Form 打开。但是登录表单没有关闭。



The problems:

问题:

  1. I want to close LogIn Form and open Work1 or Work2 Form after user click btnLogin.
  2. And when user minimize the Work1 or Work2 Form, I want the Home Form also minimize. And when user mouse move the taskbar, I want it preview which Form that I opened.
  3. Each Form(Home Form, Work1 Form and Work2 Form) contain a lot of control that make my user interface very slow and not smooth. So I want it loads its contain first and then visible the Form.
  1. 我想在用户单击 btnLogin 后关闭登录表单并打开 Work1 或 Work2 表单。
  2. 当用户最小化 Work1 或 Work2 Form 时,我希望 Home Form 也最小化。当用户鼠标移动任务栏时,我希望它预览我打开的表单。
  3. 每个 Form(Home Form、Work1 Form 和 Work2 Form)都包含很多控件,这使我的用户界面非常缓慢且不流畅。所以我希望它首先加载它的包含,然后看到表单。

Thank for your help.

感谢您的帮助。

回答by Steve

Your Home Form knows what is the right form to open after the login is successuful.
Let it decide which form to Open

您的 Home Form 知道登录成功后打开的正确表单是什么。
让它决定打开哪个表单

in the first button

在第一个按钮中

frmLogin = New LogIn()
if DialogResult.OK = frmLogin.showDialog() then
    frmWork1 = New Work1()
    frmWork1.showDialog(frmHome)
end if

in the second button

在第二个按钮中

frmLogin = New LogIn()
if DialogResult.OK = frmLogin.showDialog() then
    frmWork2 = New Work2()
    frmWork2.showDialog(frmHome)
end if

Your actual code doesn't work because ShowDialog stops the execution of the subsequent code till the form opened by the ShowDialog close. It is called a Modal Form.

您的实际代码不起作用,因为 ShowDialog 停止执行后续代码,直到 ShowDialog 打开的窗体关闭。它被称为模态形式。

Forget to say to set the DialogResult property of the btnLogin to DialogResult.OK.
This will tell to the framework to automatically close your Login form when the user press that button.
Instead, if the validation of the user fails, set the frmLogin.DialogResult property to DialogResult.None to block the automatic form closing.

忘了说把btnLogin的DialogResult属性设置为DialogResult.OK。
这将告诉框架在用户按下该按钮时自动关闭您的登录表单。
相反,如果用户验证失败,请将 frmLogin.DialogResult 属性设置为 DialogResult.None 以阻止自动关闭表单。

For the second question, it should already work in this way. When you call ShowDialog method and pass the owner form, the the two forms are binded togheter and minimizing the fmrWork1/2 should also minimize the frmHome. The visualization in the Taskbar is automatic unless you have set the form ShowInTaskbar property to false.

对于第二个问题,它应该已经以这种方式工作了。当您调用 ShowDialog 方法并传递所有者表单时,这两个表单绑定在一起,最小化 fmrWork1/2 也应该最小化 frmHome。除非您将表单 ShowInTaskbar 属性设置为 false,否则任务栏中的可视化是自动的。

The third question is difficult to answer. A possible solution is to divide your input control in tab pages using the tab control. Each page should contain a user control with the appropriate inputs for that page. Every page but the first will be leaved empty and only when the user changes the selected page you load the corresponding user control. As you can see this is a very complex topic that cannot fully explained without a bit of research on your side.

第三个问题很难回答。一种可能的解决方案是使用选项卡控件在选项卡页中划分输入控件。每个页面都应包含一个用户控件,该控件具有该页面的相应输入。除了第一个页面之外的每个页面都将留空,并且只有当用户更改所选页面时才会加载相应的用户控件。如您所见,这是一个非常复杂的主题,如果您不进行一些研究,就无法完全解释。