vb.net版的CheckBox Header Column Of DataGridView

本文介绍了一个DataGridView CheckBoxHeaderCell的VB.NET实现方案,该方案允许用户在DataGridView的表头使用CheckBox,并提供了CheckBox被点击时触发事件的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在寫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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值