ListView1_AfterLabelEdit事件中调用TreeView函数出现莫名错误?高手请进!
窗体中有一个Treeview和一个ListView,重命名listview选中项名称时出现如题错误,求解
以下是相应代码:
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 SubPublic Sub TreeView1_Refresh() TreeView1.Nodes(0).Collapse() TreeView1.Nodes(0).Expand() End Sub
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
[解决办法]
Public Sub TreeView1_Refresh() If TreeView1.Nodes.Count >0 Then TreeView1.Nodes(0).Collapse() TreeView1.Nodes(0).Expand() End IfEnd Sub