vb.net版的CheckBox Header Column Of DataGridView

最近在寫datagridview的CheckBox Header Column,網路上大部分都是c#版,所以我把他轉成vb.net版


ContractedBlock.gif ExpandedBlockStart.gif Code
Public Class LISDataGridViewExtender
    
Public Delegate Sub DataGridViewCheckBoxClickedHandler(ByVal state As Boolean)
    
Public Class DataGridViewCheckBoxHeaderCellEventArgs
        
Inherits EventArgs
        
Private _bChecked As Boolean
        
Public Sub New(ByVal bChecked As Boolean)
            _bChecked 
= bChecked
        
End Sub
        
Public ReadOnly Property Checked() As Boolean
            
Get
                
Return _bChecked
            
End Get
        
End Property
    
End Class
    
Public Class DataGridViewCheckBoxHeaderCell
        
Inherits DataGridViewColumnHeaderCell
        
Private checkBoxLocation As Point
        
Private checkBoxSize As Size
        
Private _checked As Boolean = False
        
Private _cellLocation As New Point()
        
Private _cbState As System.Windows.Forms.VisualStyles.CheckBoxState = System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal
        
Public Event OnCheckBoxClicked As DataGridViewCheckBoxClickedHandler
        
Protected Overridable Overloads Sub DataGridvewOnCheckBoxClicked(ByVal state As Boolean)
            
RaiseEvent OnCheckBoxClicked(state)
        
End Sub
        
Public Property IsChecked() As Boolean
            
Get
                
Return _checked
            
End Get
            
Set(ByVal value As Boolean)
                _checked 
= value
            
End Set
        
End Property
        
Public Sub New()
        
End Sub
        
Protected Overloads Overrides Sub Paint(ByVal graphics As System.Drawing.Graphics, ByVal clipBounds As System.Drawing.Rectangle, ByVal cellBounds As System.Drawing.Rectangle, ByVal rowIndex As IntegerByVal dataGridViewElementState As DataGridViewElementStates, ByVal value As Object, _
        
ByVal formattedValue As ObjectByVal errorText As StringByVal cellStyle As DataGridViewCellStyle, ByVal advancedBorderStyle As DataGridViewAdvancedBorderStyle, ByVal paintParts As DataGridViewPaintParts)
            
MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, dataGridViewElementState, value, _
            formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts)
            
Dim p As New Point()
            
Dim s As Size = CheckBoxRenderer.GetGlyphSize(graphics, System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal)
            p.X 
= cellBounds.Location.X + (cellBounds.Width / 2- (s.Width / 2)
            p.Y 
= cellBounds.Location.Y + (cellBounds.Height / 2- (s.Height / 2)
            _cellLocation 
= cellBounds.Location
            checkBoxLocation 
= p
            checkBoxSize 
= s
            
If _checked Then
                _cbState 
= System.Windows.Forms.VisualStyles.CheckBoxState.CheckedNormal
            
Else
                _cbState 
= System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal
            
End If
            CheckBoxRenderer.DrawCheckBox(graphics, checkBoxLocation, _cbState)
        
End Sub

        
Protected Overloads Overrides Sub OnMouseClick(ByVal e As DataGridViewCellMouseEventArgs)
            
Dim p As New Point(e.X + _cellLocation.X, e.Y + _cellLocation.Y)
            
If p.X >= checkBoxLocation.X AndAlso p.X <= checkBoxLocation.X + checkBoxSize.Width AndAlso p.Y >= checkBoxLocation.Y AndAlso p.Y <= checkBoxLocation.Y + checkBoxSize.Height Then
                _checked 
= Not _checked
                DataGridvewOnCheckBoxClicked(_checked)
                
Me.DataGridView.InvalidateCell(Me)
            
End If
            
MyBase.OnMouseClick(e)
        
End Sub
    
End Class
End Class


參考網址:
http://www.codeproject.com/KB/grid/CheckBoxHeaderCell.aspx

转载于:https://www.cnblogs.com/eacdpizzy/archive/2009/08/31/1557002.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值