动态网站制作指南 [  QQ表情  ]
[ 投票调查 ]
[ 企业邮箱 ]
[ 网站空间 ]
网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
ASP源码 | .Net源码 | PHP源码 | JSP源码 | JAVA源码 | CGI源码 | VB源码 | C++源码 | Delphi源码 | PB源码 | VF源码 | 汇编 | 服务器
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > C/C++教程
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
文章搜索服务
邮件订阅
输入你的邮件地址,
你将不会错过任何关于:
[ C/C++教程 ]的信息

本月文章推荐
.C++箴言:考虑支持不抛异常的swap.
.2000年4月基础知识知识和C语言程.
.数据库程序的精简安装.
.黒白棋.
.链表的C语言实现之动态内存分配.
.瘦身前后——兼谈C++语言进化.
.使用foreach来读取记录.
.三层数据库与应用程序服务器的澄.
.C++中的EXIT与析构函数.
.数据结构C语言实现系列——队列.
.C语言高效编程的的四大绝招.
.C++箴言:避免构造或析构函数中调.
.C++箴言:从模板中分离出参数无关.
.深度探索C++对象模型(3).
.理解C++语言的布尔类型和枚举类型.
.限次使用新法.
.24点扑克牌游戏.
.The Standard C Library for Lin.
.C++还能重新辉煌吗?C++复杂性的.
.C语言入门之指针的慨念(1).

datagridcolumnstyle重写,实现插入不同控件列体验

发表日期:2008-3-8 |


最近谈论较多的就是Datagrid,非凡新手最是郁闷为何没有更好的控件,来满足自已的需求。
其实通过重写可以达到很多不同的功能体验,在这里我们仅仅讨论关于datagridcolumnstyle重写的问题
==========================================
Power by: landlordh
Datatime: 2005-08-04
转载请注明出处,谢谢
==========================================
1。重写TextBox: Public Class XP_TextBox
    Inherits System.Windows.Forms.TextBox
#Region " Windows "     Public Sub New()
        MyBase.New()         InitializeComponent()
    End Sub
    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
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        '
        'TextBox
        '
        Me.EnableContextMenu = True
        Me.EnablePaste = True
        Me.Name = "TextBox"     End Sub #End Region #Region " Variables "     Private m_EnPaste As Boolean = True
    Private m_EnContextMenu As Boolean = True #End Region #Region " Property "     Property EnablePaste() As Boolean
        Get
            Return m_EnPaste
        End Get
        Set(ByVal Value As Boolean)
            m_EnPaste = Value
            Me.Invalidate()
        End Set
    End Property     Property EnableContextMenu() As Boolean
        Get
            Return m_EnContextMenu
        End Get

        Set(ByVal Value As Boolean)
            m_EnContextMenu = Value
            Me.Invalidate()
        End Set
    End Property #End Region     Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        Select Case m.Msg
            Case &H302 'paste
                RaiseEvent PasteEvent()
                If Not m_EnPaste Then Return
            Case &H7B 'contextmenu
                If Not m_EnContextMenu Then Return
        End Select
        MyBase.WndProc(m)
    End Sub     Public Event PasteEvent() End Class 2。重写datagridcolumnstyle(重点介绍内容): Imports System.Drawing
