首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

ListView1_AfterLabelEdit事件中调用TreeView函数出现莫名异常?

2012-04-26 
ListView1_AfterLabelEdit事件中调用TreeView函数出现莫名错误?高手请进!窗体中有一个Treeview和一个ListV

ListView1_AfterLabelEdit事件中调用TreeView函数出现莫名错误?高手请进!
窗体中有一个Treeview和一个ListView,重命名listview选中项名称时出现如题错误,求解
以下是相应代码:

VB.NET code
Private Sub ListView1_AfterLabelEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.LabelEditEventArgs) Handles ListView1.AfterLabelEdit        'Dim oldName As String = ListView1.SelectedItems(0).Text        '' Determine if label is changed by checking to see if it is equal to Nothing.        'If e.Label = Nothing Then        '    Return        'End If        '' ASCIIEncoding is used to determine if a number character has been entered.        'Dim AE As New ASCIIEncoding()        '' Convert the new label to a character array.        'Dim temp As Char() = e.Label.ToCharArray()        '' Check each character in the new label to determine if it is a number.        'Dim x As Integer        'For x = 0 To temp.Length - 1        '    ' Encode the character from the character array to its ASCII code.        '    Dim bc As Byte() = AE.GetBytes(temp(x).ToString())        '    ' Determine if the ASCII code is within the valid range of numerical values.        '    If bc(0) > 47 And bc(0) < 58 Then        '        ' Cancel the event and return the lable to its original state.        '        e.CancelEdit = True        '        ' Display a MessageBox alerting the user that numbers are not allowed.        '        MessageBox.Show("The text for the item cannot contain numerical values.")        '        ' Break out of the loop and exit.        '        Return        '    End If        'Next x        'Dim newName As String = e.Label + ".txt"        'If IO.File.Exists(IO.Path.Combine(directory, newName)) = False And newName <> oldName Then        '    IO.File.Move(IO.Path.Combine(directory, oldName), IO.Path.Combine(directory, newName))        'End If        'ListView1.LabelEdit = False        TreeView1_Refresh()            End Sub

VB.NET code
Public Sub TreeView1_Refresh()        TreeView1.Nodes(0).Collapse()        TreeView1.Nodes(0).Expand()    End Sub


listview其他事件中调用TreeView1_Refresh()正常。
直接把把折叠和展开树根节点两句,替换掉TreeView1_Refresh()调用,也不行。
为了测试无法调用TreeView1_Refresh(),我把其他无关的注释掉了,结果运行错误依旧,一下是错误提示(奇怪的是错误提示框与任何程序语句没有黄线相连接):

[解决办法]
说明node(0)不存在。
[解决办法]
要判断:
VB.NET code
Public Sub TreeView1_Refresh()  if TreeView1.Nodes isnot nothing AndAlso TreeView1.Nodes.Count >0 then        TreeView1.Nodes(0).Collapse()        TreeView1.Nodes(0).Expand()  end if End Sub
[解决办法]
VB.NET code
Public Sub TreeView1_Refresh()    If TreeView1.Nodes.Count >0 Then        TreeView1.Nodes(0).Collapse()        TreeView1.Nodes(0).Expand()    End IfEnd Sub 

热点排行