基于VB.NET(XML)的三层联动菜单,有人会做吗?

jonical 2021-02-02 02:10:49

<?xml version="1.0" encoding="utf-8"?>
<Config>
<Root Level="0" Index="0" Code="A" Name="方案1">
<Solution Level="1" Index="0" Code="A1" Name="默认配置1">
<Bind Level="2" Index="0" Code="A1A" Name="声音1"/>
<Bind Level="2" Index="1" Code="A1B" Name="回音1"/>
</Solution>
<Solution Level="1" Index="1" Code="A2" Name="默认配置2">
<Bind Level="2" Index="0" Code="A2A" Name="声音2"/>
<Bind Level="2" Index="1" Code="A2B" Name="回音2"/>
</Solution>
</Root>
<Root Level="0" Index="1" Code="B" Name="方案2">
<Solution Level="1" Index="0" Code="B1" Name="默认配置1">
<Bind Level="2" Index="0" Code="B1A" Name="声音1"/>
<Bind Level="2" Index="1" Code="B1B" Name="回音1"/>
</Solution>
<Solution Level="1" Index="1" Code="B2" Name="默认配置2">
<Bind Level="2" Index="0" Code="B2A" Name="声音2"/>
<Bind Level="2" Index="1" Code="B2B" Name="回音2"/>
</Solution>
</Root>
</Config>

以上是Xml文件的结构体,在程序根目录存放。
之前发了一帖子,没正确答案。
想实现菜单的三级联动效果。如何实现?
...全文
254 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
你的XML文件怎么存发都不是问题,
读取XML的就不在这里写了,我提供一下后续的数据处理,类名XMLMsg内容如下:
Public Class XMLMsg
Public Property Level() As String
Public Property Index() As String
Public Property Code() As String
Public Property Name() As String

End Class

第一步,全局变量三个:
Dim MsgLsit As New List(Of XMLMsg)
Dim Com1 As New List(Of XMLMsg)
Dim Com2 As New List(Of XMLMsg)
Dim Com3 As New List(Of XMLMsg)
第二步,在窗口加载的时候你是读XML,我就不读取了,在Load事件如下代码如下:
Dim tCond As New XMLMsg
tCond.Level = "0"
tCond.Index = "0"
tCond.Code = "A"
tCond.Name = "方案1"
MsgLsit.Add(tCond)
tCond = New XMLMsg
tCond.Level = "1"
tCond.Index = "0"
tCond.Code = "A1"
tCond.Name = "1-默认配置1"
MsgLsit.Add(tCond)
tCond = New XMLMsg
tCond.Level = "2"
tCond.Index = "0"
tCond.Code = "A1A"
tCond.Name = "1-声音1"
MsgLsit.Add(tCond)
tCond = New XMLMsg
tCond.Level = "2"
tCond.Index = "1"
tCond.Code = "A1B"
tCond.Name = "1-回音1"
MsgLsit.Add(tCond)

tCond = New XMLMsg
tCond.Level = "1"
tCond.Index = "1"
tCond.Code = "A2"
tCond.Name = "1-默认配置2"
MsgLsit.Add(tCond)
tCond = New XMLMsg
tCond.Level = "2"
tCond.Index = "0"
tCond.Code = "A2A"
tCond.Name = "1-声音2"
MsgLsit.Add(tCond)
tCond = New XMLMsg
tCond.Level = "2"
tCond.Index = "1"
tCond.Code = "A2B"
tCond.Name = "1-回音2"
MsgLsit.Add(tCond)

tCond = New XMLMsg
tCond.Level = "0"
tCond.Index = "1"
tCond.Code = "B"
tCond.Name = "方案2"
MsgLsit.Add(tCond)
tCond = New XMLMsg
tCond.Level = "1"
tCond.Index = "0"
tCond.Code = "B1"
tCond.Name = "2-默认配置1"
MsgLsit.Add(tCond)
tCond = New XMLMsg
tCond.Level = "2"
tCond.Index = "0"
tCond.Code = "B1A"
tCond.Name = "2-声音1"
MsgLsit.Add(tCond)
tCond = New XMLMsg
tCond.Level = "2"
tCond.Index = "1"
tCond.Code = "B1B"
tCond.Name = "2-回音1"
MsgLsit.Add(tCond)

tCond = New XMLMsg
tCond.Level = "1"
tCond.Index = "1"
tCond.Code = "B2"
tCond.Name = "2-默认配置2"
MsgLsit.Add(tCond)
tCond = New XMLMsg
tCond.Level = "2"
tCond.Index = "0"
tCond.Code = "B2A"
tCond.Name = "2-声音2"
MsgLsit.Add(tCond)
tCond = New XMLMsg
tCond.Level = "2"
tCond.Index = "1"
tCond.Code = "B2B"
tCond.Name = "2-回音2"
MsgLsit.Add(tCond)

ComboBox1.Items.Clear()
Com1.Clear()
Dim Comd1 = From x In MsgLsit Where x.Code = "A" Or x.Code = "B" Select x
For Each t As XMLMsg In Comd1
Com1.Add(t)
ComboBox1.Items.Add(t.Name)
Next
If ComboBox1.Items.Count > 0 Then
ComboBox1.SelectedIndex = 0
End If


然后是二个SelectedIndexChanged事件
代码如下:
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
If ComboBox1.SelectedIndex >= 0 Then
Com2.Clear()
ComboBox2.Items.Clear()
Dim tCond As New XMLMsg
tCond = Com1(ComboBox1.SelectedIndex)
Dim Comd2 = From x In MsgLsit Where x.Level = 1 And x.Code.Substring(0, 1) = tCond.Code Select x
For Each t As XMLMsg In Comd2
Com2.Add(t)
ComboBox2.Items.Add(t.Name)
Next
If ComboBox2.Items.Count > 0 Then
ComboBox2.SelectedIndex = 0
End If
End If
End Sub
Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
If ComboBox2.SelectedIndex >= 0 Then
Com3.Clear()
ComboBox3.Items.Clear()
Dim tCond As New XMLMsg
tCond = Com2(ComboBox2.SelectedIndex)
Dim Comd2 = From x In MsgLsit Where x.Level = 2 Select x
For Each t As XMLMsg In Comd2
If t.Code.Substring(0, 2) = tCond.Code Then
Com3.Add(t)
ComboBox3.Items.Add(t.Name)
End If
Next
If ComboBox3.Items.Count > 0 Then
ComboBox3.SelectedIndex = 0
End If
End If
End Sub

效果如下图,你选择了方案2,只会出现方案2的内容,不会出现方案1的,我这里在代码上面,把都加了“1-”和“2”,这样可以把方案1和方案2的子项区分开。




最后说一句,VB.NET决对支持linq,VB.net 你做不到,是你没有用对,如果让你纠结,建议你早点换语言。

jonical 2021-02-02
  • 打赏
  • 举报
回复
读是能读,但是没办法做菜单的三级联动,比如省、市、区。比如选定某一区之后,再按照一个值读取相应的code值,这是VB.NET没法办到的,不太灵活。
hztltgg 2021-02-02
  • 打赏
  • 举报
回复
VB.NET 怎么可能不支持 Linq语法,事实上vb.net 对xml的查询比c#功能更强
jonical 2021-02-02
  • 打赏
  • 举报
回复
界面上有三个combobox控件,combobox1显示“方案1”、“方案2”、“方案N”。 第二个combobox显示“默认配置1”、“默认配置2”、“默认配置N”。 第三个combobox显示“声音1”、“声音2”、“声音N” 如何实现?VB.NET不支持linq语法。工具VS2019或者VS2010

16,722

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