首页 > 站长学院 > .Net专区 > 按钮列的应用
 

按钮列的应用

日期:2005-06-28 00:00:00  来源:中国站长学院
请您记住思索网的网址: http://www.4so.net  [加入收藏夹]

     

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

 
 
热门信息
 
相关文章
 
    无相关信息