防止树视图中的节点文本重复(VB.NET)

时间:2013-01-17 04:22:42

标签: vb.net treeview duplicates

进程:使用node text = textbox1.text

在树视图控件中添加节点

我想防止添加重复节点,即,如果添加了带有文本“ABC”的节点,那么下次时,不应将具有文本“ABC”的节点添加到树视图控件中。

我尝试了以下方法但无法达到预期效果。 方法A)

Dim list As New ArrayList
list.Add(TextBox1.Text) 
if list.Contains(Textbox1.Text) then
       MsgBox("Use different name")
else 
       .....code to add node with text
end if

方法B)

if Treeview1.Nodes.Count > 0 then 
   For i = 0 to Treeview1.Nodes.Count
      if Treeview1.Nodes(i).Text=Textbox1.Text then
         MsgBox("Use different name")
      end if
   next
else 
   ........code to add node with text
end if 

我无法理解在此论坛上为C#建议的解决方案。

任何帮助都将非常感激。

由于

2 个答案:

答案 0 :(得分:0)

方法A应该可以正常工作。您的代码中可能有另一个错误(在else部分?)。如果list位于重复调用的函数中,则应将其声明为静态,否则每次都会重置为新的(已清除)。

方法B有几个错误:(1)for语句应该是For i = 0 to Treeview1.Nodes.Count - 1(可能使用“for each”),而else带有添加节点的代码应该在msgbox声明。此外,方法B仅搜索树视图的根节点。您需要遍历树以检查所有节点。

答案 1 :(得分:0)

If ListView1.Items.Count > 0 Then
                For I = 0 To ListView1.Items.Count - 1
                    For Each LVL As ListViewItem In ListView1.Items
                        If ListView1.Items.Item(I).Index = LVL.Index Then
                            Continue For
                        Else
                            If ListView1.Items.Item(I).Text = LVL.Text Then 
                                LVL.Remove()
                            End If
                        End If
                    Next
                Next
           End If