如何匹配treeview中文本文件的最后一行并在vb.net中显示文本框?
我对分类树视图有问题。我选择文本文件的最后一行,然后从该文本文件中提取树视图中添加的最后一个子节点。关键是我做不到!我尝试了此文件中的行数,但没有结果。事实上,我做了一些事情(当然不是这样),以使选定的节点在树状视图中与文本框中的显示一致。下面是一个截图和我的代码!我不知道我是否理解正确,我的英语是翻译成英语的。非常感谢。克劳德如何匹配treeview中文本文件的最后一行并在vb.net中显示文本框?,vb.net,sorting,treeview,Vb.net,Sorting,Treeview,我对分类树视图有问题。我选择文本文件的最后一行,然后从该文本文件中提取树视图中添加的最后一个子节点。关键是我做不到!我尝试了此文件中的行数,但没有结果。事实上,我做了一些事情(当然不是这样),以使选定的节点在树状视图中与文本框中的显示一致。下面是一个截图和我的代码!我不知道我是否理解正确,我的英语是翻译成英语的。非常感谢。克劳德 Dim NbLine As Integer = 0 Dim SR As System.IO.StreamReader = New System.IO.S
Dim NbLine As Integer = 0
Dim SR As System.IO.StreamReader = New System.IO.StreamReader(OuvrirFichier)
While Not SR.EndOfStream
SR.ReadLine()
NbLine += 1
End While
SR.Close()
Dim lastLine As String = File.ReadLines(OuvrirFichier, Encoding.UTF8) _
.Where(Function(f As String) (Not String.IsNullOrEmpty(f))).Last.ToString
Dim mytext As String = lastLine.Substring(17, 90)
If NbLine > 0 Then
Dim lignesDuFichier As String() = File.ReadAllLines(OuvrirFichier, Encoding.UTF8)
Dim derniereLigne As String = lignesDuFichier(lignesDuFichier.Length - 1)
TreeView1.Focus()
TreeView1.SelectedNode = TreeView1.Nodes(0).Nodes(lignesDuFichier.Length - 1)
End If
行中的注释
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim OuvrirFichier = "C:\Users\maryo\Desktop\Code\Test Empty Line.txt" '"path to file"
'At least you will only be reading the file once
Dim AllLines = File.ReadAllLines(OuvrirFichier)
Dim LinesWithContent = AllLines.Where(Function(s) s.Trim() <> String.Empty)
Dim lastLine = LinesWithContent.Last
Dim mytext As String = lastLine.Substring(17, 90)
Debug.Print(mytext) 'Just checking that you get what was expected
Dim NbLine = AllLines.Length
Dim derniereLigne As String = AllLines(NbLine - 1) 'Another variable to hold last line???
'But this time it could be a blank line.
TreeView1.Focus()
'This makes no sense. An index of a subNode base on the number of lines in the text file
'is supposed to be the SelectedNode
'Why would this be the last node added?
TreeView1.SelectedNode = TreeView1.Nodes(0).Nodes(NbLine - 1)
'You never test the equality of the SelectedNode with mytext
End Sub
Private子按钮1\u单击(发送者作为对象,e作为事件参数)处理按钮1。单击
Dim OuvrirFichier=“C:\Users\maryo\Desktop\Code\Test Empty Line.txt”“文件路径”
'至少您将只读取该文件一次
Dim AllLines=File.ReadAllLines(OuvrirFichier)
Dim LinesWithContent=AllLines.Where(函数s.Trim()String.Empty)
Dim lastLine=LinesWithContent.Last
将mytext设置为字符串=lastLine.Substring(17,90)
“只是检查您是否得到了预期的结果
尺寸NbLine=所有线。长度
Dim derniereLigne As String=AllLines(NbLine-1)'保存最后一行的另一个变量???
但这次可能是一个空行。
TreeView1.Focus()
这是没有道理的。基于文本文件中行数的子节点索引
'应该是SelectedNode
'为什么这是最后添加的节点?
TreeView1.SelectedNode=TreeView1.Nodes(0).Nodes(NbLine-1)
'您从未测试SelectedNode与mytext的相等性
端接头
我觉得你的方法有点不合适。可能会发现,只需将文件读入集合一次,就可以更轻松地呈现数据,并在内存中更好地保存所有内容@Mary:它确实是添加的最后一个节点,因为它是程序中显示的节点。这无疑是因为在这些条件下,排序非常麻烦。非常感谢你,玛丽。我只是测试了一下,但结果没有改变。焦点总是放在树视图中的最后一个排序项上!