プロが教えるわが家の防犯対策術!

VB.NET2003 を使用して、
CheckBox付のTreeViewを作成しました。
階層は3階層用意して、最下層のTextに人名・Tagにメールアドレスをセットしました。

下記のサンプルの場合、チェックを入れたDさんのメールアドレスのみを
取得したいのですが、どのよう書けばよろしいでしょうか?
ご教授いただけますよう、お願い致します。

TreeViewイメージ
□本部
 □総務課
  □Aさん
  □Bさん
 □人事課
  □Cさん
  ■Dさん
  □Eさん
□営業
 □東京
  □Fさん
  □Gさん

TreeView作成コード
Dim parentrow As DataRow
Dim ParentTable As DataTable

For Each parentrow In ds.Tables("A").Rows
Dim parentnode As TreeNode
parentnode = New TreeNode(parentrow.Item("SHOZOKU").ToString)
treeView1.Nodes.Add(parentnode)
''''child'''''
Dim childrow As DataRow
Dim childnode As TreeNode
childnode = New TreeNode
For Each childrow In parentrow.GetChildRows("A")
childnode = parentnode.Nodes.Add(childrow("BUSHO_NAME").ToString)
''''child2''''
Dim childrow2 As DataRow
Dim childnode2 As TreeNode
childnode2 = New TreeNode
For Each childrow2 In childrow.GetChildRows("B")
childnode2 = childnode.Nodes.Add(childrow2("NAME").ToString)
childnode2.Tag = childrow2("MAIL").ToString
Next childrow2
''''''''''''''''''''''''
Next childrow
'''''''''''''''
Next parentrow
treeView1.CheckBoxes = True

A 回答 (1件)

チェックされたのをいつ所得したいのでしょう?



チェックされた時点でいいのであれば TreeViewのAfterChekイベントで

dim nd as TreeNode
nd = e.Node
if nd.Checked then
  MsgBox(nd.Tag)
end if

別のイベント たとえば別ボタンのClick時点なら

dim ndParent, ndChaild1, ndChaild2 as TreeNode
dim sb as new System.Text.StringBuilder
for each ndParent in TreeView1.Nodes
  for each ndChild1 in ndParent.Nodes
    for each ndchild2 in ndChild1.Nodes
      if ndChild2.Chkeced then
        if sb.Length <> 0 then
          sb.Append( vbCrLf )
        end if
        sb.Append( ndChild2.Tag )
      end if
    next ' ndChild2
  next '   ndChild1
next '     ndParent
MsgBox(sb.ToString)

といった具合になるかと

この回答への補足

教えて頂いた通り、
別のボタンクリック時にデータ取得したいと考えております。
上記内容でまずは試したいと思います。
有り難うございます。

補足日時:2011/10/07 13:06
    • good
    • 0
この回答へのお礼

完璧なご回答有り難うございます!
すぐに解決致しました。
非常に参考になりました。

お礼日時:2011/10/07 13:22

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!