Imports System.Windows.Forms Public NotInheritable Class DataGridTextBoxColumnStyle
    Inherits System.Windows.Forms.DataGridColumnStyle #Region "Declare Property"     Private WithEvents m_TextBox As New Landlord.Component.XP_TextBox
    Private IsEditing As Boolean
    Private EditingRow As Integer = -1
    Private m_oldvalue As String #End Region #Region " windows "     Sub New()
        Me.m_TextBox.Visible = False
    End Sub     Public Sub New(ByVal Container As System.ComponentModel.IContainer)
        MyClass.New()         Container.Add(Me)
    End Sub     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     Private components As System.ComponentModel.IContainer     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        components = New System.ComponentModel.Container
    End Sub #End Region #Region "Get Function"     Protected Overrides Function GetMinimumHeight() As Integer
        Return m_TextBox.PreferredHeight + 2
    End Function     Protected Overrides Function GetPreferredHeight(ByVal g As System.Drawing.Graphics, ByVal value As Object) As Integer
        Return m_TextBox.PreferredHeight + 2
    End Function     Protected Overrides Function GetPreferredSize(ByVal g As System.Drawing.Graphics, ByVal value As Object) As System.Drawing.Size
        Return New Size(50, m_TextBox.PreferredHeight + 2)
    End Function #End Region #Region "Paint"     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)
        Paint(g, bounds, [source], rowNum, False)
    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 alignToRight As Boolean)
        Dim brush_for As Brush = New SolidBrush(Me.DataGridTableStyle.ForeColor)
        Dim brush_bak As Brush = New SolidBrush(Me.DataGridTableStyle.BackColor)
        Paint(g, bounds, [source], rowNum, brush_bak, brush_for, alignToRight)
        brush_for.Dispose()
        brush_bak.Dispose()
    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 Brush, ByVal foreBrush As Brush, ByVal alignToRight As Boolean)
        Dim str As String
        If IsDBNull(GetColumnValueAtRow([source], rowNum)) Then
            str = Me.NullText
        Else
            str = CType(GetColumnValueAtRow([source], rowNum), String)
        End If
        Dim brush As Brush = backBrush
        Dim rect As System.Drawing.Rectangle = bounds

        g.FillRectangle(brush, rect)
        If Me.IsEditing And EditingRow = rowNum Then
            brush = New SolidBrush(Color.White)
            g.FillRectangle(brush, bounds)
        End If
        rect.Offset(0, 2)
        rect.Height -= 2
        brush = New SolidBrush(Me.DataGridTableStyle.ForeColor)
        If Me.DataGridTableStyle.DataGrid.IsSelected(rowNum) Then
            brush = New SolidBrush(Me.DataGridTableStyle.SelectionBackColor)
            Dim rectf As RectangleF = New RectangleF(bounds.X, bounds.Y, bounds.Width, bounds.Height)
            g.FillRectangle(brush, rectf)
            brush = New SolidBrush(Me.DataGridTableStyle.SelectionForeColor)
        End If
        If Me.Alignment = HorizontalAlignment.Center Then
            Dim w As Integer = g.MeasureString(str, Me.DataGridTableStyle.DataGrid.Font, New SizeF(bounds.Width, bounds.Height)).Width
            rect.X = rect.X + (bounds.Width - w) / 2
        ElseIf Me.Alignment = HorizontalAlignment.Right Then
            Dim w As Integer = g.MeasureString(str, Me.DataGridTableStyle.DataGrid.Font, New SizeF(bounds.Width, bounds.Height)).Width
            rect.X = bounds.Right - w
        End If
        g.DrawString(str, Me.DataGridTableStyle.DataGrid.Font, brush, rect.X, rect.Y)
        brush.Dispose()
    End Sub #End Region #Region "Overrides Method"     Protected Overrides Function Commit(ByVal dataSource As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer) As Boolean
        Me.m_TextBox.Bounds = Rectangle.Empty
        If Not Me.IsEditing Then
            Return True
        End If
        EditingRow = -1
        IsEditing = False

        Try
            Dim value As Object
            value = m_TextBox.Text
            If NullText.Equals(value) Then
                value = System.DBNull.Value
            End If
            SetColumnValueAtRow(dataSource, rowNum, value)
        Catch ex As Exception
            Abort(rowNum)
            Return False
        End Try
        invalidate()
        Return True
    End Function     Protected Overrides Sub Abort(ByVal rowNum As Integer)
        Me.m_TextBox.Text = m_oldvalue         EditingRow = -1         If m_TextBox.Focused Then
            Me.DataGridTableStyle.DataGrid.Focus()
        End If
        Me.m_TextBox.Visible = False         Me.IsEditing = False
        Me.Invalidate()
    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)
        EditingRow = rowNum
        IsEditing = True
        Dim value As String
        If IsDBNull(GetColumnValueAtRow(source, rowNum)) Then
            value = Me.NullText
        Else
            value = CType(GetColumnValueAtRow(source, rowNum), String)
        End If
        m_oldvalue = value
        If cellIsVisible Then
            If Not Me.ReadOnly Then
                Me.m_TextBox.Bounds = New Rectangle(bounds.X + 1, bounds.Y + 1, bounds.Width - 2, bounds.Height - 2)

                Me.m_TextBox.Text = value
                Me.m_TextBox.Select()
                Me.m_TextBox.Focus()
                Me.m_TextBox.SelectAll()
                Me.m_TextBox.Visible = True
                Me.m_TextBox.Flat = True
            End If
        Else
            Me.m_TextBox.Text = value
            '滚动时会丢失焦点
            'Me.m_TextBox.Visible = False
        End If
        If Me.m_TextBox.Visible Then
            DataGridTableStyle.DataGrid.Invalidate(bounds)
        End If
    End Sub     Protected Overloads Overrides Sub SetDataGridInColumn(ByVal value As System.Windows.Forms.DataGrid)
        MyBase.SetDataGridInColumn(value)
        If Not m_TextBox.Parent Is Nothing Then
            m_TextBox.Parent.Controls.Remove(Me.m_TextBox)
        End If
        If Not value Is Nothing Then
            value.Controls.Add(Me.m_TextBox)
        End If
    End Sub     Protected Overrides Sub ConcedeFocus()
        EditingRow = -1
        '否则先点到新增行,再回选非新行格时该列最后一行的值变为(null)
        IsEditing = False
        Me.m_TextBox.Visible = False
        invalidate()
    End Sub     Protected Overrides Sub EnterNullValue()
        Me.m_TextBox.Text = Me.NullText
    End Sub     Private Sub m_TextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles m_TextBox.KeyPress
        If Not Char.IsControl(e.KeyChar) Then

            Me.IsEditing = True
            MyBase.ColumnStartedEditing(m_TextBox)
        End If
    End Sub     Private Sub m_TextBox_PasteEvent() Handles m_TextBox.PasteEvent
        Me.IsEditing = True
        Me.ColumnStartedEditing(m_TextBox)
    End Sub #End Region End Class 3。使用:
