2013-01-17 151 views
0

过程:一个节点与节点的文本树视图控制加入= textbox1.text防止重复(VB.NET)

欲防止另外的重复节点即比方说,如果一个节点在下一次添加文本“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#在这个论坛。

任何帮助将非常感激。

谢谢

回答

0

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

方法B有几个误区:(1)for语句应该是For i = 0 to Treeview1.Nodes.Count - 1(也许还可以利用“每个”),以及与else的代码添加节点应该是msgbox后声明。另外,方法B只搜索树视图的根节点。您需要遍历树来检查所有节点。

+0

方法B +您的建议修改=解决方案。我再次尝试了方法A,但它添加了重复节点。我不确定那里有什么问题。非常感谢你的时间和帮助。 – mrn

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