2015-02-06 94 views
1

我想製作一個程序,要求用戶登錄,如果輸入正確的用戶名和密碼,然後打開該用戶的窗體。我也只是現在做的方式是用戶名被存儲在一個變量,我想知道我怎麼可以在表單名稱使用的文字從變量,例如:Visual Basic,打開表格

用戶名=用戶1然後

FrmUser1。顯示

用戶名= JohnSmith對然後

FrmJohnSmith.show

這裏是從代碼的摘錄我迄今爲止:

Dim Path As String = "Account_File.text " 
    Dim Read_File() As String = File.ReadAllLines(Path) 
    Dim NoOfLines As Integer = Read_File.Length 
    Dim UserName(NoOfLines) As String 
    Dim Password(NoOfLines) As String 
    Dim LastNonEmpty As Integer = -1 
    Dim InputUserName As String = "" 
    Dim InputPassword As String = "" 

    If TxtUserName.Text = "" Then 
     MsgBox("Please enter a username.") 
     GoTo InvalidDetails 
    Else 
     InputUserName = TxtUserName.Text 
    End If 

    If TxtPassword.Text = "" Then 
     MsgBox("Please enter a Password.") 
     GoTo InvalidDetails 
    Else 
     InputPassword = TxtPassword.Text 
    End If 

    For i = 0 To NoOfLines 
     Dim SplitString() As String = Split(Read_File(i)) 
     For j As Integer = 0 To SplitString.Length - 1 
       If SplitString(j) <> "" Then 
       LastNonEmpty += 1 
       SplitString(LastNonEmpty) = SplitString(j) 
      End If 
     Next 
     ReDim Preserve SplitString(LastNonEmpty) 
     LastNonEmpty = -1 
     UserName(i) = SplitString(0) 
     Password(i) = SplitString(1) 
    Next 

    For i = 0 To NoOfLines 
     If UserName(i) = InputUserName And Password(i) = InputPassword Then 
      frm.show() 
     Else 
      MsgBox("Either the username or password is incorrect.") 
     End If 
    Next 
+0

我從VB6重新簽名到VB.Net(變量初始值設定器語法) – 2015-02-06 18:11:51

+0

謝謝我得到2混合有時 – Lazzer555 2015-02-06 18:32:48

+0

您是否有帳戶文件中的每個用戶的自定義窗體? – LarsTech 2015-02-06 18:45:24

回答

2

一些示例代碼:

Dim formname = "Form" + TxtUserName.Text 
    Dim typename = Me.GetType().Namespace + "." + formname 
    Dim type = Me.GetType().Assembly.GetType(typename) 
    If type IsNot Nothing Then 
     Dim form = CType(Activator.CreateInstance(type), Form) 
     form.Show() 
    End If 

即假設所有形式住在同一組件,並且具有相同的命名空間。請記住,您的客戶不太可能因爲每次獲得新員工而不得不打電話給您。或者就此而言,您會希望您提供用戶名和密碼,而不必提供Windows登錄已提供的安全保證。不要這樣做。

+0

這只是個人使用的練習程序,目前我正在參加大學學習軟件開發。 – Lazzer555 2015-02-06 19:12:48

相關問題