分享
 
 
 

Henry手记:WinForm Datagrid结构剖析(三)类代码

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

-------------------X类的代码X---------------------

Option Strict On

Option Explicit On

Imports System.Collections

Imports System.ComponentModel

Imports System.Drawing

Imports System.Windows.Forms

Imports System.Data

Public Class DataGridComboBox

‘与DataGridTextBox类相类似地定义一个在下拉框列类中使用的ComboBox

Inherits ComboBox

Public Sub New()

MyBase.New()

End Sub

Public isInEditOrNavigateMode As Boolean = True

End Class

Public Class DataGridComboBoxColumn

Inherits DataGridColumnStyle

' 与用户界面相关的变量

Private xMargin As Integer = 2

Private yMargin As Integer = 1

Private Combo As DataGridComboBox

Private _DisplayMember As String

Private _ValueMember As String

' 用于跟踪编辑状态变化的变量

Private OldVal As String = String.Empty

Private InEdit As Boolean = False

'构造函数 – 实例的DisplayMember, ValueMember值为由父类传来的integer类型的值

Public Sub New(ByRef DataSource As DataTable, _

ByVal DisplayMember As Integer, _

ByVal ValueMember As Integer)

Combo = New DataGridComboBox()

_DisplayMember = DataSource.Columns.Item(index:=DisplayMember).ToString

_ValueMember = DataSource.Columns.Item(index:=ValueMember).ToString

With Combo

.Visible = False

.DataSource = DataSource

.DisplayMember = _DisplayMember

.ValueMember = _ValueMember

End With

End Sub

'构造函数– 实例的DisplayMember, ValueMember 是String类型的值

Public Sub New(ByRef DataSource As DataTable, _

ByVal DisplayMember As String, _

ByVal ValueMember As String)

Combo = New DataGridComboBox()

With Combo

.Visible = False

.DataSource = DataSource

.DisplayMember = DisplayMember

.ValueMember = ValueMember

End With

End Sub

'------------------------------------------------------

'从 DataGridColumnStyle类继承下来的方法

'------------------------------------------------------

' 焦点离开combobox格后的改变

Protected Overloads Overrides Sub Abort(ByVal RowNum As Integer)

Debug.WriteLine("Abort()")

RollBack()

HideComboBox()

EndEdit()

End Sub

' 接受改变

Protected Overloads Overrides Function Commit(ByVal DataSource As CurrencyManager, _

ByVal RowNum As Integer) As Boolean

HideComboBox()

If Not InEdit Then

Return True

End If

Try

Dim Value As Object = Combo.SelectedValue

If NullText.Equals(Value) Then

Value = Convert.DBNull

End If

SetColumnValueAtRow(DataSource, RowNum, Value)

Catch e As Exception

RollBack()

Return False

End Try

EndEdit()

Return True

End Function

' 移开聚焦

Protected Overloads Overrides Sub ConcedeFocus()

Combo.Visible = False

End Sub

' 编辑单元格

Protected Overloads Overrides Sub Edit(ByVal Source As CurrencyManager, _

ByVal Rownum As Integer, _

ByVal Bounds As Rectangle, _

ByVal [ReadOnly] As Boolean, _

ByVal InstantText As String, _

ByVal CellIsVisible As Boolean)

Combo.Text = String.Empty

Dim OriginalBounds As Rectangle = Bounds

OldVal = Combo.Text

If CellIsVisible Then

Bounds.Offset(xMargin, yMargin)

Bounds.Width -= xMargin * 2

Bounds.Height -= yMargin

Combo.Bounds = Bounds

Combo.Visible = True

Else

Combo.Bounds = OriginalBounds

Combo.Visible = False

End If

Combo.SelectedValue = GetText(GetColumnValueAtRow(Source, Rownum))

If Not InstantText Is Nothing Then

Combo.SelectedValue = InstantText

End If

Combo.RightToLeft = Me.DataGridTableStyle.DataGrid.RightToLeft

Combo.Focus()

If InstantText Is Nothing Then

Combo.SelectAll()

Else

Dim [End] As Integer = Combo.Text.Length

Combo.Select([End], 0)

End If

If Combo.Visible Then

DataGridTableStyle.DataGrid.Invalidate(OriginalBounds)

End If

InEdit = True

End Sub

Protected Overloads Overrides Function GetMinimumHeight() As Integer

' 设置combobox的最小高度

Return Combo.PreferredHeight + yMargin

End Function

Protected Overloads Overrides Function GetPreferredHeight(ByVal g As Graphics, _

ByVal Value As Object) As Integer

Debug.WriteLine("GetPreferredHeight()")

Dim NewLineIndex As Integer = 0

Dim NewLines As Integer = 0

Dim ValueString As String = Me.GetText(Value)

Do

While NewLineIndex <> -1

NewLineIndex = ValueString.IndexOf("r\n", NewLineIndex + 1)

NewLines += 1

End While

Loop

Return FontHeight * NewLines + yMargin

End Function

Protected Overloads Overrides Function GetPreferredSize(ByVal g As Graphics, _

ByVal Value As Object) As Size

Dim Extents As Size = Size.Ceiling(g.MeasureString(GetText(Value), _

Me.DataGridTableStyle.DataGrid.Font))

Extents.Width += xMargin * 2 + DataGridTableGridLineWidth

Extents.Height += yMargin

Return Extents

End Function

Protected Overloads Overrides Sub Paint(ByVal g As Graphics, _

ByVal Bounds As Rectangle, _

ByVal Source As CurrencyManager, _

ByVal RowNum As Integer)

Paint(g, Bounds, Source, RowNum, False)

End Sub

Protected Overloads Overrides Sub Paint(ByVal g As Graphics, _

ByVal Bounds As Rectangle, _

ByVal Source As CurrencyManager, _

ByVal RowNum As Integer, _

ByVal AlignToRight As Boolean)

Dim Text As String = GetText(GetColumnValueAtRow(Source, RowNum))

PaintText(g, Bounds, Text, AlignToRight)

End Sub

Protected Overloads Sub Paint(ByVal g As Graphics, _

ByVal Bounds As Rectangle, _

ByVal Source As CurrencyManager, _

ByVal RowNum As Integer, _

ByVal BackBrush As

Brush, _

ByVal ForeBrush As

Brush, _

ByVal AlignToRight As Boolean)

Dim Text As String = GetText(GetColumnValueAtRow(Source, RowNum))

PaintText(g, Bounds, Text, BackBrush, ForeBrush, AlignToRight)

End Sub

Protected Overloads Overrides Sub SetDataGridInColumn(ByVal Value As DataGrid)

MyBase.SetDataGridInColumn(Value)

If Not (Combo.Parent Is Value) Then

If Not (Combo.Parent Is Nothing) Then

Combo.Parent.Controls.Remove(Combo)

End If

End If

If Not (Value Is Nothing) Then Value.Controls.Add(Combo)

End Sub

Protected Overloads Overrides Sub UpdateUI(ByVal Source As CurrencyManager, _

Val RowNum As Integer, ByVal InstantText As String)

Combo.Text = GetText(GetColumnValueAtRow(Source, RowNum))

If Not (InstantText Is Nothing) Then

Combo.Text = InstantText

End If

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- 王朝網路 版權所有