Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > VB6 > Дайте исходник по работе с TreeView


Автор: Pl.Posayshen 29.10.2004, 17:48
Кто может дайте исходник по работе с TreeView.
Я только начинающий программист.
Мне нужно создать древовидную структуру.
Например:
Есть группы работ, каждая группа работ так же включает группы. В свою очередь каждая группа включает в себя как группу так и сами виды работ.
Мне кажется TreeView с MSFlexGrid подходит для реализации моей задачи.
Ели можно кратко опишите принцип работы с TreeView.
Заранее благодарен.
Спасибо.

Автор: Vach 3.11.2004, 07:36
Начни с поиска, скажем в Google "VB +TreeView +example"
Так дело быстрее пойдет.

А вот симпатичный примерчик, взят с planetsourcecode но ссылка не живая.

Создай в блокноте файл Form1.frm и скопируй туду следкющий код.
Код
VERSION 5.00
Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "MSCOMCTL.OCX"
Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
Begin VB.Form Form1
  BorderStyle     =   4  'Fixed ToolWindow
  Caption         =   "TreeView Example - Pio"
  ClientHeight    =   2895
  ClientLeft      =   150
  ClientTop       =   675
  ClientWidth     =   3255
  LinkTopic       =   "Form1"
  MaxButton       =   0   'False
  MinButton       =   0   'False
  ScaleHeight     =   2895
  ScaleWidth      =   3255
  StartUpPosition =   3  'Windows Default
  Begin MSComDlg.CommonDialog CommonDialog1
     Left            =   2520
     Top             =   240
     _ExtentX        =   847
     _ExtentY        =   847
     _Version        =   393216
     DialogTitle     =   "Open a File to Load"
     Filter          =   "Text Files|*.txt|All Files|*.*|"
  End
  Begin MSComctlLib.TreeView TV1
     Height          =   2655
     Left            =   120
     TabIndex        =   0
     Top             =   120
     Width           =   3015
     _ExtentX        =   5318
     _ExtentY        =   4683
     _Version        =   393217
     Indentation     =   441
     LineStyle       =   1
     Style           =   7
     Appearance      =   1
  End
  Begin VB.Menu mnuExpand
     Caption         =   "Expand"
  End
  Begin VB.Menu mnuCollapse
     Caption         =   "Collapse"
  End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit


Private Sub Form_Load()
'#################################################
'#### Notes: ########################################
'# The main thing you have to learn is to use the Keys, Keys are very imporant#
'#'                                                                                                         #
'#################################################

TV1.Nodes.Add , , "main", "Contents" 'Create Main Parent
   TV1.Nodes.Add "main", tvwChild, "I1", "Example #1" 'Child Node to the Main Parent or ROOT
   TV1.Nodes.Add "I1", tvwChild, "SI1", "Example #1's Child Node"
       TV1.Nodes.Add "main", tvwChild, "I2", "Example #2"
           TV1.Nodes.Add "main", tvwChild, "I3", "Example #3"
           TV1.Nodes.Add "I3", tvwChild, "IA3", "Example #3's Child Node"
           TV1.Nodes.Add "IA3", tvwChild, "IAA3", "Example #3's 2nd Child Node"
               TV1.Nodes.Add "main", tvwChild, "L1", "Load List Example"
               TV1.Nodes.Add "L1", tvwChild, "LA1", "Click To Load List"

TV1.Nodes.Item(1).Expanded = True 'expands the 1st or ROOT node
TV1.Nodes.Item(8).Expanded = True ' expands the 8th Node


End Sub

Private Sub mnuCollapse_Click()
Dim i As Integer
For i = 0 To TV1.Nodes.Count - 1 ' goes threw each node and Collapses it
TV1.Nodes.Item(i + 1).Expanded = False
Next i

End Sub

Private Sub mnuExpand_Click()
Dim i As Integer
For i = 0 To TV1.Nodes.Count - 1 ' goes threw each node and Expandes it
TV1.Nodes.Item(i + 1).Expanded = True
Next i
End Sub

Private Sub TV1_NodeClick(ByVal Node As MSComctlLib.Node)
Dim File As String, Temp
If Node.Key = "LA1" Then ' make sure that the node that is clicked is our Load node
CommonDialog1.ShowOpen ' opens the open dialog
File = CommonDialog1.FileName ' this is the file you selected
   If Trim(File) = "" Then Exit Sub 'makes sure the file isnt just spaces
       TV1.Nodes.Add "main", tvwChild, "I" & FileLen(File), File 'i used the FileLen as the key for this cause it would be different :) i guess"
           Open File For Input As #1 ' opens the file
               
              Do While Not EOF(1) 'this makes the loop keep going unless its at the End Of File (EOF) then it stops
                   Input #1, Temp 'gets the info
                   If Trim(Temp) = "" Then ' make sure that Temp isn't just spaces
                   Else
                   TV1.Nodes.Add "I" & FileLen(File), tvwChild, "I" & TV1.Nodes.Count + 2, Temp 'adds a child node containging the text in the file
                   End If
                   
               Loop ' loop
           Close #1 'closes the file
End If

End Sub


Создай в блокноте файл TreeView Ex.vbp и скопируй туду следкющий код.
Код
Type=Exe
Form=Form1.frm
Reference=*\G{00020430-0000-0000-C000-000000000046}#2.0#0#..\..\..\..\WINDOWS.000\SYSTEM\stdole2.tlb#OLE Automation
Object={831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0; MSCOMCTL.OCX
Object={F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0; COMDLG32.OCX
IconForm="Form1"
Startup="Form1"
Command32=""
Name="Project1"
HelpContextID="0"
CompatibleMode="0"
MajorVer=1
MinorVer=0
RevisionVer=0
AutoIncrementVer=0
ServerSupportFiles=0
VersionCompanyName="Home"
CompilationType=0
OptimizationType=0
FavorPentiumPro(tm)=0
CodeViewDebugInfo=0
NoAliasing=0
BoundsCheck=0
OverflowCheck=0
FlPointCheck=0
FDIVCheck=0
UnroundedFP=0
StartMode=0
Unattended=0
Retained=0
ThreadPerObject=0
MaxNumberOfThreads=1


Создай в блокноте файл TreeView Ex.vbw и скопируй туду следкющий код.
Код

Form1 = 163, 79, 888, 527, C, 66, 66, 694, 514, C


Удачи.

Автор: Sirik 13.11.2004, 12:34
Создай на форме Command Button и TreeView, вот код:
Код

Private Sub Command1_Click()
   TreeView1.Nodes.Clear
   
   TreeView1.Nodes.Add , , "G1", "Группа 1"
   TreeView1.Nodes.Add , , "G2", "Группа 2"
   TreeView1.Nodes.Add , , "G3", "Группа 3"
   TreeView1.Nodes.Add , , "G4", "Группа 4"

   TreeView1.Nodes.Add "G1", tvwChild, "GG1", "Подгруппа 1"
   TreeView1.Nodes.Add "G1", tvwChild, "GG2", "Подгруппа 2"
   TreeView1.Nodes.Add "G1", tvwChild, "GG3", "Подгруппа 3"
   TreeView1.Nodes.Add "G1", tvwChild, "GG4", "Подгруппа 4"

End Sub


Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)