分享
 
 
 

按钮列的应用。(在datagrid中加入按钮列,winforms)

王朝system·作者佚名  2006-01-31
窄屏简体版  字體: |||超大  

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有