TreeView 的應用...
TreeView的使用:
我們用一個例子來說明TreeView 的使用,這個例子要實現的效果是這樣的,在Form 上這樣幾個控件,一個DataTimePicker ,兩個TextBox,一個DataGrid,一個TreeView,和一個Button。首先我們選擇日期,然後在TextBox中填入對應的內容,然後在點擊Button,那麼會在TreeView 記錄下這條記錄的日期,如果在同一天有很多記錄,日期也只顯示一次…
通常 我們要在TreeView中實現幾個節點,我們就會定義幾個 TreeNode
代碼部分:
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
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
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
Friend WithEvents TreeView1 As System.Windows.Forms.TreeView
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents Splitter1 As System.Windows.Forms.Splitter
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents DateTimePicker1 As System.Windows.Forms.DateTimePicker
Friend WithEvents ToolTip1 As System.Windows.Forms.ToolTip
Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
Me.DataGrid1 = New System.Windows.Forms.DataGrid
Me.TreeView1 = New System.Windows.Forms.TreeView
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.Splitter1 = New System.Windows.Forms.Splitter
Me.Button2 = New System.Windows.Forms.Button
Me.DateTimePicker1 = New System.Windows.Forms.DateTimePicker
Me.ToolTip1 = New System.Windows.Forms.ToolTip(Me.components)
Me.ComboBox1 = New System.Windows.Forms.ComboBox
Me.Button1 = New System.Windows.Forms.Button
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'DataGrid1
'
Me.DataGrid1.DataMember = ""
Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.DataGrid1.Location = New System.Drawing.Point(164, 4)
Me.DataGrid1.Name = "DataGrid1"
Me.DataGrid1.Size = New System.Drawing.Size(536, 236)
Me.DataGrid1.TabIndex = 0
'
'TreeView1
'
Me.TreeView1.Dock = System.Windows.Forms.DockStyle.Left
Me.TreeView1.ImageIndex = -1
Me.TreeView1.Location = New System.Drawing.Point(0, 0)
Me.TreeView1.Name = "TreeView1"
Me.TreeView1.SelectedImageIndex = -1
Me.TreeView1.Size = New System.Drawing.Size(154, 343)
Me.TreeView1.TabIndex = 1
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(308, 252)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.TabIndex = 2
Me.TextBox1.Text = ""
'
'TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(424, 252)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.TabIndex = 3
Me.TextBox2.Text = ""
'
'Splitter1
'
Me.Splitter1.Location = New System.Drawing.Point(154, 0)
Me.Splitter1.Name = "Splitter1"
Me.Splitter1.Size = New System.Drawing.Size(3, 343)
Me.Splitter1.TabIndex = 5
Me.Splitter1.TabStop = False
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(532, 310)
Me.Button2.Name = "Button2"
Me.Button2.TabIndex = 6
Me.Button2.Text = "Button2"
'
'DateTimePicker1
'
Me.DateTimePicker1.Location = New System.Drawing.Point(176, 252)
Me.DateTimePicker1.Name = "DateTimePicker1"
Me.DateTimePicker1.Size = New System.Drawing.Size(122, 20)
Me.DateTimePicker1.TabIndex = 7
'
'ComboBox1
'
Me.ComboBox1.Location = New System.Drawing.Point(568, 250)
Me.ComboBox1.Name = "ComboBox1"
Me.ComboBox1.Size = New System.Drawing.Size(121, 21)
Me.ComboBox1.TabIndex = 8
Me.ComboBox1.Text = "ComboBox1"
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(334, 294)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 9
Me.Button1.Text = "Button1"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(702, 343)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.ComboBox1)
Me.Controls.Add(Me.DateTimePicker1)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Splitter1)
Me.Controls.Add(Me.TextBox2)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.TreeView1)
Me.Controls.Add(Me.DataGrid1)
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Private RootNote As New TreeNode
Private YearNote As New TreeNode
Private MonthNote As New TreeNode
Private DayNote As New TreeNode
Private idtb_temp As New DataTable
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
idtb_temp.Columns.Add(New DataColumn("idte_date", Type.GetType("System.DateTime")))
idtb_temp.Columns.Add(New DataColumn("value_1", Type.GetType("System.String")))
idtb_temp.Columns.Add(New DataColumn("value_2", Type.GetType("System.String")))
idtb_temp.TableName = "xo"
Dim ldgst_style As New DataGridTableStyle
Dim ldcl_header As DataGridTextBoxColumn
ldgst_style.MappingName = "xo"
ldcl_header = New DataGridTextBoxColumn
ldcl_header.MappingName = "idte_date"
ldcl_header.HeaderText = "NO_0"
ldgst_style.GridColumnStyles.Add(ldcl_header)
ldcl_header = New DataGridTextBoxColumn
ldcl_header.MappingName = "value_1"
ldcl_header.HeaderText = "NO_1"
ldgst_style.GridColumnStyles.Add(ldcl_header)
ldcl_header = New DataGridTextBoxColumn
ldcl_header.MappingName = "value_2"
ldcl_header.HeaderText = "NO_2"
ldgst_style.GridColumnStyles.Add(ldcl_header)
'Dim idrw_row As DataRow
'idrw_row = idtb_temp.NewRow
'idrw_row.Item("value_1") = 11
'idrw_row.Item("value_2") = 22
'idtb_temp.Rows.Add(idrw_row)
Me.DataGrid1.TableStyles.Add(ldgst_style)
Me.idtb_temp.DefaultView.AllowNew = False
' Me.DataGrid1.DataSource = Me.idtb_temp
Me.TextBox1.Focus()
GetDate()
Me.RootNote.Text = "我的記事"
'Me.TreeView1.CheckBoxes = True
Me.TreeView1.Nodes.Add(Me.RootNote)
End Sub
Private Sub AddNewItem()
Dim ldrw_row As DataRow
ldrw_row = idtb_temp.NewRow
ldrw_row.Item("idte_date") = Me.DateTimePicker1.Value.ToShortDateString
ldrw_row.Item("value_1") = Me.TextBox1.Text
ldrw_row.Item("value_2") = Me.TextBox2.Text
Me.idtb_temp.Rows.Add(ldrw_row)
If Me.DateTimePicker1.Value.ToShortDateString.Trim <> "" Then
Dim YearFound, MonthFound, DayFound As Boolean
For Each Me.YearNote In Me.RootNote.Nodes
If Me.YearNote.Text.ToUpper = Me.DateTimePicker1.Value.Year.ToString.ToUpper & "年" Then
YearFound = True
Exit For
End If
Next
If Not YearFound Then
Me.YearNote = Me.RootNote.Nodes.Add(Me.DateTimePicker1.Value.Year.ToString.ToUpper & "年")
End If
If Not Me.YearNote Is Nothing Then
For Each Me.MonthNote In Me.YearNote.Nodes
If Me.MonthNote.Text.ToUpper = Me.DateTimePicker1.Value.Month.ToString.ToUpper & "月" Then
MonthFound = True
Exit For
End If
Next
If Not MonthFound Then
Me.MonthNote = Me.YearNote.Nodes.Add(Me.DateTimePicker1.Value.Month.ToString.ToUpper & "月")
End If
End If
If Not Me.YearNote Is Nothing Then
If Not Me.MonthNote Is Nothing Then
For Each Me.DayNote In Me.MonthNote.Nodes
If Me.DayNote.Text.ToUpper = Me.DateTimePicker1.Value.Day.ToString.ToUpper & "日" Then
DayFound = True
Exit For
End If
Next
End If
End If
If Not DayFound Then
Me.DayNote = Me.MonthNote.Nodes.Add(Me.DateTimePicker1.Value.Day.ToString.ToUpper & "日")
End If
End If
Me.TreeView1.ExpandAll()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.AddNewItem()
End Sub
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
Me.TextBox2.Focus()
End If
End Sub
Private Sub TextBox2_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyDown
If e.KeyCode = 13 Then
Me.AddNewItem()
Me.TextBox1.Focus()
Me.TextBox1.Text = String.Empty
Me.TextBox2.Text = String.Empty
End If
End Sub
Private Sub TreeView1_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
For Each Me.DayNote In Me.MonthNote.Nodes
If Me.TreeView1.SelectedNode.Text = Me.TreeView1.Nodes(0).Nodes(Me.TreeView1.SelectedNode.Parent.Parent.Index).Nodes(Me.TreeView1.SelectedNode.Parent.Index).Nodes(Me.TreeView1.SelectedNode.Index).Text Then
Me.TreeView1.SelectedNode.ForeColor = System.Drawing.Color.Red
' System.Windows.Forms.MessageBox.Show(Me.TreeView1.SelectedNode.Parent.Parent.Text & Me.TreeView1.SelectedNode.Parent.Text & Me.TreeView1.SelectedNode.Text)
Dim istg_date As DateTime
istg_date = Me.TreeView1.SelectedNode.Parent.Parent.Text & Me.TreeView1.SelectedNode.Parent.Text & Me.TreeView1.SelectedNode.Text
' MessageBox.Show(istg_date)
' MessageBox.Show(istg_date.ToString("yyyy年MM月dd日"))
Dim Con As OleDb.OleDbConnection = New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & Application.StartupPath & "/xo.mdb;")
Dim Com As OleDb.OleDbCommand = New OleDb.OleDbCommand
Com.Connection = Con
Con.Open()
Dim DAp As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter("select * from xx_o where idte_date='" & istg_date.ToString("dd/MM/yyyy") & "'", Con)
Dim kk As New DataTable
DAp.Fill(kk)
Me.DataGrid1.DataSource = kk
End If
Next
' MessageBox.Show(Me.TreeView1.SelectedNode.Parent.Text)
End Sub
Public Function GetDate()
Dim Con As OleDb.OleDbConnection = New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & Application.StartupPath & "/xo.mdb;")
Dim Com As OleDb.OleDbCommand = New OleDb.OleDbCommand
Com.Connection = Con
Con.Open()
Dim DAp As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter("select * from xx_o", Con)
DAp.Fill(Me.idtb_temp)
' Me.DataGrid1.DataSource = idtb_temp
End Function
Public Function Fill_Tree()
If Me.idtb_temp.Rows.Count > 0 Then
For lint_row As Integer = 0 To Me.idtb_temp.Rows.Count - 1
Dim YearFound, MonthFound, DayFound As Boolean
For Each Me.YearNote In Me.RootNote.Nodes
If Me.YearNote.Text.ToUpper = Me.idtb_temp.Rows(lint_row).Item("idte_date").Year.ToString.ToUpper & "年" Then
YearFound = True
Exit For
End If
Next
If Not YearFound Then
Me.YearNote = Me.RootNote.Nodes.Add(Me.idtb_temp.Rows(lint_row).Item("idte_date").Year.ToString.ToUpper & "年")
End If
If Not Me.YearNote Is Nothing Then
For Each Me.MonthNote In Me.YearNote.Nodes
If Me.MonthNote.Text.ToUpper = Me.idtb_temp.Rows(lint_row).Item("idte_date").Month.ToString.ToUpper & "月" Then
MonthFound = True
Exit For
End If
Next
If Not MonthFound Then
Me.MonthNote = Me.YearNote.Nodes.Add(Me.idtb_temp.Rows(lint_row).Item("idte_date").Month.ToString.ToUpper & "月")
End If
End If
If Not Me.YearNote Is Nothing Then
If Not Me.MonthNote Is Nothing Then
For Each Me.DayNote In Me.MonthNote.Nodes
If Me.DayNote.Text.ToUpper = Me.idtb_temp.Rows(lint_row).Item("idte_date").Day.ToString.ToUpper & "日" Then
DayFound = True
Exit For
End If
Next
End If
End If
If Not DayFound Then
Me.DayNote = Me.MonthNote.Nodes.Add(Me.idtb_temp.Rows(lint_row).Item("idte_date").Day.ToString.ToUpper & "日")
End If
Next
Me.TreeView1.ExpandAll()
End If
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Fill_Tree()
End Sub
End Class
效果圖:
效果圖: