Articles in this section
Category / Section

How to select TreeViewItemAdv using key input?

2 mins read

As like in Microsoft Office application, end users would like an option in TreeViewAdv to select TreeViewItemAdv, just by typing its content in Keyboard. But there is no default option for this requirement in TreeViewAdv and it can be achieved by handling its PreviewKeyDown event.


The following code snippets demonstrates the same.


Code Example: [C#]


        /// <summary>
        /// Occurs when Key is pressed
        /// </summary>
        /// <param name="sender">TreeViewAdv</param>
        /// <param name="e">KeyEventArgs</param>
        void treeview_PreviewKeyDown(object sender, KeyEventArgs e)
            List<TreeViewItemAdv> samekey = new List<TreeViewItemAdv>();
            TreeViewItemAdv selecteditemlevel1 = treeview.Items[0] as TreeViewItemAdv;
            if (PreviousKey != e.Key.ToString())
                PreviousselectedItem = null;
            for (int i = 0; i < selecteditemlevel1.Items.Count; i++)
                if (selecteditemlevel1.Items[i].ToString().StartsWith(e.Key.ToString()))
                    samekey.Add(selecteditemlevel1.Items[i] as TreeViewItemAdv);
            for (int i = 0; i < samekey.Count; i++)
                if (PreviousselectedItem == null || PreviousselectedItem == (samekey[i] as TreeViewItemAdv))
                    loopcompleted = true;
                if (!(samekey[i] as TreeViewItemAdv).IsSelected && loopcompleted)
                    (samekey[i] as TreeViewItemAdv).IsSelected = true;
                    PreviousselectedItem = (samekey[i] as TreeViewItemAdv);
                    loopcompleted = false;
                    PreviousKey = e.Key.ToString();
                    if (samekey.Count - 1 == i)
                        PreviousselectedItem = null;



Code Example: [VB]


              ''' <summary>
              ''' Occurs when Key is pressed
              ''' </summary>
              ''' <param name="sender">TreeViewAdv</param>
              ''' <param name="e">KeyEventArgs</param>
              Private Sub treeview_PreviewKeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
                     Dim samekey As New List(Of TreeViewItemAdv)()
                     Dim selecteditemlevel1 As TreeViewItemAdv = TryCast(treeview.Items(0), TreeViewItemAdv)
                     If PreviousKey <> e.Key.ToString() Then
                           PreviousselectedItem = Nothing
                     End If
                     For i As Integer = 0 To selecteditemlevel1.Items.Count - 1
                           If selecteditemlevel1.Items(i).ToString().StartsWith(e.Key.ToString()) Then
                                  samekey.Add(TryCast(selecteditemlevel1.Items(i), TreeViewItemAdv))
                           End If
                     Next i
                     For i As Integer = 0 To samekey.Count - 1
                           If PreviousselectedItem Is Nothing OrElse PreviousselectedItem Is (TryCast(samekey(i), TreeViewItemAdv)) Then
                                  loopcompleted = True
                           End If
                           If Not (TryCast(samekey(i), TreeViewItemAdv)).IsSelected AndAlso loopcompleted Then
                                  TryCast(samekey(i), TreeViewItemAdv).IsSelected = True
                                  PreviousselectedItem = (TryCast(samekey(i), TreeViewItemAdv))
                                  loopcompleted = False
                                  PreviousKey = e.Key.ToString()
                                  If samekey.Count - 1 = i Then
                                         PreviousselectedItem = Nothing
                                  End If
                           End If
                     Next i
              End Sub


Did you find this information helpful?
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment