📄 adduserform.vb
字号:
'---------------------------------------------------------------------
' This file is part of the Microsoft .NET Framework SDK Code Samples.
'
' Copyright (C) Microsoft Corporation. All rights reserved.
'
' This source code is intended only as a supplement to Microsoft
' Development Tools and/or on-line documentation. See these other
' materials for detailed information regarding Microsoft code samples.
'
' THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
' KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
' IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
' PARTICULAR PURPOSE.
'---------------------------------------------------------------------
Imports System.Text.RegularExpressions
Public Class AddUserForm
Inherits System.Windows.Forms.Form
Public Sub New(ByVal dl As DataLayer)
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
m_DataLayer = dl
End Sub
Private m_DataLayer As DataLayer
Private m_ResourceManager As New Resources.ResourceManager("TeamVision.Localize", System.Reflection.Assembly.GetExecutingAssembly())
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
If IsFormValid() Then
Dim newUserInfo As New Business.User
Dim facade As New Database.UserFacade
With newUserInfo
.Name = txtUserName.Text.Trim()
.Password = txtPassword.Text ' TODO: Add Encryption here
.FullName = txtUserFullName.Text.Trim()
.Email = txtUserEmail.Text.Trim()
.IsAdministrator = cbAdministrator.Checked
.IsAccountLocked = cbLocked.Checked
End With
'if we were able to add the user to the database, update our local data
If Not UserExist(newUserInfo) Then
newUserInfo.UserID = facade.InsertUser(newUserInfo)
Me.DialogResult = System.Windows.Forms.DialogResult.OK
Me.Close()
Else
MessageBox.Show(m_ResourceManager.GetString("That_user_name_is_already_taken"))
txtUserName.Focus()
End If
Else
MessageBox.Show(m_ResourceManager.GetString("Unable_to_add_user_at_this_time"))
Me.DialogResult = System.Windows.Forms.DialogResult.None
End If
End Sub
Private Function UserExist(ByVal NewUser As Business.User) As Boolean
Dim facade As New Database.UserFacade
Dim sql As String = Database.Columns.User.Name + "=""" + NewUser.Name + """"
Dim Users() As Business.User = Nothing
Users = facade.GetUserArrayWhere(sql)
If Users Is Nothing Then Return False
Return True
End Function
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
Me.Close()
End Sub
Private Function IsFormValid() As Boolean
'check the fields for valid data and
'display message boxes if neccessary
If txtUserName.Text.IndexOf(" ") > -1 Then
MessageBox.Show(m_ResourceManager.GetString("User_name_may_not_contain_spaces"))
Return False
ElseIf txtUserName.Text.Length < 3 Then
MessageBox.Show(m_ResourceManager.GetString("User_name_must_be_between"))
Return False
ElseIf txtPassword.Text.IndexOf(" ") > -1 Then
MessageBox.Show(m_ResourceManager.GetString("Password_may_not_contain_spaces"))
Return False
ElseIf txtPassword.Text.Length < 3 Then
MessageBox.Show(m_ResourceManager.GetString("Password_must_be_between"))
Return False
ElseIf txtPassword.Text <> txtConfirmPassword.Text Then
MessageBox.Show(m_ResourceManager.GetString("Password_fields_do_not_match"))
Return False
ElseIf txtUserFullName.Text.Trim().Length < 3 Then
MessageBox.Show(m_ResourceManager.GetString("Full_name_must_be_between"))
Return False
End If
Dim r As New Regex("\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", RegexOptions.IgnoreCase)
If Not r.IsMatch(txtUserEmail.Text) Then
MessageBox.Show(m_ResourceManager.GetString("Email_address_is_not_valid"))
Return False
End If
Return True
End Function
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -