日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

VB中添加进度条列

發布時間:2023/12/14 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 VB中添加进度条列 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目的:實現如下圖所示效果:

寫實現進度條的類,繼承DataGridViewTextBoxColumn類,代碼如下:

Imports System Imports System.Drawing Imports System.Windows.Forms Public Class DataGridViewPrassBarPublic Class DataGridViewProgressBarColumnInherits DataGridViewTextBoxColumnPublic Sub New()Me.CellTemplate = New DataGridViewProgressBarCell()End SubPublic Overrides Property CellTemplate() As DataGridViewCellGetReturn MyBase.CellTemplateEnd GetSet(ByVal value As DataGridViewCell)If Not TypeOf value Is DataGridViewProgressBarCell ThenThrow New InvalidCastException("請指定DataGridViewProgressBarCell")End IfMyBase.CellTemplate = valueEnd SetEnd PropertyPublic Property Maximum() As IntegerGetReturn CType(Me.CellTemplate, DataGridViewProgressBarCell).MaximumEnd GetSet(ByVal value As Integer)If Me.Maximum = value ThenReturnEnd IfCType(Me.CellTemplate, DataGridViewProgressBarCell).Maximum = valueIf Me.DataGridView Is Nothing ThenReturnEnd IfDim rowCount As Integer = Me.DataGridView.RowCountDim i As IntegerFor i = 0 To rowCount - 1Dim r As DataGridViewRow = Me.DataGridView.Rows.SharedRow(i)CType(r.Cells(Me.Index), DataGridViewProgressBarCell).Maximum = valueNext iEnd SetEnd PropertyPublic Property Mimimum() As IntegerGetReturn CType(Me.CellTemplate, DataGridViewProgressBarCell).MimimumEnd GetSet(ByVal value As Integer)If Me.Mimimum = value ThenReturnEnd IfCType(Me.CellTemplate, DataGridViewProgressBarCell).Mimimum = valueIf Me.DataGridView Is Nothing ThenReturnEnd IfDim rowCount As Integer = Me.DataGridView.RowCountDim i As IntegerFor i = 0 To rowCount - 1Dim r As DataGridViewRow = Me.DataGridView.Rows.SharedRow(i)CType(r.Cells(Me.Index), DataGridViewProgressBarCell).Mimimum = valueNext iEnd SetEnd PropertyEnd ClassPublic Class DataGridViewProgressBarCellInherits DataGridViewTextBoxCellPublic Sub New()Me.maximumValue = 100Me.mimimumValue = 0End SubPrivate maximumValue As IntegerPublic Property Maximum() As IntegerGetReturn Me.maximumValueEnd GetSet(ByVal value As Integer)Me.maximumValue = valueEnd SetEnd PropertyPrivate mimimumValue As IntegerPublic Property Mimimum() As IntegerGetReturn Me.mimimumValueEnd GetSet(ByVal value As Integer)Me.mimimumValue = valueEnd SetEnd PropertyPublic Overrides ReadOnly Property ValueType() As TypeGetReturn GetType(Integer)End GetEnd PropertyPublic Overrides ReadOnly Property DefaultNewRowValue() As ObjectGetReturn 0End GetEnd PropertyPublic Overrides Function Clone() As ObjectDim cell As DataGridViewProgressBarCell = CType(MyBase.Clone(), DataGridViewProgressBarCell)cell.Maximum = Me.Maximumcell.Mimimum = Me.MimimumReturn cellEnd FunctionProtected 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 = 0If TypeOf value Is Integer ThenintValue = CInt(value)End IfIf intValue < Me.mimimumValue ThenintValue = Me.mimimumValueEnd IfIf intValue > Me.maximumValue ThenintValue = Me.maximumValueEnd IfDim rate As Double = CDbl(intValue - Me.mimimumValue) / (Me.maximumValue - Me.mimimumValue)If (paintParts And DataGridViewPaintParts.Border) = DataGridViewPaintParts.Border ThenMe.PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle)End IfDim 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 ColorIf isSelected AndAlso (paintParts And DataGridViewPaintParts.SelectionBackground) = DataGridViewPaintParts.SelectionBackground ThenbkColor = cellStyle.SelectionBackColorElsebkColor = cellStyle.BackColorEnd IfIf (paintParts And DataGridViewPaintParts.Background) = DataGridViewPaintParts.Background ThenDim backBrush As New SolidBrush(bkColor)Trygraphics.FillRectangle(backBrush, paintRect)FinallybackBrush.Dispose()End TryEnd IfpaintRect.Offset(cellStyle.Padding.Right, cellStyle.Padding.Top)paintRect.Width -= cellStyle.Padding.HorizontalpaintRect.Height -= cellStyle.Padding.VerticalIf (paintParts And DataGridViewPaintParts.ContentForeground) = DataGridViewPaintParts.ContentForeground ThenIf ProgressBarRenderer.IsSupported ThenProgressBarRenderer.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)Elsegraphics.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 IfEnd IfIf Me.DataGridView.CurrentCellAddress.X = Me.ColumnIndex AndAlso Me.DataGridView.CurrentCellAddress.Y = Me.RowIndex AndAlso (paintParts And DataGridViewPaintParts.Focus) = DataGridViewPaintParts.Focus AndAlso Me.DataGridView.Focused ThenDim focusRect As Rectangle = paintRectfocusRect.Inflate(-3, -3)ControlPaint.DrawFocusRectangle(graphics, focusRect)End IfIf (paintParts And DataGridViewPaintParts.ContentForeground) = DataGridViewPaintParts.ContentForeground ThenDim txt As String = String.Format("{0}%", Math.Round((rate * 100)))Dim flags As TextFormatFlags = TextFormatFlags.HorizontalCenter Or TextFormatFlags.VerticalCenterDim fColor As Color = cellStyle.ForeColorpaintRect.Inflate(-2, -2)TextRenderer.DrawText(graphics, txt, cellStyle.Font, paintRect, fColor, flags)End IfIf (paintParts And DataGridViewPaintParts.ErrorIcon) = DataGridViewPaintParts.ErrorIcon AndAlso Me.DataGridView.ShowCellErrors AndAlso Not String.IsNullOrEmpty(errorText) ThenDim iconBounds As Rectangle = Me.GetErrorIconBounds(graphics, cellStyle, rowIndex)iconBounds.Offset(cellBounds.X, cellBounds.Y)Me.PaintErrorIcon(graphics, iconBounds, cellBounds, errorText)End IfEnd SubEnd Class End Class

運行項目進行編譯
對DataGridView進行列編輯,選中要設置進度條顯示的列,把columnType設置為自己寫的DataGridViewProgressBarColumn,默認的是DataGridViewTextBoxColumn屬性??梢远x好最大值和最小值,默認的是0,100.把value值賦值成介于最大值和最小值之間的整數就可以顯示進度條的效果了。
如果不正常顯示的話,檢查是不是正整數值,另外不要把賦值符號“=”右邊的值寫成計算的形式,要把計算好的值直接賦予value值。

總結

以上是生活随笔為你收集整理的VB中添加进度条列的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。