VB.net 中的 Treeview 键盘导航问题

问题描述 投票:0回答:1

我正在用音乐列表填充树视图。根节点的文本是“Library”。我想通过输入名字来找到特定的艺术家。 例如,如果我想转到“Billy Idol”,当我输入“Billy”时,它会转到列表中的第一个 Billy,但是当我按空格键和“I”时,树视图将跳转到第一个“I”在树上。我怎样才能改变这种行为?

我希望树视图选择 Billy Idol,而不是跳到第一个“I”。

文本文件使用选项卡来显示树视图项目的级别。

由于缺乏 Unicode 支持,因此从 VB6 迁移代码。 在VB6中,捕获KeyDown事件中的空格字符并将KeyCode更改为0。无论如何我找不到在VB.net中执行此操作。

这是代码:

Imports System.IO

Public Class Form1
    Inherits System.Windows.Forms.Form


    Dim mNode As TreeNode

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim file_name As String = Application.StartupPath
        file_name = file_name.Substring(0, file_name.Length - 1)
        file_name = file_name.Substring(0, file_name.LastIndexOf("\"))
        file_name &= "\test.txt"
        LoadTreeViewFromFile(file_name, TreeView1)
        TreeView1.Sort()

    End Sub
    Private Sub LoadTreeViewFromFile(ByVal file_name As String, ByVal trv As TreeView)
        Dim stream_reader As New StreamReader(file_name)
        Dim file_contents As String = stream_reader.ReadToEnd()
        stream_reader.Close()

        file_contents = file_contents.Replace(vbLf, "")

        Const charCR As Char = CChar(vbCr)
        Const charTab As Char = CChar(vbTab)
        Dim lines() As String = file_contents.Split(charCR)

        Dim text_line As String
        Dim level As Integer
        Dim tree_nodes() As TreeNode
        Dim num_nodes As Integer = 0
        ReDim tree_nodes(num_nodes)

        trv.Nodes.Clear()
        For i As Integer = 0 To lines.GetUpperBound(0)
            text_line = lines(i)
            If text_line.Trim().Length > 0 Then
                level = text_line.Length - text_line.TrimStart(charTab).Length
                If level > num_nodes Then
                    num_nodes = level
                    ReDim Preserve tree_nodes(num_nodes)
                End If

                If level = 0 Then
                    tree_nodes(level) = trv.Nodes.Add(text_line.Trim())
                Else
                    tree_nodes(level) = tree_nodes(level - 1).Nodes.Add(text_line.Trim())
                    If level = 1 Then tree_nodes(level).EnsureVisible()
                End If
            End If
        Next i

        If trv.Nodes.Count > 0 Then trv.Nodes(0).EnsureVisible()
    End Sub
    Private Sub TreeView1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TreeView1.KeyPress
        If TreeView1.SelectedNode.Parent Is TreeView1.Nodes(0) Then TreeView1.SelectedNode.Expand()
    End Sub
End Class
treeview
1个回答
0
投票

迁移到 twinBASIC 编程语言会更好。它兼容 VB6,因此可以轻松迁移。

© www.soinside.com 2019 - 2024. All rights reserved.