熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

VB.NET 中的組件開發源代碼剖析

2022-06-13   來源: .NET編程 

  
                                 
         登錄驗證組件                  
                                 
  
  
  Imports SystemSecurityCryptography
  Imports SystemText
  Imports SystemData
  Imports SystemDataSqlClient
  
  Public Class ValidatorClass Validator
    Inherits SystemComponentModelComponent
  
    Private username As String
    Private userpwd As String
  
    Public Property vUsername()Property vUsername() As String
      Get
        Return username
      End Get
      Set(ByVal Value As String)
        username = Value
      End Set
    End Property
  
    Public Property vUserpwd()Property vUserpwd() As String
      Get
        Return userpwd
      End Get
      Set(ByVal Value As String)
        userpwd = Value
      End Set
    End Property
  
    轉換為MD5
    Private Function convertMD()Function convertMD(ByVal pwd As String) As String
  
      Dim md As New MDCryptoServiceProvider
      Dim password As Byte() = (New ASCIIEncoding)GetBytes(pwd)
  
      轉換為哈希值Byte數組
      Dim mdByte As Byte() = mdComputeHash(password)
      Dim mdString As String = SystemBitConverterToString(mdByte)
      Dim mdString As String = (New ASCIIEncoding)GetString(mdByte)
      Return mdString
  
    End Function
  
    Public Function validate()Function validate() As Boolean
  
      連接到Users表
  
      Dim myConnection As New SqlConnection(server=localhost;database=TEST;Trusted_Connection=yes;user id=sa;password=;)
      Dim selectAdapter As New SqlDataAdapter(select * from Users where UserName= + username + + and Password= + convertMD(userpwd) + myConnection)
      Dim ds As New DataSet
      Try
        selectAdapterFill(ds Users)
        If (dsTables()RowsCount > ) Then
          Return True
        Else
          Return False
        End If
      Catch ep As SqlException
        MsgBox(連接數據庫出錯)
      Catch pp As Exception
        MsgBox(Oh發生了不可預料的事情在你身邊你死定了退出吧)
      End Try
    End Function
  #Region 組件設計器生成的代碼
  
    Public Sub New()Sub New(ByVal Container As SystemComponentModelIContainer)
      MyClassNew()
  
      WindowsForms 類撰寫設計器支持所必需的
      ContainerAdd(Me)
    End Sub
  
    Public Sub New()Sub New()
      MyBaseNew()
  
      該調用是組件設計器所必需的
      InitializeComponent()
  
      在 InitializeComponent() 調用之後添加任何初始化
  
    End Sub
  
    組件重寫 dispose 以清理組件列表
    Protected Overloads Overrides Sub Dispose()Sub Dispose(ByVal disposing As Boolean)
      If disposing Then
        If Not (components Is Nothing) Then
          componentsDispose()
        End If
      End If
      MyBaseDispose(disposing)
    End Sub
  
    組件設計器所必需的
    Private components As SystemComponentModelIContainer
  
    注意: 以下過程是組件設計器所必需的
    可以使用組件設計器修改此過程
    不要使用代碼編輯器修改它
    <SystemDiagnosticsDebuggerStepThrough()> Private Sub InitializeComponent()Sub InitializeComponent()
      components = New SystemComponentModelContainer
    End Sub
  
  #End Region
  
  End Class
  
  簡介組件其實是一段可以重用的代碼通過遵循IComponent接口的標准來實現一個組件所以有組件都是派生於Component類由Component類來實現IComponent接口在組件中應正確使用函數的訪問級別來控制外部對其的訪問限制
  
  只要有足夠的權限就可以將組件放到自己的程序中而不用擔心組件會產生多大的錯誤因為組件已經經過測試的比如說可以把一段登錄的程序做成一個組件或者把經常使用到的一些功能也做成組件這樣就可以減少開發中的錯誤也可以縮短開發時間組件之間也可以互相套用如一個組件引用另一個組件都是沒問題但要先在Add Reference中添加對組件的引用NET中是通過把組件放在程序集中來實現的程序集中存放著這些組件所依賴的文件信息和所在路徑因此CLR就可以通過這些信息來確定組件所需要的其他程序集的位置
  
  ( 另外在組件設計過程中應好好利用接口來設計組件)
  
  在VS中創建組件選建一個Project再從模板中選Class LibraryOK接著再從Project菜單中Add Component到些為止組件的一個框架就呈現在眼前平台自動繼承了Component類和構造函數可以刪除原先創建類庫時自動生成的Class看應用的需要接著就可以在組件類裡寫要實現的功能最後從Build(生成)菜單中選擇Build Solution(生成解決方案)來生成組件如果生成成功的話到應用程序的BIN目錄下會看到一個DLL文件
  
  引用組件只要在Solution Explorer窗口中添加對DLL的Reference就可以了
  
  Imports loginValidator
  Imports SystemData
  Imports SystemDataSqlClient
  
  Public Class loginFormClass loginForm
    Inherits SystemWindowsFormsForm
  
  #Region Windows 窗體設計器生成的代碼
  
    Public Sub New()Sub New()
      MyBaseNew()
  
      該調用是 Windows 窗體設計器所必需的
      InitializeComponent()
  
      在 InitializeComponent() 調用之後添加任何初始化
  
    End Sub
  
    窗體重寫 dispose 以清理組件列表
    Protected Overloads Overrides Sub Dispose()Sub Dispose(ByVal disposing As Boolean)
      If disposing Then
        If Not (components Is Nothing) Then
          componentsDispose()
        End If
      End If
      MyBaseDispose(disposing)
    End Sub
  
    Windows 窗體設計器所必需的
    Private components As SystemComponentModelIContainer
  
    注意: 以下過程是 Windows 窗體設計器所必需的
    可以使用 Windows 窗體設計器修改此過程
    不要使用代碼編輯器修改它
    Friend WithEvents lblUserPwd As SystemWindowsFormsLabel
    Friend WithEvents lblUserName As SystemWindowsFormsLabel
    Friend WithEvents txtUserName As SystemWindowsFormsTextBox
    Friend WithEvents txtUserPwd As SystemWindowsFormsTextBox
    Friend WithEvents btnSubmit As SystemWindowsFormsButton
    Friend WithEvents btnExit As SystemWindowsFormsButton
    Friend WithEvents Label As SystemWindowsFormsLabel
    Friend WithEvents Label As SystemWindowsFormsLabel
    Friend WithEvents btnCancel As SystemWindowsFormsButton
    Friend WithEvents Label As SystemWindowsFormsLabel
    <SystemDiagnosticsDebuggerStepThrough()> Private Sub InitializeComponent()Sub InitializeComponent()
      Dim resources As SystemResourcesResourceManager = New SystemResourcesResourceManager(GetType(loginForm))
      MelblUserPwd = New SystemWindowsFormsLabel
      MelblUserName = New SystemWindowsFormsLabel
      MetxtUserName = New SystemWindowsFormsTextBox
      MetxtUserPwd = New SystemWindowsFormsTextBox
      MebtnSubmit = New SystemWindowsFormsButton
      MebtnExit = New SystemWindowsFormsButton

From:http://tw.wingwit.com/Article/program/net/201311/13907.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.