Eric
Eric

Reputation: 248

How to manage the multiple forms in VB.NET

I have five forms in 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.

  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()

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

 Private frm As OpenForm 

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

And when user click 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.


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.

Thank for your help.

Upvotes: 0

Views: 8180

Answers (1)

Steve
Steve

Reputation: 216361

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

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.

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.

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.

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.

Upvotes: 1

Related Questions