明輝手游網(wǎng)中心:是一個免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺!

[NET.VB]小問題集錦(供入門者參考)

[摘要]聲明:1、所有代碼不注明出處的皆為本人所寫2、所有代碼均在本機(jī)調(diào)試通過3、本貼適用于初學(xué)者(本人也是)4、歡迎各位仁兄斧正,提供想法或代碼5、本人力求每日補(bǔ)充內(nèi)容6、非本人同意,請勿轉(zhuǎn)載本人所寫的代碼7、各位說要不要上面第6條?8、哈,聲明是這樣寫的嗎,一點都不嚴(yán)肅,也不規(guī)范 ...
聲明:

1、所有代碼不注明出處的皆為本人所寫

2、所有代碼均在本機(jī)調(diào)試通過

3、本貼適用于初學(xué)者(本人也是)

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

5、本人力求每日補(bǔ)充內(nèi)容

6、非本人同意,請勿轉(zhuǎn)載本人所寫的代碼

7、各位說要不要上面第6條?

8、哈,聲明是這樣寫的嗎,一點都不嚴(yán)肅,也不規(guī)范

水如煙 2004.7.19

[控件類]

TreeView

1、給節(jié)點加個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)
'當(dāng)前沒節(jié)點
If mNode Is Nothing Then
mTreeViewToolTip.SetToolTip(Me.TreeView1, Nothing)
Exit Sub
End If
'超過當(dāng)前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、自適應(yīng)各列長度(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數(shù)據(jù)源類型
'如果綁定的是DataSet或DataViewManager或沒有綁定任何數(shù)據(jù)源,則退出,
If TypeOf mDataGrid.DataSource Is System.Data.DataSet OrElse _
TypeOf mDataGrid.DataSource Is System.Data.DataViewManager OrElse _
mDataGrid.DataSource Is Nothing Then Exit Sub

'以下分別考慮兩種數(shù)據(jù)源,一是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 '這里可以設(shè)置為只讀
.Format = "" '這里可以設(shè)置顯示格式,要顯示日時分秒的就在這設(shè)
End With
ts.GridColumnStyles.Add(TxtCol)
End If
Next

mDataGrid.TableStyles.Clear()
mDataGrid.TableStyles.Add(ts)
'至于其它的功能,比如綁定事件之類的,后面再補(bǔ)充
End Sub
 

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