2015-08-22 175 views
0

我想做一個登錄窗口和一個主窗口。問題是當我關閉登錄窗體並顯示主窗口時,整個程序停止。VB.NET - 使用多種形式

登錄表單:

Imports System.IO 
Imports System.Text 
Imports System.Net 

Public Class frmLogin 

    Dim address As String = "http://puu.sh/jKJ**Zq/d613de****29.txt" 
    Dim client As WebClient = New WebClient() 

    Private Sub frmLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
     'frmMain.Close() 
    End Sub 

    Private Sub frmLogin_Close(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.FormClosed 
     frmMain.Show() 
    End Sub 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 

     If attemptLogin() Then 
      MsgBox("Login Successful") 
      Me.Close() 
      'frmMain.Show() 
     Else 
      MsgBox("Username or password is incorrect") 
     End If 

    End Sub 

    Private Function attemptLogin() 
     Dim reader As StreamReader = New StreamReader(client.OpenRead(address)) 

     Dim line As String 
     Dim username As String 
     Dim password As String 

     line = reader.ReadLine() 

     Do While Not line Is Nothing 

      username = line.Split(":")(0) 
      password = line.Split(":")(1) 

      If (username = TextBox1.Text And password = TextBox2.Text) Then 
       Return True 
      End If 

      line = reader.ReadLine() 

     Loop 

     reader.Close() 
     client.Dispose() 

     Return False 

    End Function 

    Private Function Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 

     Dim client As WebClient = New WebClient() 
     Dim reader As StreamReader = New StreamReader(client.OpenRead(address)) 
     Dim line As String 

     line = reader.ReadLine 

     Do While Not line Is Nothing 
      line = reader.ReadLine() 
     Loop 

     reader.Close() 
     Dim writer As StreamWriter = New StreamWriter(client.OpenRead(address)) 
     writer.Write(TextBox1.Text & ":" & TextBox2.Text) 

     writer.Close() 
     client.Dispose() 

     Return False 

    End Function 

End Class 

主窗口:

Public Class frmMain 

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load 
     'frmMain.Close() 
    End Sub 

    Private Sub frmMain_closed(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.FormClosing 

    End Sub 

End Class 

我是相當新的VB.NET很抱歉的是,缺乏知識/醜編碼。我習慣用Java編程GUI。

+0

轉到項目屬性,並將關機模式更改爲「當最後一個窗體關閉」 – Plutonix

+0

也許你可以在這裏得到你的問題提示http://stackoverflow.com/questions/4976380/net-end-vs-form- close-vs-application-exit-cleaner-way-to-close-ones-app – Steve

回答

0

您需要將'關閉模式'設置爲'最後一個窗體關閉時'。

您可以將我的項目,然後應用(第一個選項卡)點擊找到此設置。 然後您可以在底部找到該設置。

+0

哦,我的壞,我不知道我能做到這一點。另一個問題,是在程序開始時創建的表單,還是我在程序啓動時選擇顯示的表單? – Man16

+0

只是你選擇的表單。但是,當第一個窗體加載時,您可以使用frmSecond.ShowDialog()輕鬆打開第二個窗體。 – brco

+0

或frmSecond.Show()在你的情況下... – brco