以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  TreeView控件疑惑?  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=72180)

--  作者:blackwolf
--  发布时间:2015/7/24 23:00:00
--  TreeView控件疑惑?
    动态将MenuStrip控件中的信息添加到TreeView控件中
 下载信息  [文件大小:   下载次数: ]
点击浏览该文件:管理项目3.table

项目文件中,AfterLoad窗口时间有注释,实在是搞不下去了。请指教,想实现这样的效果
此主题相关图片如下:qq图片20150724225557.png
按此在新窗口浏览图片
[此贴子已经被作者于2015/7/24 23:00:11编辑过]

--  作者:大红袍
--  发布时间:2015/7/26 10:43:00
--  
For i As Integer = 0 To MenuS.Items.Count - 1
    Dim newmenu As object = MenuS.Items(i)
    Dim nd As WinForm.TreeNode = treeV.Nodes.Add(newmenu.Text)
    For j As Integer = 0 To newmenu.DropDownItems.Count - 1
        Dim cnewmenu As object = newmenu.DropDownItems(j)
        Dim cnd As WinForm.TreeNode = nd.Nodes.Add(cnewmenu.Text)
    Next
Next

--  作者:blackwolf
--  发布时间:2015/7/26 14:11:00
--  
非常感谢大红袍的回答!代码已经试了。想要实现的目的基本已实现,但还有瑕疵。如下图:

