明辉站/网站教程/内容

[NET.VB]小问题集锦(供入门者参考)

网站教程2024-02-18 阅读
[摘要]声明:1、所有代码不注明出处的皆为本人所写2、所有代码均在本机调试通过3、本贴适用于初学者(本人也是)4、欢迎各位仁兄斧正,提供想法或代码5、本人力求每日补充内容6、非本人同意,请勿转载本人所写的代码7、各位说要不要上面第6条?8、哈,声明是这样写的吗,一点都不严肃,也不规范 ...
声明:

1、所有代码不注明出处的皆为本人所写

2、所有代码均在本机调试通过

3、本贴适用于初学者(本人也是)

4、欢迎各位仁兄斧正,提供想法或代码

5、本人力求每日补充内容

6、非本人同意,请勿转载本人所写的代码

7、各位说要不要上面第6条?

8、哈,声明是这样写的吗,一点都不严肃,也不规范

水如烟 2004.7.19

[控件类]

TreeView

1、给节点加个ToolTip(2004.7.19)

Private tmpTreeNode As TreeNode
Private mTreeViewToolTip As New ToolTip

Private Sub TreeView1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TreeView1.MouseMove
Dim mNode As TreeNode
mNode = Me.TreeView1.GetNodeAt(e.X, e.Y)
'当前没节点
If mNode Is Nothing Then
mTreeViewToolTip.SetToolTip(Me.TreeView1, Nothing)
Exit Sub
End If
'超过当前Node的边界
If mNode.Bounds.Right < e.X OrElse mNode.Bounds.Left > e.X Then
mTreeViewToolTip.SetToolTip(Me.TreeView1, Nothing)
Exit Sub
End If
'同一个Node
If mNode Is tmpTreeNode Then
Exit Sub
End If
mTreeViewToolTip.AutomaticDelay = 500
mTreeViewToolTip.SetToolTip(Me.TreeView1, mNode.Text)
tmpTreeNode = mNode
End Sub



DataGrid

1、自适应各列长度(2004.7.19)

'定义一个字段信息类
Private Class ColumnInfo
Public [Name] As String
Public [DataType] As String
Public MaxWidth As Integer = 0
Public Sub CompareStringLength(ByVal mString As String)
Dim mLength As Integer
mLength = System.Text.Encoding.Default.GetBytes(mString).Length()
If MaxWidth < mLength Then MaxWidth = mLength
End Sub
Public Function ColumnWidth(ByVal mDataGrid As DataGrid) As Integer
Dim mGraphics As Graphics = mDataGrid.CreateGraphics
Dim mColWidth As Single
mColWidth = mGraphics.MeasureString(New String(CType("A", Char), MaxWidth), mDataGrid.Font).Width + 2
Return CType(mColWidth, Integer)
End Function
End Class


Private Sub MakeDataGridAutoExtend(ByVal mDataGrid As DataGrid)
'判断mDataGrid数据源类型
'如果绑定的是DataSet或DataViewManager或没有绑定任何数据源,则退出,
If TypeOf mDataGrid.DataSource Is System.Data.DataSet OrElse _
TypeOf mDataGrid.DataSource Is System.Data.DataViewManager OrElse _
mDataGrid.DataSource Is Nothing Then Exit Sub

'以下分别考虑两种数据源,一是DataView,一是DataTable
Dim dt As DataTable
If TypeOf mDataGrid.DataSource Is System.Data.DataView Then
dt = CType(mDataGrid.DataSource, DataView).Table
Else
dt = CType(mDataGrid.DataSource, DataTable)
End If

'取各字段信息
Dim mColumnInfo(dt.Columns.Count - 1) As ColumnInfo

Dim i As Integer = 0

Dim mColumn As DataColumn
For Each mColumn In dt.Columns
Dim mInfo As New ColumnInfo
With mInfo
.Name = mColumn.ColumnName
.DataType = mColumn.DataType.ToString
.CompareStringLength( mColumn.ColumnName)
End With
mColumnInfo(i) = mInfo
i += 1
Next

'取各字段的最大长度
Dim mRow As DataRow
For Each mRow In dt.Rows
For i = 0 To dt.Columns.Count - 1
If Not IsDBNull(mRow(i)) Then
mColumnInfo(i).CompareStringLength(CType(mRow(i), String))
End If
Next
Next

'建DataGridTableStyle
Dim ts As New DataGridTableStyle
ts.MappingName = dt.TableName

For i = 0 To dt.Columns.Count - 1
If mColumnInfo(i).DataType.Equals("System.Boolean") Then
'这是Boolean字段
Dim blnCol As New DataGridBoolColumn
With blnCol
.MappingName = mColumnInfo(i).Name
.HeaderText = mColumnInfo(i).Name
.Width = mColumnInfo(i).ColumnWidth(mDataGrid)
.NullText = ""
End With
ts.GridColumnStyles.Add(blnCol)
Else
'非Boolean字段
Dim TxtCol As New DataGridTextBoxColumn
With TxtCol
.MappingName = mColumnInfo(i).Name
.HeaderText = mColumnInfo(i).Name
.Width = mColumnInfo(i).ColumnWidth(mDataGrid)
.NullText = ""
.ReadOnly = False '这里可以设置为只读
.Format = "" '这里可以设置显示格式,要显示日时分秒的就在这设
End With
ts.GridColumnStyles.Add(TxtCol)
End If
Next

mDataGrid.TableStyles.Clear()
mDataGrid.TableStyles.Add(ts)
'至于其它的功能,比如绑定事件之类的,后面再补充
End Sub
 

'调用
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
MakeDataGridAutoExtend(Me.DataGrid1)
En

……

相关阅读