tagCANDY CGI VBレスキュー(花ちゃん)の Visual Basic 6.0用 掲示板
VBレスキュー(花ちゃん)の Visual Basic 6.0用 掲示板
[ツリー表示へ]  [ワード検索]  [Home]

タイトル Re^2: TreeViewのノードの保存
投稿日: 2010/09/02(Thu) 13:14
投稿者ima
魔界の仮面弁士さん、いつも素早い回答で感謝します。
私の中に組み込んだ場合、ご呈示いただいたコードでは、
なぜか最初では良さそうでしたが、再帰が上手く行かなかったのか
不必要に繰り返しが有りました。

> Function GetText(ByVal n As Node) As String
>     If n Is Nothing Then
>         GetText = "(nothing)"
>     Else
>         GetText = n.Key
>     End If
> End Function
>
> Sub DumpNextNode(ByVal target As Node, ByVal level As Integer)
>     If target Is Nothing Then
>         Exit Sub
>     Else
>         Debug.Print Tab(level); target.Key
>     End If
>     DumpNextNode target.Child, level + 1
>    
>     Dim n As Node
>     Set n = target.Next
>     Do Until n Is Nothing
>         DumpNextNode n, level
>         Set n = n.Next
>     Loop
> End Sub
>
> Private Sub Command1_Click()
>     If TreeView1.Nodes.Count = 0 Then
>         Exit Sub
>     Else
>         Debug.Print "----"
>         DumpNextNode TreeView1.Nodes(1).Root, 1
>     End If
> End Sub

私なりにヘルプを参考に作ったコードです。

'TreeViewの見たまま書き出し
Private Sub Command2_Click()
Dim objNode As Node, strText As String, lNodeDepth As Long
    Set objNode = TreeView1.Nodes.Item(1).FirstSibling.Child
    Call DumpNextNode(objNode, strText, lNodeDepth)
    Debug.Print strText    ' 結果を表示
End Sub

Private Sub DumpNextNode(ByVal BaseNode As Node, strText As String, lNodeDepth As Long)
Dim n As Long
Dim objNode As Node
    If Not BaseNode Is Nothing Then
        '変数 n に先頭項目のインデックスを代入
        n = BaseNode.FirstSibling.Index
        '文字列変数に先頭項目のテキストおよび改行を代入
        strText = strText & String(lNodeDepth, vbTab) & BaseNode.FirstSibling.Text & vbLf
        While n <> BaseNode.LastSibling.Index
            If TreeView1.Nodes(n).children > 0 Then
                Set objNode = TreeView1.Nodes(n).Child.FirstSibling
                Call DumpNextNode(objNode, strText, lNodeDepth + 1)
            End If
            'n が末尾項目のインデックスになるまで、次の項目に進み、
            'そのテキストを文字列変数に入れます。
            strText = strText & String(lNodeDepth, vbTab) & TreeView1.Nodes(n).Next.Text & vbLf
            '変数 n に次のノードのインデックスを代入します。
            n = TreeView1.Nodes(n).Next.Index
        Wend
    End If
End Sub

とりあえず、希望の形式で出力できましたがおかしな所も有るかも知れません。

今回も色々勉強させていただきました。
有り難うございました。

- 関連一覧ツリー をクリックするとツリー全体を一括表示します)

古いスレッドにレスはつけられません。