VB中添加进度条列
来源:互联网 发布:filco 蓝牙mac 编辑:程序博客网 时间:2024/06/07 03:13
目的:实现如下图所示效果:
写实现进度条的类,继承DataGridViewTextBoxColumn类,代码如下:
Imports SystemImports System.DrawingImports System.Windows.FormsPublic Class DataGridViewPrassBar Public Class DataGridViewProgressBarColumn Inherits DataGridViewTextBoxColumn Public Sub New() Me.CellTemplate = New DataGridViewProgressBarCell() End Sub Public Overrides Property CellTemplate() As DataGridViewCell Get Return MyBase.CellTemplate End Get Set(ByVal value As DataGridViewCell) If Not TypeOf value Is DataGridViewProgressBarCell Then Throw New InvalidCastException("请指定DataGridViewProgressBarCell") End If MyBase.CellTemplate = value End Set End Property Public Property Maximum() As Integer Get Return CType(Me.CellTemplate, DataGridViewProgressBarCell).Maximum End Get Set(ByVal value As Integer) If Me.Maximum = value Then Return End If CType(Me.CellTemplate, DataGridViewProgressBarCell).Maximum = value If Me.DataGridView Is Nothing Then Return End If Dim rowCount As Integer = Me.DataGridView.RowCount Dim i As Integer For i = 0 To rowCount - 1 Dim r As DataGridViewRow = Me.DataGridView.Rows.SharedRow(i) CType(r.Cells(Me.Index), DataGridViewProgressBarCell).Maximum = value Next i End Set End Property Public Property Mimimum() As Integer Get Return CType(Me.CellTemplate, DataGridViewProgressBarCell).Mimimum End Get Set(ByVal value As Integer) If Me.Mimimum = value Then Return End If CType(Me.CellTemplate, DataGridViewProgressBarCell).Mimimum = value If Me.DataGridView Is Nothing Then Return End If Dim rowCount As Integer = Me.DataGridView.RowCount Dim i As Integer For i = 0 To rowCount - 1 Dim r As DataGridViewRow = Me.DataGridView.Rows.SharedRow(i) CType(r.Cells(Me.Index), DataGridViewProgressBarCell).Mimimum = value Next i End Set End Property End Class Public Class DataGridViewProgressBarCell Inherits DataGridViewTextBoxCell Public Sub New() Me.maximumValue = 100 Me.mimimumValue = 0 End Sub Private maximumValue As Integer Public Property Maximum() As Integer Get Return Me.maximumValue End Get Set(ByVal value As Integer) Me.maximumValue = value End Set End Property Private mimimumValue As Integer Public Property Mimimum() As Integer Get Return Me.mimimumValue End Get Set(ByVal value As Integer) Me.mimimumValue = value End Set End Property Public Overrides ReadOnly Property ValueType() As Type Get Return GetType(Integer) End Get End Property Public Overrides ReadOnly Property DefaultNewRowValue() As Object Get Return 0 End Get End Property Public Overrides Function Clone() As Object Dim cell As DataGridViewProgressBarCell = CType(MyBase.Clone(), DataGridViewProgressBarCell) cell.Maximum = Me.Maximum cell.Mimimum = Me.Mimimum Return cell End Function Protected Overrides Sub Paint(ByVal graphics As Graphics, ByVal clipBounds As Rectangle, ByVal cellBounds As Rectangle, ByVal rowIndex As Integer, ByVal cellState As DataGridViewElementStates, ByVal value As Object, ByVal formattedValue As Object, ByVal errorText As String, ByVal cellStyle As DataGridViewCellStyle, ByVal advancedBorderStyle As DataGridViewAdvancedBorderStyle, ByVal paintParts As DataGridViewPaintParts) Dim intValue As Integer = 0 If TypeOf value Is Integer Then intValue = CInt(value) End If If intValue < Me.mimimumValue Then intValue = Me.mimimumValue End If If intValue > Me.maximumValue Then intValue = Me.maximumValue End If Dim rate As Double = CDbl(intValue - Me.mimimumValue) / (Me.maximumValue - Me.mimimumValue) If (paintParts And DataGridViewPaintParts.Border) = DataGridViewPaintParts.Border Then Me.PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle) End If Dim borderRect As Rectangle = Me.BorderWidths(advancedBorderStyle) Dim paintRect As New Rectangle(cellBounds.Left + borderRect.Left, cellBounds.Top + borderRect.Top, cellBounds.Width - borderRect.Right, cellBounds.Height - borderRect.Bottom) Dim isSelected As Boolean = ((cellState And DataGridViewElementStates.Selected) = DataGridViewElementStates.Selected) Dim bkColor As Color If isSelected AndAlso (paintParts And DataGridViewPaintParts.SelectionBackground) = DataGridViewPaintParts.SelectionBackground Then bkColor = cellStyle.SelectionBackColor Else bkColor = cellStyle.BackColor End If If (paintParts And DataGridViewPaintParts.Background) = DataGridViewPaintParts.Background Then Dim backBrush As New SolidBrush(bkColor) Try graphics.FillRectangle(backBrush, paintRect) Finally backBrush.Dispose() End Try End If paintRect.Offset(cellStyle.Padding.Right, cellStyle.Padding.Top) paintRect.Width -= cellStyle.Padding.Horizontal paintRect.Height -= cellStyle.Padding.Vertical If (paintParts And DataGridViewPaintParts.ContentForeground) = DataGridViewPaintParts.ContentForeground Then If ProgressBarRenderer.IsSupported Then ProgressBarRenderer.DrawHorizontalBar(graphics, paintRect) Dim barBounds As New Rectangle(paintRect.Left + 3, paintRect.Top + 3, paintRect.Width - 4, paintRect.Height - 6) barBounds.Width = CInt(Math.Round((barBounds.Width * rate))) ProgressBarRenderer.DrawHorizontalChunks(graphics, barBounds) Else graphics.FillRectangle(Brushes.White, paintRect) graphics.DrawRectangle(Pens.Black, paintRect) Dim barBounds As New Rectangle(paintRect.Left + 1, paintRect.Top + 1, paintRect.Width - 1, paintRect.Height - 1) barBounds.Width = CInt(Math.Round((barBounds.Width * rate))) graphics.FillRectangle(Brushes.Blue, barBounds) End If End If If Me.DataGridView.CurrentCellAddress.X = Me.ColumnIndex AndAlso Me.DataGridView.CurrentCellAddress.Y = Me.RowIndex AndAlso (paintParts And DataGridViewPaintParts.Focus) = DataGridViewPaintParts.Focus AndAlso Me.DataGridView.Focused Then Dim focusRect As Rectangle = paintRect focusRect.Inflate(-3, -3) ControlPaint.DrawFocusRectangle(graphics, focusRect) End If If (paintParts And DataGridViewPaintParts.ContentForeground) = DataGridViewPaintParts.ContentForeground Then Dim txt As String = String.Format("{0}%", Math.Round((rate * 100))) Dim flags As TextFormatFlags = TextFormatFlags.HorizontalCenter Or TextFormatFlags.VerticalCenter Dim fColor As Color = cellStyle.ForeColor paintRect.Inflate(-2, -2) TextRenderer.DrawText(graphics, txt, cellStyle.Font, paintRect, fColor, flags) End If If (paintParts And DataGridViewPaintParts.ErrorIcon) = DataGridViewPaintParts.ErrorIcon AndAlso Me.DataGridView.ShowCellErrors AndAlso Not String.IsNullOrEmpty(errorText) Then Dim iconBounds As Rectangle = Me.GetErrorIconBounds(graphics, cellStyle, rowIndex) iconBounds.Offset(cellBounds.X, cellBounds.Y) Me.PaintErrorIcon(graphics, iconBounds, cellBounds, errorText) End If End Sub End ClassEnd Class
运行项目进行编译
对DataGridView进行列编辑,选中要设置进度条显示的列,把columnType设置为自己写的DataGridViewProgressBarColumn,默认的是DataGridViewTextBoxColumn属性。可以定义好最大值和最小值,默认的是0,100.把value值赋值成介于最大值和最小值之间的整数就可以显示进度条的效果了。
如果不正常显示的话,检查是不是正整数值,另外不要把赋值符号“=”右边的值写成计算的形式,要把计算好的值直接赋予value值。
0 0
- VB中添加进度条列
- VB 中添加进度条
- DataGridView 列中添加进度条
- vb.net 中给ListView 添加进度条
- 在DataGrid中添加图片列(VB.NET)
- 状态栏中添加进度条
- VB.NET Datagridview 增加列用来显示进度条
- CListCtrl 中添加控制进度条
- 数据表中添加列
- 自定义进度条,可在进度条中添加节点
- EXCEL中使用VB按列排序
- DataGridView列中添加CheckBox
- 在表中添加列
- 在datagridview中添加列
- DataGridView 代码中添加列
- gridControl1 中添加checkbox 列
- C# 在listview 中添加进度条
- CListCtrl 中添加控制进度条 (2)
- 实例 (MySQLi 使用预处理语句)
- Spring Boot中使用@Async实现异步调用
- 关于在mac下的idea debug运行时会卡顿几十秒的问题
- mysql update 的时候使用left join和where语句
- 潜在威胁的识别
- VB中添加进度条列
- 第一天开始学习之路
- PopupWindow的使用以及ArrayAdatper.notifyDataSetChanged()无效详解
- png压缩,jpg压缩不错的网站推荐
- FileSystemWatcher文件监控
- 第一个shell脚本
- udp通信
- __weak __strong 在block中的作用
- mysql5.7.x:this is incompatible with DISTINCT