动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
   .Net → 阅读文章

 按钮列的应用

作者:郁郁小蝎 来源:中国站长学院 
阅读 数 319 人次 , 2006-2-15 14:30:00 

 

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



  
 
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:.net中窗体的调整
· 下一篇:带颜色的listbox控件
· 关于如何读取别人的网站使用NGWS NET classes
· 使aspx页面能接受HTML,asp的页面传送的文件
· 用ASP.Net写一个发送ICQ信息的程序
· 十三、JSP动作
· 带颜色的listbox控件


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077