新建一个空窗体,拖入datagrid,窗体load事件中代码如下
Private idtb_temp As New DataTable Private Sub form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load     idtb_temp = New DataTable("NameTable")
    idtb_temp.Columns.Add(New DataColumn("normal"))
    idtb_temp.Columns.Add(New DataColumn("textbox1"))
    idtb_temp.Columns.Add(New DataColumn("combobox1"))
    Dim dateColumns As DataColumn
    dateColumns = New DataColumn("datetime1", Type.GetType("System.DateTime"))
    idtb_temp.Columns.Add(dateColumns)
    idtb_temp.Columns.Add(New DataColumn("checkbox1", Type.GetType("System.Boolean")))     Dim idrw_row As DataRow
    Dim i As Integer
    For i = 0 To 20
        idrw_row = idtb_temp.NewRow
        idrw_row.Item("normal") = "names"
        idrw_row.Item("textbox1") = "nick"
        idrw_row.Item("combobox1") = i.ToString
        idrw_row.Item("datetime1") = "2004-06-04"
        idrw_row.Item("checkbox1") = True
        idtb_temp.Rows.Add(idrw_row)
    Next     Me.DataGrid1.DataSource = idtb_temp     Dim myGridStyle As Windows.Forms.DataGridTableStyle = New Windows.Forms.DataGridTableStyle
    myGridStyle.MappingName = "NameTable"
    myGridStyle.PreferredRowHeight = 30
    myGridStyle.SelectionBackColor = Color.Blue
    myGridStyle.BackColor = Color.Yellow
    Dim c1 As Windows.Forms.DataGridTextBoxColumn = New Windows.Forms.DataGridTextBoxColumn
    With c1
        .MappingName = "normal"
        .Width = 100
        .HeaderText = "normal"
        .Alignment = HorizontalAlignment.Center
    End With
    myGridStyle.GridColumnStyles.Add(c1)     Dim c2 As Landlord.Component.DragGrid.DataGridTextBoxColumnStyle = New Landlord.Component.DragGrid.DataGridTextBoxColumnStyle

    With c2
        .MappingName = "textbox1"
        .Width = 100
        .HeaderText = "textbox1"
    End With
    myGridStyle.GridColumnStyles.Add(c2)     Dim c3 As Landlord.Component.DragGrid.DataGridComboBoxColumnStyle = New Landlord.Component.DragGrid.DataGridComboBoxColumnStyle
    With c3
        .MappingName = "combobox1"
        .HeaderText = "combobox1"
        .Width = 100
        .AddItem("111")
        .AddItem("222")
        .Alignment = HorizontalAlignment.Center
    End With
    myGridStyle.GridColumnStyles.Add(c3)     Dim c4 As Landlord.Component.DragGrid.DataGridDateTimePickerColumnStyle = New Landlord.Component.DragGrid.DataGridDateTimePickerColumnStyle
    With c4
        .MappingName = "datetime1"
        .HeaderText = "datetime1"
        .Width = "100"
        .Alignment = HorizontalAlignment.Center
    End With
    myGridStyle.GridColumnStyles.Add(c4)     Dim c5 As Landlord.Component.DragGrid.DataGridCheckBoxColumnStyle = New Landlord.Component.DragGrid.DataGridCheckBoxColumnStyle
    With c5
        .MappingName = "checkbox1"
        .HeaderText = "checkbox1"
        .Width = 100
        .TrueColor = Color.Red
    End With
    myGridStyle.GridColumnStyles.Add(c5)     Me.DataGrid1.TableStyles.Clear()
    Me.DataGrid1.TableStyles.Add(myGridStyle) End Sub 4。说明:
