TreeView app ...

xiaoxiao2021-03-06  14

TreeView's use:

We use an example to illustrate the use of TreeView, this example is the effect of this is like this, such as such controls, a DataTimePicker, two Textbox, a DataGrid, a TreeView, and a Button. First we select the date, then fill in the corresponding content in TextBox, then click Button, then the date of this record will be recorded in TreeView. If there is a lot of records on the same day, the date is only displayed once ...

Usually we have to implement a few nodes in TreeView, we will define a few Treenode

Code section:

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 anyinitization 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 Private Sub InitializationComponent ()

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 E AS ISTEM.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 = "My Notes"

'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 <> "" "

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 & "Year" THEN

Yearfound = TRUE

EXIT for

END IF

NEXT

IF not yearfound then

Me.yearnote = me.rootnote.nodes.add (me.datetimepicker1.value.year.tostring.toupper & "year")

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 & "Month" THEN

Monthfound = TRUE

EXIT for

END IF

NEXT

IF not Monthfound Then

Me.monthnote = me.yearnote.nodes.add (me.datetimepicker1.value.month.tostring.toupper & "Month")

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 & "Day" 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 & "day")

END IF

END IF

Me.treeView1.expandall ()

End Sub

Private sub Button2_click (byvalgend, byval e as system.eventargs) Handles Button2.click

Me.AddnewItem ()

End Sub

Private sub textbox1_keydown (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 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 e as system.windows.forms.treeviewEventArgs) Handles TreeView1.AfTerselectlectElectlect

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.terext & 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 Moon DD Day)))))

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.OLDBDATAADAPTER = New OLEDB.OLDBDATAADAPTER ("Select * from xx_o where idte_date = '" & ISTG_DATE.TOSTRING ("DD / MM / YYYYYYYYYYY) &"' ", 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.OLDBDATADAPTER = New OLEDB.OLDBDATAADAPTER ("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 & "Year" 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 & "Year")

END IF

If not me.yearnote is nothing then

For each me.monthnote in me.yearnote.nodesif me.monthnote.text.toupper = me.idtb_temp.rows (lint_row) .Item ("idte_date"). Month.Tostring.Toupper & "Month" 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 & "Month")

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 & "Japan" 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 & "Japan")

END IF

NEXT

Me.treeView1.expandall ()

END IF

END FUNCTION

Private sub button1_click (byvale as system.object, byval e as system.eventargs) Handles Button1.click

Fill_tree ()

End Sub

END CLASS

Rendering:

Rendering:

转载请注明原文地址:https://www.9cbs.com/read-48429.html

New Post(0)