现在的位置: 首页 > 综合 > 正文

100% .NET Control_使用CurrencyManager 创建一个导航条来控制DatagGrid的XNavBar控件(VB.NET).

2012年12月13日 ⁄ 综合 ⁄ 共 8256字 ⁄ 字号 评论关闭

Begin MSDN:
要使 Windows 窗体数据绑定成为可能,必须有数据提供程序和使用者。
从提供程序这一方最容易接近 Windows 窗体数据绑定的结构。
绑定一个 Windows 窗体及其控件的开始在于数据的“提供程序”。
不仅可以绑定到传统的数据源,而且可以绑定到几乎任何包含数据的结构。
实现 IList 接口的数组、集合或者 ADO .NET 中的数据结构之一都是合适的数据源。
Windows 窗体控件绑定到的任何数据源将都具有一个关联的 CurrencyManager 对象。
CurrencyManager 对象跟踪位置,另外监控对该数据源的绑定。
对于当前绑定到的每个离散数据源,在窗体上都有一个 CurrencyManager 对象。
如果窗体上的所有控件都绑定到一个源(例如,如果几个 TextBox控件绑定到同一数据表),
那么它们将共享同一个 CurrencyManager。但是,有时窗体上的控件将绑定到不同的源。
在这种情况下,窗体上有多个 CurrencyManager 对象,每个都跟踪控件正在使用哪个记录或数据元素。
另外,每个 Windows 窗体都有一个 BindingContext 对象。
BindingContext 对象跟踪窗体上的所有 CurrencyManager 对象。
因此,任何具有数据绑定控件的 Windows 窗体都至少有一个跟踪一个
(或多个)CurrencyManager 对象的 BindingContext 对象,而每个
CurrencyManager 对象又跟踪一个数据源。如果使用容器控件,
如 GroupBox、Panel 或 TabControl 来包含数据绑定控件,
则可以只为该容器控件及其控件创建一个 BindingContext。这使窗体的各个部分都由它
自己的 CurrencyManager 对象来管理。
End MSDN

XNavBar Code:(100%原创)

Public Class XNavBar
    
Inherits System.Windows.Forms.UserControl

    
Private ug As Windows.Forms.DataGrid


#Region 
" Windows Form Designer generated code "

    Public Sub New()
        
MyBase.New()

        
'This call is required by the Windows Form Designer.
        InitializeComponent()

        
'Add any initialization after the InitializeComponent() call

    
End Sub


    
'UserControl1 overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        
If disposing Then
            
If Not (components Is NothingThen
                components.Dispose()
            
End If
        
End If
        
MyBase.Dispose(disposing)
    
End Sub


    
'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    
'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents btnFirst As System.Windows.Forms.Button
    
Friend WithEvents btnPrevious As System.Windows.Forms.Button
    
Friend WithEvents btnNext As System.Windows.Forms.Button
    
Friend WithEvents btnLast As System.Windows.Forms.Button
    
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(XNavBar))
        Me.btnLast 
= New System.Windows.Forms.Button
        Me.btnNext 
= New System.Windows.Forms.Button
        Me.btnPrevious 
= New System.Windows.Forms.Button
        Me.btnFirst 
= New System.Windows.Forms.Button
        Me.SuspendLayout()
        
'
        'btnLast
        '
        Me.btnLast.BackColor = System.Drawing.SystemColors.Control
        Me.btnLast.ForeColor 
= System.Drawing.SystemColors.Control
        Me.btnLast.Image 
= CType(resources.GetObject("btnLast.Image"), System.Drawing.Image)
        Me.btnLast.Location 
= New System.Drawing.Point(720)
        Me.btnLast.Name 
= "btnLast"
        Me.btnLast.Size = New System.Drawing.Size(2424)
        Me.btnLast.TabIndex 
= 3
        
'
        'btnNext
        '
        Me.btnNext.BackColor = System.Drawing.SystemColors.Control
        Me.btnNext.ForeColor 
= System.Drawing.SystemColors.Control
        Me.btnNext.Image 
= CType(resources.GetObject("btnNext.Image"), System.Drawing.Image)
        Me.btnNext.Location 
= New System.Drawing.Point(480)
        Me.btnNext.Name 
= "btnNext"
        Me.btnNext.Size = New System.Drawing.Size(2424)
        Me.btnNext.TabIndex 
= 2
        
'
        'btnPrevious
        '
        Me.btnPrevious.BackColor = System.Drawing.SystemColors.Control
        Me.btnPrevious.ForeColor 
= System.Drawing.SystemColors.ControlLight
        Me.btnPrevious.Image 
= CType(resources.GetObject("btnPrevious.Image"), System.Drawing.Image)
        Me.btnPrevious.Location 
= New System.Drawing.Point(240)
        Me.btnPrevious.Name 