其他控件的加入原理基本一样,这里就不重复了
上一篇:switch语句的“不准遍历” 人气:381
下一篇:一个四舍五入函数 人气:1215
浏览全部C/C++的内容 Dreamweaver插件下载 常用网页广告代码全集
  最新网站源码 最新软件下载
2008-9-8 美女写真小偷程序联盟 v3.0 后台
2008-9-7 站长中国企业(公司)网站系统 v4.2
2008-9-7 PBDigg v2.0 Build 20080821
2008-9-7 玩玩小游戏FLASH系统 v2.1
2008-9-7 522QQ在线电视直播程序 v1.1
2008-9-7 Pcook cms 文章管理系统 (老Y CM
2008-9-7 仿代码小说小偷系统 v1.0
2008-9-7 百度一搜集成搜索管理系统
2008-9-7 小贤统计器 v1.0
2008-9-7 iBlacklist通话黑名单汉化破解补
2008-9-7 EndlessWalls无尽壁纸 v1.0.4破解
2008-9-7 Dynolicious车载测量仪v1.1破解版
2008-9-7 iVoodoo巫毒娃娃1.0.1破解版
2008-9-7 iWallpape精品墙纸1.2破解版
2008-9-7 iChillout自然音效工具1.1破解版
2008-9-7 Todo计划提醒1.2破解版
2008-9-7 allRadio电台集合1.01破解版
2008-9-7 My Money个人理财1.0破解版
  发表评论
姓 名: 验证码:
内 容:
站长工具:网站收录查询 | Google PR查询 | ALEXA排名查询 | CSS在线编辑器 | 广告代码 | Html转换js | js/vbs加密 | md5加密 | 进制转换
实用工具:汉字翻译拼音 | 符号对照表 | 个税计算 | 经典小工具 | 汉字简繁转换 | 普通单位换算 | 公制单位换算 | 生辰老黄历 | 国内电话区号 国家代码与域名缩写 | 文字加密解密 | 健康查询 | 万年历 | 汉字横竖排版 | 手机号码查询 | 计算器 | ip搜索
业务联系 | 广告刊登 | 频道合作 | 投稿荐稿 | 联系方式 | 加入收藏 | RSS订阅
Copyright © 2000-2008 www.knowsky.com All rights reserved | 网络实名:动态网站制作指南 | 沪ICP备05001343号
ホームページ制作 不動産検索システム 求人情報
防水工事·改修工事 フットサル大会 探偵