按钮列的应用。(在datagrid中加入按钮列,winforms)
2007-05-18 17:51
417 查看
Public Class mybuttondatagridtablestyle1
Inherits System.Windows.Forms.DataGridTextBoxColumn
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'UserControl 重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意:以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
components = New System.ComponentModel.Container()
End Sub
#End Region
Public Delegate Sub DataGridCellButtonClickEventHandler(ByVal sender As Object, ByVal e As DataGridCellButtonClickEventArgs)
Public Event CellButtonClicked As DataGridCellButtonClickEventHandler
Private m_Face As Bitmap
Private m_FacePressed As Bitmap
Private m_columnNum As Integer
Private m_Row As Integer
Public Sub New(ByVal colNum As Integer)
m_columnNum = colNum
m_Row = -1
Try
Dim strm As System.IO.Stream = Me.GetType().Assembly.GetManifestResourceStream("btnface.bmp")
m_Face = New Bitmap(strm)
strm = Me.GetType().Assembly.GetManifestResourceStream("btnpressed.bmp")
m_FacePressed = New Bitmap(strm)
Catch
End Try
End Sub
Protected Overloads Overrides Sub Edit(ByVal [source] As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal bounds As System.Drawing.Rectangle, ByVal [readOnly] As Boolean, ByVal instantText As String, ByVal cellIsVisible As Boolean)
End Sub
Public Sub HandleMouseUp(ByVal sender As Object, ByVal e As MouseEventArgs)
Dim dg As DataGrid = Me.DataGridTableStyle.DataGrid
Dim hti As DataGrid.HitTestInfo = dg.HitTest(New Point(e.X, e.Y))
Dim isClickInCell As Boolean = hti.Column = Me.m_columnNum
m_Row = -1
Dim rect As New Rectangle(0, 0, 0, 0)
If isClickInCell Then
rect = dg.GetCellBounds(hti.Row, hti.Column)
isClickInCell = e.X > rect.Right - Me.m_Face.Width
End If
If isClickInCell Then
Dim g As Graphics = Graphics.FromHwnd(dg.Handle)
g.DrawImage(Me.m_Face, rect.Right - Me.m_Face.Width, rect.Y)
g.Dispose()
RaiseEvent CellButtonClicked(Me, New DataGridCellButtonClickEventArgs(hti.Row, hti.Column))
End If
End Sub
Public Sub HandleMouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
Dim dg As DataGrid = Me.DataGridTableStyle.DataGrid
Dim hti As DataGrid.HitTestInfo = dg.HitTest(New Point(e.X, e.Y))
Dim isClickInCell As Boolean = hti.Column = Me.m_columnNum
Dim rect As New Rectangle(0, 0, 0, 0)
If isClickInCell Then
rect = dg.GetCellBounds(hti.Row, hti.Column)
isClickInCell = e.X > rect.Right - Me.m_Face.Width
End If
If isClickInCell Then
Dim g As Graphics = Graphics.FromHwnd(dg.Handle)
g.DrawImage(Me.m_FacePressed, rect.Right - Me.m_FacePressed.Width, rect.Y)
g.Dispose()
m_Row = hti.Row
End If
End Sub
'重绘
Protected Overloads Overrides Sub Paint(ByVal g As System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal [source] As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal backBrush As System.Drawing.Brush, ByVal foreBrush As System.Drawing.Brush, ByVal alignToRight As Boolean)
Dim parent As DataGrid = Me.DataGridTableStyle.DataGrid
'如果该行是选中行 或者 当前单元格的行号=点击行的行号并且当前单元格的列号等于NEW的列号参数
Dim current As Boolean = parent.IsSelected(rowNum) Or (parent.CurrentRowIndex = rowNum And parent.CurrentCell.ColumnNumber = Me.m_columnNum)
Dim BackColor As Color
If current Then BackColor = parent.SelectionBackColor Else BackColor = parent.BackColor
Dim ForeColor As Color
If current Then ForeColor = parent.SelectionForeColor Else ForeColor = parent.ForeColor
'请空单元格
g.FillRectangle(New SolidBrush(BackColor), bounds)
' 绘制值
Dim s As String = Me.GetColumnValueAtRow([source], rowNum).ToString() 'parent[rowNum, 0].ToString() + ((parent[rowNum, 1].ToString())+ " ").Substring(0,2);
g.DrawString(s, parent.Font, New SolidBrush(ForeColor), bounds.X, bounds.Y)
Dim bm As Bitmap
If m_Row = rowNum Then bm = Me.m_FacePressed Else bm = Me.m_Face
g.DrawImage(bm, bounds.Right - bm.Width, bounds.Y)
End Sub
End Class
调用代码:
Private Function getdatagridstyle(ByVal table As DataTable) As DataGridTableStyle
Dim style As New DataGridTableStyle()
style.MappingName = table.TableName
style.RowHeaderWidth = 15
Dim i As Integer
For i = 0 To table.Columns.Count - 1
If i = 1 Then
Dim textButtonColStyle As New mybuttondatagridtablestyle1(i) 'pass the column#
textButtonColStyle.HeaderText = table.Columns(i).ColumnName
textButtonColStyle.MappingName = table.Columns(i).ColumnName
'hookup our cellbutton handler...
AddHandler textButtonColStyle.CellButtonClicked, AddressOf HandleCellButtonClick
style.GridColumnStyles.Add(textButtonColStyle)
'hook the mouse handlers
AddHandler DataGrid1.MouseDown, AddressOf textButtonColStyle.HandleMouseDown
AddHandler DataGrid1.MouseUp, AddressOf textButtonColStyle.HandleMouseUp
Else
Dim c As New DataGridTextBoxColumn()
c.HeaderText = table.Columns(i).ColumnName
c.MappingName = table.Columns(i).ColumnName
style.GridColumnStyles.Add(c)
End If
Next
Return style
End Function
Private Sub HandleCellButtonClick(ByVal sender As Object, ByVal e As DataGridCellButtonClickEventArgs)
MessageBox.Show(("row " + e.RowIndex.ToString() + " col " + e.ColIndex.ToString() + " clicked."))
End Sub
Inherits System.Windows.Forms.DataGridTextBoxColumn
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'UserControl 重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意:以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
components = New System.ComponentModel.Container()
End Sub
#End Region
Public Delegate Sub DataGridCellButtonClickEventHandler(ByVal sender As Object, ByVal e As DataGridCellButtonClickEventArgs)
Public Event CellButtonClicked As DataGridCellButtonClickEventHandler
Private m_Face As Bitmap
Private m_FacePressed As Bitmap
Private m_columnNum As Integer
Private m_Row As Integer
Public Sub New(ByVal colNum As Integer)
m_columnNum = colNum
m_Row = -1
Try
Dim strm As System.IO.Stream = Me.GetType().Assembly.GetManifestResourceStream("btnface.bmp")
m_Face = New Bitmap(strm)
strm = Me.GetType().Assembly.GetManifestResourceStream("btnpressed.bmp")
m_FacePressed = New Bitmap(strm)
Catch
End Try
End Sub
Protected Overloads Overrides Sub Edit(ByVal [source] As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal bounds As System.Drawing.Rectangle, ByVal [readOnly] As Boolean, ByVal instantText As String, ByVal cellIsVisible As Boolean)
End Sub
Public Sub HandleMouseUp(ByVal sender As Object, ByVal e As MouseEventArgs)
Dim dg As DataGrid = Me.DataGridTableStyle.DataGrid
Dim hti As DataGrid.HitTestInfo = dg.HitTest(New Point(e.X, e.Y))
Dim isClickInCell As Boolean = hti.Column = Me.m_columnNum
m_Row = -1
Dim rect As New Rectangle(0, 0, 0, 0)
If isClickInCell Then
rect = dg.GetCellBounds(hti.Row, hti.Column)
isClickInCell = e.X > rect.Right - Me.m_Face.Width
End If
If isClickInCell Then
Dim g As Graphics = Graphics.FromHwnd(dg.Handle)
g.DrawImage(Me.m_Face, rect.Right - Me.m_Face.Width, rect.Y)
g.Dispose()
RaiseEvent CellButtonClicked(Me, New DataGridCellButtonClickEventArgs(hti.Row, hti.Column))
End If
End Sub
Public Sub HandleMouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
Dim dg As DataGrid = Me.DataGridTableStyle.DataGrid
Dim hti As DataGrid.HitTestInfo = dg.HitTest(New Point(e.X, e.Y))
Dim isClickInCell As Boolean = hti.Column = Me.m_columnNum
Dim rect As New Rectangle(0, 0, 0, 0)
If isClickInCell Then
rect = dg.GetCellBounds(hti.Row, hti.Column)
isClickInCell = e.X > rect.Right - Me.m_Face.Width
End If
If isClickInCell Then
Dim g As Graphics = Graphics.FromHwnd(dg.Handle)
g.DrawImage(Me.m_FacePressed, rect.Right - Me.m_FacePressed.Width, rect.Y)
g.Dispose()
m_Row = hti.Row
End If
End Sub
'重绘
Protected Overloads Overrides Sub Paint(ByVal g As System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal [source] As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal backBrush As System.Drawing.Brush, ByVal foreBrush As System.Drawing.Brush, ByVal alignToRight As Boolean)
Dim parent As DataGrid = Me.DataGridTableStyle.DataGrid
'如果该行是选中行 或者 当前单元格的行号=点击行的行号并且当前单元格的列号等于NEW的列号参数
Dim current As Boolean = parent.IsSelected(rowNum) Or (parent.CurrentRowIndex = rowNum And parent.CurrentCell.ColumnNumber = Me.m_columnNum)
Dim BackColor As Color
If current Then BackColor = parent.SelectionBackColor Else BackColor = parent.BackColor
Dim ForeColor As Color
If current Then ForeColor = parent.SelectionForeColor Else ForeColor = parent.ForeColor
'请空单元格
g.FillRectangle(New SolidBrush(BackColor), bounds)
' 绘制值
Dim s As String = Me.GetColumnValueAtRow([source], rowNum).ToString() 'parent[rowNum, 0].ToString() + ((parent[rowNum, 1].ToString())+ " ").Substring(0,2);
g.DrawString(s, parent.Font, New SolidBrush(ForeColor), bounds.X, bounds.Y)
Dim bm As Bitmap
If m_Row = rowNum Then bm = Me.m_FacePressed Else bm = Me.m_Face
g.DrawImage(bm, bounds.Right - bm.Width, bounds.Y)
End Sub
End Class
调用代码:
Private Function getdatagridstyle(ByVal table As DataTable) As DataGridTableStyle
Dim style As New DataGridTableStyle()
style.MappingName = table.TableName
style.RowHeaderWidth = 15
Dim i As Integer
For i = 0 To table.Columns.Count - 1
If i = 1 Then
Dim textButtonColStyle As New mybuttondatagridtablestyle1(i) 'pass the column#
textButtonColStyle.HeaderText = table.Columns(i).ColumnName
textButtonColStyle.MappingName = table.Columns(i).ColumnName
'hookup our cellbutton handler...
AddHandler textButtonColStyle.CellButtonClicked, AddressOf HandleCellButtonClick
style.GridColumnStyles.Add(textButtonColStyle)
'hook the mouse handlers
AddHandler DataGrid1.MouseDown, AddressOf textButtonColStyle.HandleMouseDown
AddHandler DataGrid1.MouseUp, AddressOf textButtonColStyle.HandleMouseUp
Else
Dim c As New DataGridTextBoxColumn()
c.HeaderText = table.Columns(i).ColumnName
c.MappingName = table.Columns(i).ColumnName
style.GridColumnStyles.Add(c)
End If
Next
Return style
End Function
Private Sub HandleCellButtonClick(ByVal sender As Object, ByVal e As DataGridCellButtonClickEventArgs)
MessageBox.Show(("row " + e.RowIndex.ToString() + " col " + e.ColIndex.ToString() + " clicked."))
End Sub
相关文章推荐
- 按钮列的应用。(在datagrid中加入按钮列,winforms)
- flash CS3 在Datagrid(表格组件)中加入Button(按钮)、checkbox(复选框)、combobox下拉框、combotree(树状下拉框)等组件
- DataGrid加入单选按钮(C#)
- DataGrid里应用radio单选按钮
- AdvancedDataGrid高级数据网格加入自定义链接按钮LinkButton
- 在DataGrid的GridColumn列中加入按钮宣染器
- DATAGRID 加入全选反选的按钮
- datagrid的一些简单应用
- DataGrid的应用以及常用方法(Vb.Net版)
- iOS9以后应用打开第三方app时,要加入白名单
- 取经之旅——把WinForms开发的桌面应用移植到Silverlight开发的RIA(第2部分)
- 例说linux内核与应用数据通信(一):加入一个系统调用
- java swing中实现列表中加入单选按钮,单选按钮发生变化时能触发事件
- Easyui的datagrid中有分页,如何设置分页刷新按钮
- android教程之把自己的应用加入到系统分享中
- datagrid的几点简单应用
- FLEX4实践—动态生成DataGrid及应用客户化itemRenderer
- JQuery easyUi datagrid 中 自定义editor作为列表操作按钮列
- 在选择屏幕的标准应用工具条上增加自定义按钮
- Ubuntu 加入Mozilla Prism应用支持!!!!