= "btnPrevious"
        Me.btnPrevious.Size = New System.Drawing.Size(2424)
        Me.btnPrevious.TabIndex 
= 1
        
'
        'btnFirst
        '
        Me.btnFirst.BackColor = System.Drawing.SystemColors.Control
        Me.btnFirst.ForeColor 
= System.Drawing.SystemColors.Control
        Me.btnFirst.Image 
= CType(resources.GetObject("btnFirst.Image"), System.Drawing.Image)
        Me.btnFirst.Location 
= New System.Drawing.Point(00)
        Me.btnFirst.Name 
= "btnFirst"
        Me.btnFirst.Size = New System.Drawing.Size(2424)
        Me.btnFirst.TabIndex 
= 0
        
'
        'XNavBar
        '
        Me.BackColor = System.Drawing.SystemColors.Control
        Me.Controls.Add(Me.btnLast)
        Me.Controls.Add(Me.btnPrevious)
        Me.Controls.Add(Me.btnFirst)
        Me.Controls.Add(Me.btnNext)
        Me.Name 
= "XNavBar"
        Me.Size = New System.Drawing.Size(9624)
        Me.ResumeLayout(
False)

    
End Sub


#
End Region

#Region 
"Property code"

    Public Property NBFirst() As Boolean
        
Get
            
Return btnFirst.Visible
        
End Get
        
Set(ByVal Value As Boolean)
            btnFirst.Visible 
= Value
            ProecssSize()
        
End Set
    
End Property


    
Public Property NBPrior() As Boolean
        
Get
            
Return btnPrevious.Visible
        
End Get
        
Set(ByVal Value As Boolean)
            btnPrevious.Visible 
= Value
            ProecssSize()
        
End Set
    
End Property


    
Public Property NBLast() As Boolean
        
Get
            
Return btnLast.Visible
        
End Get
        
Set(ByVal Value As Boolean)
            btnLast.Visible 
= Value
            ProecssSize()
        
End Set
    
End Property


    
Public Property NBNext() As Boolean
        
Get
            
Return btnNext.Visible
        
End Get
        
Set(ByVal Value As Boolean)
            btnNext.Visible 
= Value
            ProecssSize()
        
End Set
    
End Property


    
Public Property DataGrid() As Windows.Forms.DataGrid
        
Get
            
Return ug
        
End Get
        
Set(ByVal Value As Windows.Forms.DataGrid)
            ug 
= Value
            
If Value Is Nothing Then
                btnFirst.Enabled 
= False
                btnPrevious.Enabled 
= False
                btnLast.Enabled 
= False
                btnNext.Enabled 
= False
            
Else
                btnFirst.Enabled 
= True
                btnPrevious.Enabled 
= True
                btnLast.Enabled 
= True
                btnNext.Enabled 
= True
            
End If

        
End Set
    
End Property


#
End Region

#Region 
"System.EventArgs code"
    Private Sub NavigateBar_Resize(ByVal sender As ObjectByVal e As System.EventArgs) Handles MyBase.Resize

        
If Me.Width < 96 Then Me.Width = 96
        
If Me.Height < 24 Then Me.Height = 24
        
Dim Ar As New ArrayList
        Ar.Clear()
        Ar.Add(btnFirst)
        Ar.Add(btnPrevious)
        Ar.Add(btnNext)
        Ar.Add(btnLast)
        
Dim c As Control
        
Dim I, J As Integer
        
Dim TotalWidth As Integer = Me.Width
        
For Each c In Ar
            
Dim bt As Button
            bt 
= CType(c, Button)
            
If bt.Visible Then
                I 
= I + 1
            
End If
        
Next
        
Dim sWidth As Integer = TotalWidth / I
        
For Each c In Ar
            
Dim bt As Button
            bt 
= CType(c, Button)
            
If bt.Visible Then
                bt.
Left = J * sWidth
                bt.Width 
= sWidth
                bt.Height 
= Me.Height
                J 
= J + 1
            
End If
        
Next
        Ar 
= Nothing

    
End Sub


    
Private Sub NavigateBar_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load
        RefreshNavigationBar()
    
End Sub



    
Private Sub btnFirst_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles btnFirst.Click
        MoveDataSourceByNavigation(ug, 
"tFirst")
        RefreshNavigationBar()
    
End Sub


    
Private Sub btnPrevious_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles btnPrevious.Click
        MoveDataSourceByNavigation(ug, 
"tPrevious")
        RefreshNavigationBar()
    
End Sub


    
Private Sub btnNext_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles btnNext.Click
        MoveDataSourceByNavigation(ug, 
"tNext")
        RefreshNavigationBar()
    
End Sub


    
Private Sub btnLast_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) 

抱歉!评论已关闭.