练习.net WinForm开发(一):自定义分页控件(2)

VB.NET分页控件实现
本文介绍了一个自定义的VB.NET分页控件实现方案,该控件能够通过按钮点击事件来改变显示的页码,并能根据总记录数和每页记录数自动计算页数。同时提供了数据库查询代码示例,展示如何根据当前页码获取相应的数据。
ExpandedBlockStart.gif代码
Imports System.Data
Imports System.Data.OleDb
Imports System.Text.RegularExpressions
Public Class PagerWinControl
    
Dim strsql As String = ""
    
'总项数
    Private _TotalItem As Integer
    
Public Property TotalItem() As Integer
        
Get
            
Return _TotalItem
        
End Get
        
Set(ByVal value As Integer)
            _TotalItem 
= value
            
If value = 0 Then Exit Property
            ToolCount.Text 
= value
            
Dim result As Integer
            result 
= CInt(ToolCount.Text) Mod _PageCount
            
If result = 0 Then
                ToollblCount.Text 
= CInt(ToolCount.Text) \ _PageCount
            
Else
                ToollblCount.Text 
= CInt(ToolCount.Text) \ _PageCount + 1
            
End If
        
End Set
    
End Property
    
'每一页的数量
    Private _PageCount As Integer
    
Public Property PageCount() As Integer
        
Get
            
Return _PageCount
        
End Get
        
Set(ByVal value As Integer)
            _PageCount 
= value
        
End Set
    
End Property
    
'页码
    Private _PageNO As Integer
    
Public Property PageNO() As Integer
        
Get
            
Return _PageNO
        
End Get
        
Set(ByVal value As Integer)
            _PageNO 
= value
        
End Set
    
End Property
    
Private Sub TooLFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TooLFirst.Click
        Toolcurrent.Text 
= 1
        _PageNO 
= 1
        
RaiseEvent OnButtonClick(sender, e)
    
End Sub

    
Private Sub ToolPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolPrevious.Click
        
If Toolcurrent.Text <= 1 Then
            MessageBox.Show(
"已经是第一页了""五洲商慧提醒您", MessageBoxButtons.OK, MessageBoxIcon.Information)
            
Exit Sub
        
End If
        Toolcurrent.Text 
= CInt(Toolcurrent.Text) - 1
        _PageNO 
= CInt(Toolcurrent.Text)
        
RaiseEvent OnButtonClick(sender, e)
    
End Sub

    
Private Sub ToolNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolNext.Click
        
If Toolcurrent.Text >= ToollblCount.Text Then
            MessageBox.Show(
"已经是最后一页了""五洲商慧提醒您", MessageBoxButtons.OK, MessageBoxIcon.Information)
            
Exit Sub
        
End If
        Toolcurrent.Text 
= CInt(Toolcurrent.Text) + 1
        _PageNO 
= CInt(Toolcurrent.Text)
        
RaiseEvent OnButtonClick(sender, e)
    
End Sub

    
Private Sub ToolLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolLast.Click
        Toolcurrent.Text 
= ToollblCount.Text
        _PageNO 
= CInt(ToollblCount.Text)
        
RaiseEvent OnButtonClick(sender, e)
    
End Sub

    
Private Sub ToolGO_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolGO.Click
        
If TooltxtPage.Text = "" Then Exit Sub
        
If Not Regex.Match(TooltxtPage.Text, "^[1-9]\d*$").Success = True Then
            MessageBox.Show(
"请输入数字""五洲商慧提醒您", MessageBoxButtons.OK, MessageBoxIcon.Information)
            TooltxtPage.Text 
= ""
            
Exit Sub
        
End If
        
If CInt(TooltxtPage.Text) > CInt(ToollblCount.Text) Then
            MessageBox.Show(
"不存在此页""五洲商慧提醒您", MessageBoxButtons.OK, MessageBoxIcon.Information)
            TooltxtPage.Text 
= ""
            
Exit Sub
        
End If
        Toolcurrent.Text 
= TooltxtPage.Text
        _PageNO 
= CInt(Toolcurrent.Text)
        
RaiseEvent OnButtonClick(sender, e)
    
End Sub
    
Public Custom Event OnButtonClick As EventHandler
        
AddHandler(ByVal value As EventHandler)
            Events.AddHandler(
"ClickEvent", value)
        
End AddHandler

        
RemoveHandler(ByVal value As EventHandler)
            Events.RemoveHandler(
"ClickEvent", value)
        
End RemoveHandler

        
RaiseEvent(ByVal sender As ObjectByVal e As System.EventArgs)
            
CType(Events("ClickEvent"), EventHandler).Invoke(sender, e)
        
End RaiseEvent
    
End Event
End Class

调用代码

ExpandedBlockStart.gif代码
Private Sub PagerWinControl1_OnButtonClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PagerWinControl1.OnButtonClick
        
Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\pageing.mdb;")
        conn.Open()
        
Dim pageCount As Integer
        
Dim pageNO As Integer
        pageNO 
= PagerWinControl1.PageNO
        pageCount 
= PagerWinControl1.PageCount
        
If pageNO = 1 Then
            strsql 
= "select top " & pageCount & " * from student order by id asc"
        
Else
            strsql 
= "select top " & pageCount & " * from student where id not in(select top " & pageCount * pageNO - pageCount & " id from student) order by id asc"
        
End If
        
Dim ad As New OleDbDataAdapter(strsql, conn)
        
Dim dt As New DataTable
        ad.Fill(dt)
        DataGridView1.DataSource 
= dt
    
End Sub


 

不费话了,直接上代码了。自定义控件。

 

效果图。

 

希望哪个大哥给我改改。我水平太差。

 

转载于:https://www.cnblogs.com/Believeme/archive/2010/02/25/1673775.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值