此主题相关图片如下:分割条.png
按此在新窗口浏览图片
分割条如何实现?我的菜单中的分割条代码是:
Dim OSSplitBar As New System.Windows.Forms.ToolStripSeparator                                                                                 \'定义分割条 Split bar
(我找到一个C#的实例然后转换为VB的代码,并引用修改后应用到foxtable中去,出现如下问题)
我把c#的代码转换为VB.net代码如下(截取的一小段代码):
以下内容为程序代码:

1 \'\'\' 将MenuStrip控件中的首行命令项添加到TreeView控件中
2         \'\'\' </summary>
3         \'\'\' <param treeV="TreeView">TreeView控件</param>
4         \'\'\' <param MenuS="MenuStrip">MenuStrip控件</param>
5         Public Sub GetMenu(treeV As TreeView, MenuS As MenuStrip)
6             Dim Var_Bool As Boolean = True
7             For i As Integer = 0 To MenuS.Items.Count - 1 \'遍历MenuStrip组件中的一级菜单项    
8
9                 Dim newNode1 As TreeNode = treeV.Nodes.Add(MenuS.Items(i).Text) \'将一级菜单项的名称添加到TreeView组件的根节点中,并设置当前节点的子节点newNode1
10
11                 If MenuS.Items(i).Enabled = False Then \'判断当前项是否为可用
12                     
13                     newNode1.ForeColor = Color.Silver \'改变树节点的字体颜色为不可用色
14                     
15                     Var_Bool = False
16                 Else
17                     newNode1.ForeColor = Color.Black \'改变树节点的字体颜色为可用色
18                     
19                     Var_Bool = True
20                 End If
21                 newNode1.Tag = 0
22                 \'标识,有子项的命令项
23                 \'将当前菜单项的所有相关信息存入到ToolStripDropDownItem对象中
24                 Dim newmenu As ToolStripDropDownItem = DirectCast(MenuS.Items(i), ToolStripDropDownItem)
25                     \'添加多层命令项
26                 GetCavernMenu(newNode1, newmenu, Var_Bool)
27             Next
28         End Sub
29
30         \'\'\' <summary>
31         \'\'\' 将MenuStrip控件中的多层命今项添加到TreeView控件中
32         \'\'\' </summary>
33         \'\'\' <param newNodeA="TreeNode">TreeNode对象</param>
34         \'\'\' <param newmenuA="ToolStripDropDownItem">ToolStripDropDownItem对象</param>
35         \'\'\' <param BL="bool">标识(是否可用)</param>
36         Public Sub GetCavernMenu(newNodeA As TreeNode, newmenuA As ToolStripDropDownItem, BL As Boolean)
37             Dim Var_Bool As Boolean = True
38             If newmenuA.HasDropDownItems AndAlso newmenuA.DropDownItems.Count > 0 Then
39                 For j As Integer = 0 To newmenuA.DropDownItems.Count - 1
40                     \'遍历二级菜单项
41                     \'将二级菜单名称添加到TreeView组件的子节点newNode1中,并设置当前节点的子节点newNode2
42                     Dim newNodeB As TreeNode = newNodeA.Nodes.Add(newmenuA.DropDownItems(j).Text)
43                     Var_Bool = True
44                     If BL = False Then
45                         \'判断当前命令项的上一级命令是否可用
46                         newNodeB.ForeColor = Color.Silver
47                         \'设置当前命令项的字体颜色为不可用色
48                         newNodeB.Tag = 0
49                         \'标识,不显示相应的窗体
50                         Var_Bool = False
51                     Else
52                         If newmenuA.DropDownItems(j).Enabled = False Then
53                             \'判断当前命令项是否为可用
54                             newNodeB.ForeColor = Color.Silver
55                             \'设置当前命令项的字体颜色为不可用色
56                             newNodeB.Tag = 0
57                             \'标识,不显示相应的窗体
58                             Var_Bool = False
59                         Else
60                             newNodeA.ForeColor = Color.Black
61                             \'设置当前命令项的字体颜色为可用色
62                                 \'标识,显示相应的窗体
63                             newNodeB.Tag = Integer.Parse(newmenuA.DropDownItems(j).Tag.ToString())
64                         End If
65                     End If
66                     \'将当前菜单项的所有相关信息存入到ToolStripDropDownItem对象中
67                     Dim newmenuB As ToolStripDropDownItem = DirectCast(newmenuA.DropDownItems(j), ToolStripDropDownItem)
68                     If newmenuB.HasDropDownItems AndAlso newmenuA.DropDownItems.Count > 0 Then
69                         \'如果当前命令项有子项
70                         newNodeB.Tag = 0
71                         \'标识,有子项的命令项
72                             \'调用递归方法
73                         GetCavernMenu(newNodeB, newmenuB, Var_Bool)
74                     End If
75                 Next
76             End If
77         End Sub


其中:
Dim newmenu As ToolStripDropDownItem = DirectCast(MenuS.Items(i), ToolStripDropDownItem) \'将当前菜单项的所有相关信息存入到ToolStripDropDownItem对象中
修改为  dimnewmenu as system.windows.forms.ToolStripDropDownItem = DirectCast(MenuS.Items(i), ToolStripDropDownItem) 出现错误 搞不清,具体为什么类?
后面我又在MSDN中查了下关于  ToolStripDropDownItem的用法:
以下内容为程序代码:

1 \' This method shows the drop-down for the first item
2 \' in the form\'s ToolStrip.
3 Private Sub showButton_Click( _
4 ByVal sender As Object, _
5 ByVal e As EventArgs) _
6 Handles showButton.Click
7
8 Dim item As ToolStripDropDownItem = CType(Me.toolStrip1.Items(0), ToolStripDropDownItem)
9
10 If item.HasDropDownItems Then
11 item.ShowDropDown()
12 End If
13
14 End Sub


然后是分割条能否用转换为vb.net的代码修改,具体如何实现?
代码为:
以下内容为程序代码:

1 If MenuS.Items(i).Enabled = False Then \'判断当前项是否为可用
2                     
3                     newNode1.ForeColor = Color.Silver \'改变树节点的字体颜色为不可用色
4                     
5                     Var_Bool = False
6                 Else
7                     newNode1.ForeColor = Color.Black \'改变树节点的字体颜色为可用色
8                     
9                     Var_Bool = True
10                 End If
请告知
例子上传
 下载信息  [文件大小:   下载次数: ]
点击浏览该文件:管理项目3.table


[此贴子已经被作者于2015/7/26 14:16:19编辑过]

--  作者:大红袍
--  发布时间:2015/7/26 14:31:00
--  
For i As Integer = 0 To MenuS.Items.Count - 1
    Dim newmenu As object = MenuS.Items(i)
    Dim nd As WinForm.TreeNode = treeV.Nodes.Add(newmenu.Text)
    For j As Integer = 0 To newmenu.DropDownItems.Count - 1
        Dim cnewmenu As object = newmenu.DropDownItems(j)
        If Typeof cnewmenu Is System.Windows.Forms.ToolStripSeparator Then
            Dim cnd As WinForm.TreeNode = nd.Nodes.Add("--------------")

        Else
            Dim cnd As WinForm.TreeNode = nd.Nodes.Add(cnewmenu.Text)
        End If
    Next
Next

--  作者:blackwolf
--  发布时间:2015/7/26 14:32:00
--  
Dim newmenu As ToolStripDropDownItem = DirectCast(MenuS.Items(i), ToolStripDropDownItem) \'将当前菜单项的所有相关信息存入到ToolStripDropDownItem对象中
修改为  dimnewmenu as system.windows.forms.ToolStripDropDownItem = DirectCast(MenuS.Items(i), ToolStripDropDownItem) 出现错误 搞不清,具体为什么类?
能否解答?

--  作者:大红袍
--  发布时间:2015/7/26 14:33:00
--  
 没有必要转换,Vb.Net没必要指定类型。
--  作者:blackwolf
--  发布时间:2015/7/26 15:03:00
--  
好的谢谢!为了这个问题,我搞了1天左右的时间都没有解决,头都是大的!