如何用vb处理树形结构

如何用vb处理树形结构,第1张

树状结构使用treeview实现,控件添加方法:右击“工具箱”-“部件”-microsoft windows common controls 6.0-确定,应用

下面是简单的添加删除方法,

Option Explicit

‘添加项目

Private Sub Command1_Click()

Dim nodx As Node

Set nodx = TreeView1.Nodes.Add(, , "zf", "紫风无痕关系(双击收起)")

Set nodx = TreeView1.Nodes.Add("zf", tvwChild, "zfwife", "老婆")

Set nodx = TreeView1.Nodes.Add("zf", tvwChild, "zflover", "情人")

Set nodx = TreeView1.Nodes.Add("zf", tvwChild, "zffriend", "朋友")

Set nodx = TreeView1.Nodes.Add("zfwife", tvwChild, "lp1", "大老婆")

Set nodx = TreeView1.Nodes.Add("zfwife", tvwChild, "lp2", "二老婆")

Set nodx = TreeView1.Nodes.Add("zfwife", tvwChild, "lp3", "三老婆")

Set nodx = TreeView1.Nodes.Add("zfwife", tvwChild, "lp4", "四老婆")

Set nodx = TreeView1.Nodes.Add("zfwife", tvwChild, "lp5", "五老婆")

Set nodx = TreeView1.Nodes.Add("zflover", tvwChild, "qr1", "1号情人")

Set nodx = TreeView1.Nodes.Add("zflover", tvwChild, "qr2", "2号情人")

Set nodx = TreeView1.Nodes.Add("zflover", tvwChild, "qr3", "3号情人")

Set nodx = TreeView1.Nodes.Add("zffriend", tvwChild, "py1", "张三")

Set nodx = TreeView1.Nodes.Add("zffriend", tvwChild, "py2", "李四")

Set nodx = TreeView1.Nodes.Add("zffriend", tvwChild, "py3", "王麻子")

Set nodx = TreeView1.Nodes.Add("py1", tvwChild, "pylp1", "张三的老婆")

Set nodx = TreeView1.Nodes.Add("py1", tvwChild, "pylp2", "张三的孩子")

nodx.EnsureVisible

End Sub

'删除代码

Private Sub Command2_Click()

MsgBox ("将要删除" &TreeView1.SelectedItem.Text)

TreeView1.Nodes.Remove (TreeView1.SelectedItem.Index)

End Sub

VB6.0中定义结构体

窗体中可以定义,只能定义成 Private ,在本窗体中使用

Private Type MyType

a As Integer

b As Integer

End Type

模块中

可以定义成 Private ,在本模块中使用

Private Type MyType

a As Integer

b As Integer

End Type

可以定义成 Public ,在任何地方都可以使用

Public Type MyType

a As Integer

b As Integer

End Type

结构体定义以后就成为一种数据类型,和 Long等数据类型使用方法类似

Private Sub Form_Load()

Dim x As MyType

Dim y As MyType

x.a = 1

x.b = 2

y = x 'VB 允许 结构体 像这样整体赋值

ReDim MyArray(1 To 10) As MyType

End Sub

Option

Explicit

'定义

结构体

,注意,此处如果用到字符串变量,要设置

定长

Private

Type

typUser

uid

As

Long

uName

As

String

*

10

'定长10个字符

uPWD

As

String

*

10

End

Type

Dim

user(10)

As

typUser

'读取

Dim

userGet(10)

As

typUser

Private

Sub

Form_Load()

'随便填充数据

Dim

i

As

Integer

For

i

=

0

To

10

With

user(i)

.uid

=

i

.uName

=

Rnd

*

99999

.uPWD

=

Rnd

*

88888

End

With

Next

'保存结构体数组到文件

Open

App.Path

&

"\save.dat"

For

Random

As

#1

Len

=

Len(user(0))

'Random方式打开文件,这里的len是每条记录的长度

For

i

=

0

To

10

Put

#1,

,

user(i)

Next

Close

#1

'从文件中读取结构体

Open

App.Path

&

"\save.dat"

For

Random

As

#1

Len

=

Len(user(0))

For

i

=

0

To

10

Get

#1,

i

+

1,

userGet(i)

Next

Close

#1

End

Sub


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/7851895.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-10
下一篇 2023-04-10

发表评论

登录后才能评论

评论列表(0条)

保存