[求助]想改造个TreeDataGridView控件类,可是多处报错,请帮忙看看!
自己想编个程序,需要用到 TreeDataGridView 这类的控件,而网上的控件功能又不全,想自己改个,用的 2010代码全是从网上搜的,有好多地方看不懂~
' Properties
Public Overloads Property DataGridView() As TreeDataGirdView
Get
Return DirectCast(MyBase.DataGridView, TreeDataGirdView)
End Get
Set(ByVal value As TreeDataGirdView)
If mLevel = 1 Then
me.get_DataGridView().fcRootCellAdd(Me)
End If
End Set
End Property
这句里 get_DataGridView() 老报错,说未定义,可是定义了,又说与TreeDataGirdView隐式定义冲突
还有 Me.get_Datagridview 也一样,网上查不到。。。书上也没写,是用C#代码转成的
C#源码:
public TreeDataGirdView DataGridView
{
get
{
return (TreeDataGirdView) base.DataGridView;
}
set
{
if (mLevel == 1)
{
get_Datagridview().fcRootCellAdd(this);
}
}
}
graphics.DrawLine(Pens.Black, (Me.mPlusMinusRect.X + 3), (Me.mPlusMinusRect.Top + (Me.mPlusMinusRect.Height / 2)), ((Me.mPlusMinusRect.Left + Me.mPlusMinusRect.Width) - 3), (Me.mPlusMinusRect.Top + (Me.mPlusMinusRect.Height / 2)))
还有上面这句总说 :
重载决策失败,原因是没有可访问的“DrawLine”能够不经收缩转换即可被调用:
“Public Sub DrawLine(pen As System.Drawing.Pen, x1 As Integer, y1 As Integer, x2 As Integer, y2 As Integer)”: 与形参“y1”匹配的实参从“Double”收缩转换为“Integer”。
“Public Sub DrawLine(pen As System.Drawing.Pen, x1 As Integer, y1 As Integer, x2 As Integer, y2 As Integer)”: 与形参“y2”匹配的实参从“Double”收缩转换为“Integer”。
“Public Sub DrawLine(pen As System.Drawing.Pen, x1 As Single, y1 As Single, x2 As Single, y2 As Single)”: 与形参“y1”匹配的实参从“Double”收缩转换为“Single”。
“Public Sub DrawLine(pen As System.Drawing.Pen, x1 As Single, y1 As Single, x2 As Single, y2 As Single)”: 与形参“y2”匹配的实参从“Double”收缩转换为“Single”。
请高手帮忙看看,怎么回事,在网上查遍了,也不知道怎么改
这个是全部源码,组件类
Imports Microsoft.VisualBasic
Imports Microsoft.
Imports System
Imports System.Collections.Generic
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Linq
Imports System.
Imports System.Windows.Forms
Public Class TreeDataGirdView
Inherits DataGridView
' Constructors
Public Sub New()
AddHandler MyBase.RowsAdded, New DataGridViewRowsAddedEventHandler(AddressOf Me.TreeDataGirdView_RowsAdded)
AddHandler MyBase.RowsRemoved, New DataGridViewRowsRemovedEventHandler(AddressOf Me.TreeDataGirdView_RowsRemoved)
Me.mRootCells = New List(Of TreeDataGridViewCell)
Me.mIsTreeDataGridView = True
Me.AllowUserToAddRows = False
End Sub
' Methods
Public Sub init()
Dim col As New TreeDataGridViewColumn
col.Name = ChrW(30446) & ChrW(24405) & ChrW(26641)
Me.Columns.Add(col)
End Sub
Public Sub fcMoveRow(ByVal indexSource As Integer, ByVal indexDestination As Integer)
Dim row As DataGridViewRow = Me.Rows.Item(indexSource)
Me.Rows.RemoveAt(indexSource)
Console.WriteLine(RuntimeHelpers.GetObjectValue(row.Cells.Item(0).Value))
Me.Rows.Insert(indexDestination, row)
Console.WriteLine(RuntimeHelpers.GetObjectValue(row.Cells.Item(0).Value))
End Sub
Public Sub onsjFirstExpand(ByVal rowIndex As Integer)
Dim handler1 As TreeDataGirdView.sjFirstExpandEventHandler = Me.sjFirstExpandEvent
If (Not handler1 Is Nothing) Then
handler1.Invoke(Me, New DataGridViewRowEventArgs(Me.Rows.Item(rowIndex)))
End If
End Sub
Public Function fcRootCellRangeAdd(ByVal RootCells As TreeDataGridViewCell()) As Integer
If (RootCells Is Nothing) Then
Throw New InvalidCastException(ChrW(19981) & ChrW(33021) & ChrW(28155) & ChrW(21152) & " Nothing")
End If
Dim cellArray1 As TreeDataGridViewCell() = RootCells
Dim i As Integer
For i = 0 To cellArray1.Length - 1
Dim cell As TreeDataGridViewCell = cellArray1(i)
Next i
Me.mRootCells.AddRange(RootCells)
Return Enumerable.Count(Of TreeDataGridViewCell)(RootCells)
End Function
Public Function fcRootCellAdd(ByVal RootCell As TreeDataGridViewCell) As Integer
Return Me.fcRootCellRangeAdd(New TreeDataGridViewCell() {RootCell})
End Function
Public Function fcRootCellRemove(ByVal RootCell As Object) As Integer
If Me.mRootCells.Contains(DirectCast(RootCell, TreeDataGridViewCell)) Then
Me.mRootCells.Remove(DirectCast(RootCell, TreeDataGridViewCell))
End If
Return Me.mRootCells.Count
End Function
Public Sub fcRootCellClear()
Me.mRootCells.Clear()
End Sub
Private Sub TreeDataGirdView_RowsAdded(ByVal sender As Object, ByVal e As DataGridViewRowsAddedEventArgs)
If Me.mIsTreeDataGridView Then
Console.WriteLine((ChrW(28155) & ChrW(21152) & Conversions.ToString(e.RowCount) & ChrW(34892)))
Dim cell As TreeDataGridViewCell = Nothing
If (e.RowCount > 0) Then
Dim num1 As Integer = ((e.RowIndex + e.RowCount) - 1)
Dim i As Integer = e.RowIndex
Do While (i <= num1)
Try
cell = DirectCast(Me.Rows.Item(i).Cells.Item(0), TreeDataGridViewCell)
If ((Not cell Is Nothing) AndAlso (cell.sxLevel = 0)) Then
cell.sxLevel = 1
End If
Catch exception1 As Exception
ProjectData.SetProjectError(exception1)
Dim ex As Exception = exception1
Interaction.MsgBox(ex.Message, 0, Nothing)
ProjectData.ClearProjectError()
End Try
i += 1
Loop
End If
End If
End Sub
Private Sub TreeDataGirdView_RowsRemoved(ByVal sender As Object, ByVal e As DataGridViewRowsRemovedEventArgs)
If (Me.RowCount = 0) Then
Me.mRootCells.Clear()
Else
Dim i As Integer = (Me.mRootCells.Count - 1)
Do While (i >= 0)
If (Me.mRootCells.Item(i).RowIndex = -1) Then
Me.mRootCells.RemoveAt(i)
End If
i = (i + -1)
Loop
End If
End Sub
' Properties
Public ReadOnly Property sxRootCellS As TreeDataGridViewCell()
Get
Return Me.mRootCells.ToArray
End Get
End Property
Public Property sxRowHeight As Integer
Get
Return Me.RowTemplate.Height
End Get
Set(ByVal value As Integer)
If (value > 0) Then
Me.RowTemplate.Height = value
End If
End Set
End Property
Public Property sxReservationImagePosition As Boolean
Get
Return Me.mReservationImagePosition
End Get
Set(ByVal value As Boolean)
Me.mReservationImagePosition = value
End Set
End Property
Public Property sxImageList As ImageList
Get
Return Me.mImageList
End Get
Set(ByVal value As ImageList)
Me.mImageList = value
End Set
End Property
' Nested Types
Public Delegate Sub sjFirstExpandEventHandler(ByVal sender As Object, ByVal e As DataGridViewRowEventArgs)
' Events
Public Event sjFirstExpand As TreeDataGirdView.sjFirstExpandEventHandler
AddHandler(ByVal obj As TreeDataGirdView.sjFirstExpandEventHandler)
Me.sjFirstExpandEvent = DirectCast((Me.sjFirstExpandEvent, obj), TreeDataGirdView.sjFirstExpandEventHandler)
End AddHandler
RemoveEvent(ByVal obj As TreeDataGirdView.sjFirstExpandEventHandler)
Me.sjFirstExpandEvent = DirectCast(Delegate.Remove(Me.sjFirstExpandEvent, obj), TreeDataGirdView.sjFirstExpandEventHandler)
End RemoveEvent
End Event
' Instance Fields
Private mRootCells As List(Of TreeDataGridViewCell)
Private mIsTreeDataGridView As Boolean
Private mReservationImagePosition As Boolean
Private mImageList As ImageList
Private sjFirstExpandEvent As TreeDataGirdView.sjFirstExpandEventHandler
End Class
Public Class TreeDataGridViewCell
Inherits DataGridViewTextBoxCell
' Constructors
Public Sub New()
Me.mChildCells = New List(Of TreeDataGridViewCell)
Me.mIndentWidth = 20
Me.mFirstExpand = False
Me.mImageIndex = -1
Me.mImageKey = Nothing
End Sub
' Methods
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 rect As Rectangle
Dim wPlus As Integer = 10
Dim offsetX As Integer = 2
Dim gridPen As New Pen(Me.get_Datagridview.GridColor)
Dim bruBK As New SolidBrush(cellStyle.BackColor)
Dim DashPen As New Pen(Me.get_Datagridview.GridColor)
DashPen.DashStyle = DashStyle.Dot
If (Me.RowIndex = 1) Then
Console.WriteLine()
End If
graphics.FillRectangle(bruBK, cellBounds)
Me.PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle)
Me.mPlusMinusRect = New Rectangle(((((Me.mLevel - 1) * Me.mIndentWidth) + cellBounds.X) + offsetX), (cellBounds.Top + ((cellBounds.Height - wPlus) / 2)), wPlus, wPlus)
Dim p As New Point((Me.mPlusMinusRect.X + (Me.mPlusMinusRect.Width / 2)), (Me.mPlusMinusRect.Y + (Me.mPlusMinusRect.Height / 2)))
If ((Not Me.ParentCell Is Nothing) AndAlso (Me.ParentCell.sxChildCells.Length = 1)) Then
graphics.DrawLine(DashPen, p.X, p.Y, p.X, cellBounds.Top)
End If
graphics.DrawLine(DashPen, p.X, p.Y, cellBounds.Right, p.Y)
Dim parent As TreeDataGridViewCell = Me
Do While True
Dim x As Integer
Dim pcells As TreeDataGridViewCell()
If (parent.sxLevel = 1) Then
Dim roots As TreeDataGridViewCell() = Me.get_Datagridview.sxRootCellS
x = (p.X - ((Me.mLevel - 1) * Me.mIndentWidth))
If (roots.Length > 1) Then
If Me.Equals(roots(0)) Then
graphics.DrawLine(DashPen, x, p.Y, x, cellBounds.Bottom)
Exit Do
End If
If Me.Equals(roots((roots.Length - 1))) Then
graphics.DrawLine(DashPen, x, p.Y, x, cellBounds.Top)
Exit Do
End If
graphics.DrawLine(DashPen, x, cellBounds.Top, x, cellBounds.Bottom)
End If
Exit Do
End If
x = (p.X - ((Me.mLevel - parent.mLevel) * Me.mIndentWidth))
If (parent.mLevel = 1) Then
pcells = Me.TreeDataGridview.sxRootCellS
Else
pcells = parent.ParentCell.sxChildCells
End If
If (pcells.Length > 1) Then
If parent.Equals(pcells((pcells.Length - 1))) Then
graphics.DrawLine(DashPen, x, cellBounds.Top, x, p.Y)
Else
graphics.DrawLine(DashPen, x, cellBounds.Top, x, cellBounds.Bottom)
End If
End If
parent = parent.ParentCell
Loop
If ((Not Me.mChildCells Is Nothing) AndAlso (Me.mChildCells.Count > 0)) Then
graphics.DrawRectangle(gridPen, Me.mPlusMinusRect)
graphics.DrawLine(Pens.Black, (Me.mPlusMinusRect.X + 3), (Me.mPlusMinusRect.Top + (Me.mPlusMinusRect.Height / 2)), ((Me.mPlusMinusRect.Left + Me.mPlusMinusRect.Width) - 3), (Me.mPlusMinusRect.Top + (Me.mPlusMinusRect.Height / 2)))
If Me.mCollapse Then
graphics.DrawLine(Pens.Black, (Me.mPlusMinusRect.X + (Me.mPlusMinusRect.Width / 2)), (Me.mPlusMinusRect.Y + 3), (Me.mPlusMinusRect.X + (Me.mPlusMinusRect.Width / 2)), ((Me.mPlusMinusRect.Top + Me.mPlusMinusRect.Height) - 3))
End If
End If
Dim image As ImageList = Me.TreeDataGridview.sxImageList
Dim img As Image = Nothing
If (Not image Is Nothing) Then
If (Not Me.mImageIndex = -1) Then
img = image.Images.Item(Me.mImageIndex)
Else
If (Not Me.mImageKey Is Nothing) Then
img = image.Images.Item(Me.mImageKey)
End If
End If
End If
Dim leftw As Integer = ((Me.mLevel * Me.mIndentWidth) + cellBounds.X)
If (Not img Is Nothing) Then
leftw = (leftw + 2)
graphics.DrawImage(img, New Point(leftw, (((cellBounds.Height - image.ImageSize.Height) / 2) + cellBounds.Top)))
rect = New Rectangle(((leftw + img.Width) + 2), cellBounds.Y, (((cellBounds.Width - (Me.mLevel * Me.mIndentWidth)) - img.Width) - 4), cellBounds.Height)
Else
rect = New Rectangle(((Me.mLevel * Me.mIndentWidth) + cellBounds.X), cellBounds.Y, (cellBounds.Width - (Me.mLevel * Me.mIndentWidth)), cellBounds.Height)
End If
MyBase.Paint(graphics, clipBounds, rect, rowIndex, cellState, RuntimeHelpers.GetObjectValue(value), RuntimeHelpers.GetObjectValue(formattedValue), errorText, cellStyle, advancedBorderStyle, paintParts)
gridPen.Dispose()
bruBK.Dispose()
DashPen.Dispose()
End Sub
Private Sub drawLianxian2(ByVal g As Graphics, ByVal cell As TreeDataGridViewCell, ByVal cellBounds As Rectangle)
End Sub
Private Sub drawLianxian(ByVal g As Graphics, ByVal cell As TreeDataGridViewCell, ByVal cellBounds As Rectangle)
End Sub
Public Function fcGetParentCell(ByVal cell As TreeDataGridViewCell, ByVal level As Integer) As TreeDataGridViewCell
Dim c As TreeDataGridViewCell = cell.ParentCell
Console.WriteLine(c.sxLevel)
If (Not c.sxLevel = level) Then
c = Me.fcGetParentCell(c, level)
End If
Return c
End Function
Protected Overrides Sub OnMouseMove(ByVal e As DataGridViewCellMouseEventArgs)
MyBase.OnMouseMove(e)
If ((Not e.RowIndex = -1) AndAlso (Not e.ColumnIndex = -1)) Then
Dim bln As Boolean
If (((Not Me.mChildCells Is Nothing) AndAlso (Me.sxChildCells.Length > 0)) AndAlso Me.mPlusMinusRect.Contains(Me.get_Datagridview.PointToClient(Cursor.Position))) Then
bln = True
End If
If bln Then
Me.get_Datagridview.Cursor = Cursors.Hand
Else
Me.get_Datagridview.Cursor = Cursors.Default
End If
End If
End Sub
Protected Overrides Sub OnMouseUp(ByVal e As DataGridViewCellMouseEventArgs)
MyBase.OnMouseUp(e)
If ((Not e.RowIndex = -1) AndAlso (Not e.ColumnIndex = -1)) Then
If ((Not Me.mChildCells Is Nothing) AndAlso (Me.sxChildCells.Length > 0)) Then
Dim point1 As Point = Me.get_Datagridview.PointToClient(Cursor.Position)
Console.WriteLine((point1.ToString & " " & Me.mPlusMinusRect.ToString))
If Me.mPlusMinusRect.Contains(Me.get_Datagridview.PointToClient(Cursor.Position)) Then
Me.sxCollapse = Not Me.sxCollapse
End If
End If
If (Not Me.mCollapse AndAlso Not Me.mFirstExpand) Then
Me.mFirstExpand = True
Me.TreeDataGridview.onsjFirstExpand(Me.RowIndex)
End If
End If
End Sub
Private Function fcgetClildsCount(ByVal cell As TreeDataGridViewCell) As Object
Dim count As Integer = cell.sxChildCells.Length
Dim cellArray1 As TreeDataGridViewCell() = cell.sxChildCells
Dim i As Integer
For i = 0 To cellArray1.Length - 1
Dim c As TreeDataGridViewCell = cellArray1(i)
count = Conversions.ToInteger(Operators.AddObject(count, Me.fcgetClildsCount(c)))
Next i
Return count
End Function
Private Sub subSetRowVis(ByVal cell As TreeDataGridViewCell, ByVal vis As Boolean)
If (Not Information.IsNothing(cell.sxChildCells) AndAlso (Not Enumerable.Count(Of TreeDataGridViewCell)(cell.sxChildCells) = 0)) Then
Dim cellArray1 As TreeDataGridViewCell() = cell.sxChildCells
Dim i As Integer
For i = 0 To cellArray1.Length - 1
Dim c As TreeDataGridViewCell = cellArray1(i)
c.get_Datagridview.Rows.Item(c.RowIndex).Visible = vis
If (c.sxChildCells.Length > 0) Then
Me.subSetRowVis(c, vis)
End If
Next i
End If
End Sub
Public Function fcSetChildCell(ByVal cell As TreeDataGridViewCell) As Object
Return Me.fcSetClildCellRanga(New TreeDataGridViewCell() {cell})
End Function
Public Function fcSetClildCellRanga(ByVal arrCells As TreeDataGridViewCell()) As Object
Dim needRedraw As Boolean = False
If (Me.mChildCells Is Nothing) Then
Me.mChildCells = New List(Of TreeDataGridViewCell)
needRedraw = True
End If
Dim pIndex As Integer = Me.RowIndex
Dim index As Object = Operators.AddObject(Operators.AddObject(Me.RowIndex, Me.sxGetCellChildCount(True)), 1)
Dim cellArray1 As TreeDataGridViewCell() = arrCells
Dim i As Integer
For i = 0 To cellArray1.Length - 1
Dim cell As TreeDataGridViewCell = cellArray1(i)
If Operators.ConditionalCompareObjectNotEqual(cell.RowIndex, index, False) Then
cell.TreeDataGridview.fcMoveRow(cell.RowIndex, Conversions.ToInteger(index))
End If
Me.mChildCells.Add(cell)
cell.ParentCell = Me
cell.sxLevel = (Me.mLevel + 1)
index = Operators.AddObject(index, 1)
Next i
If needRedraw Then
Me.get_Datagridview.InvalidateCell(Me)
End If
Return Me.mChildCells.Count
End Function
' Properties
Public Overloads Property DataGridView() As TreeDataGirdView
Get
Return DirectCast(MyBase.DataGridView, TreeDataGirdView)
End Get
Set(ByVal value As TreeDataGirdView)
If mLevel = 1 Then
me.get_DataGridView().fcRootCellAdd(Me)
End If
End Set
End Property
Public ReadOnly Property sxGetCellChildCount(ByVal isSubChild As Boolean) As Object
Get
If isSubChild Then
Return Me.fcgetClildsCount(Me)
End If
Return Me.mChildCells.Count
End Get
End Property
Public Property ImageIndex As Integer
Get
Return Me.mImageIndex
End Get
Set(ByVal value As Integer)
Me.mImageIndex = value
Me.mImageKey = Nothing
End Set
End Property
Public Property ImageKey As String
Get
Return Me.mImageKey
End Get
Set(ByVal value As String)
Me.mImageKey = value
Me.mImageIndex = -1
End Set
End Property
Public ReadOnly Property PrePlusMinusRect As Rectangle
Get
Return Me.mPlusMinusRect
End Get
End Property
Public Property ParentCell As TreeDataGridViewCell
Get
Return Me.mParentCell
End Get
Set(ByVal value As TreeDataGridViewCell)
Me.mParentCell = value
End Set
End Property
Public Property sxCollapse As Boolean
Get
Return Me.mCollapse
End Get
Set(ByVal value As Boolean)
Me.mCollapse = value
Dim flag11 As Boolean = Not Me.mCollapse
If (Not Me.sxChildCells Is Nothing) Then
Me.subSetRowVis(Me, flag11)
End If
End Set
End Property
Public Property sxLevel As Integer
Get
Return Me.mLevel
End Get
Set(ByVal value As Integer)
If (value < 1) Then
Throw New InvalidCastException(ChrW(23618) & ChrW(32423) & ChrW(24517) & ChrW(39035) & ChrW(26159) & ChrW(22823) & ChrW(20110) & "1" & ChrW(30340) & ChrW(25968) & ChrW(23383))
End If
Dim view11 As TreeDataGirdView = Me.get_Datagridview
If (Not view11 Is Nothing) Then
If (value = 1) Then
view11.fcRootCellAdd(Me)
Else
If (Me.mLevel = 1) Then
view11.fcRootCellRemove(Me)
End If
End If
Me.mLevel = value
End If
End Set
End Property
Public ReadOnly Property sxChildCells As TreeDataGridViewCell()
Get
Return Me.mChildCells.ToArray
End Get
End Property
Public ReadOnly Property TreeDataGridview As TreeDataGirdView
Get
Return Me.get_Datagridview
End Get
End Property
' Instance Fields
Private rc As Rectangle
Private mLevel As Integer
Private mCollapse As Boolean
Private mChildCells As List(Of TreeDataGridViewCell)
Private mParentCell As TreeDataGridViewCell
Private mIndentWidth As Integer
Private mPlusMinusRect As Rectangle
Private mFirstExpand As Boolean
Private mImageIndex As Integer
Private mImageKey As String
End Class
Public Class TreeDataGridViewColumn
Inherits DataGridViewTextBoxColumn
' Constructors
Public Sub New()
Me.CellTemplate = New TreeDataGridViewCell
Me.ReadOnly = True
End Sub
' Properties
Public Overrides Property CellTemplate As DataGridViewCell
Get
Return MyBase.CellTemplate
End Get
Set(ByVal value As DataGridViewCell)
If ((Not value Is Nothing) AndAlso Not TypeOf value Is TreeDataGridViewCell) Then
Throw New InvalidCastException("Need System.Windows.Forms.DataGridViewGroupCell")
End If
MyBase.CellTemplate = value
End Set
End Property
End Class
[ 本帖最后由 juxina 于 2012-2-19 23:37 编辑 ]