Program,Life,Society.....

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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) Handles btnLast.Click
        MoveDataSourceByNavigation(ug, 
"tLast")
        RefreshNavigationBar()
    
End Sub


#
End Region

#Region 
"customer mothed code"
    Private Sub ProecssSize()
        
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
        Me.Width 
= I * sWidth
        Ar 
= Nothing
    
End Sub


    
Private Function GetCurrencyManager(ByVal datasource As ObjectAs CurrencyManager
        
Dim cmg As CurrencyManager

        cmg 
= Me.BindingContext.Item(datasource)
        
If IsNothing(cmg) Then Exit Function
        
Dim m As Integer = cmg.Count
        
If m <= 1 Then
            
Exit Function
        
End If
        
Return cmg
    
End Function


    
Private Sub MoveDataSourceByNavigation(ByRef ug As Windows.Forms.DataGrid, ByVal sKey As String)
        sKey 
= Trim(sKey)
        
If sKey = "" Then Exit Sub
        
If ug Is Nothing Then
            MessageBox.Show(
"No set UltraGrid on Navigation bar!""NavigationBar", MessageBoxButtons.OK, MessageBoxIcon.Stop, MessageBoxDefaultButton.Button1)
            
Exit Sub
        
End If
        
Dim cmg As CurrencyManager
        cmg 
= GetCurrencyManager(ug.DataSource)
        
If cmg Is Nothing Then Exit Sub

        
Dim Position, m As Integer
        m 
= cmg.Count
        Position 
= cmg.Position

        
Select Case sKey
            
Case "tFirst"
                Position = 0
                
If cmg.Position <> Position Then
                    cmg.Position 
= Position
                
End If
            
Case "tLast"
                Position = m - 1
                
If cmg.Position <> Position Then
                    cmg.Position 
= Position
                
End If
            
Case "tPrevious"
                Position = Position - 1
                
If Position < 0 Then Position = 0
                
If cmg.Position <> Position Then
                    cmg.Position 
= Position
                
End If
            
Case "tNext"
                Position = Position + 1
                
If Position > (m - 1Then Position = m - 1
                
If cmg.Position <> Position Then
                    cmg.Position 
= Position
                
End If
        
End Select

    
End Sub


#
End Region

#Region 
"RefreshNavigationBar mothed code"
    Public Sub RefreshNavigationBar()
        
If (ug Is NothingThen
            btnFirst.Enabled 
= False
            btnPrevious.Enabled 
= False
            btnLast.Enabled 
= False
            btnNext.Enabled 
= False
            
Exit Sub
        
End If
        
Dim cmg As CurrencyManager
        
If ug.DataSource Is Nothing Then Exit Sub
        cmg 
= GetCurrencyManager(ug.DataSource)
        
If cmg Is Nothing Then Exit Sub

        
Dim Position, m As Integer
        m 
= cmg.Count
        Position 
= cmg.Position


        
If m = 0 Then Exit Sub

        
Select Case Position
            
Case 0
                btnFirst.Enabled 
= False
                btnPrevious.Enabled 
= False
                btnLast.Enabled 
= True
                btnNext.Enabled 
= True
            
Case m - 1
                btnLast.Enabled 
= False
                btnNext.Enabled 
= False
                btnFirst.Enabled 
= True
                btnPrevious.Enabled 
= True
            
Case Else
                btnLast.Enabled 
= True
                btnNext.Enabled 
= True
                btnFirst.Enabled 
= True
                btnPrevious.Enabled 
= True
        
End Select
    
End Sub

#
End Region

End Class








posted on 2004-06-30 14:50  vuejs3  阅读(2367)  评论(2)    收藏  举报