Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have Treeview in my WPF Project with C#.
I Use the below code for refresh the TreeView eachtime the OpenFileDialog excute.

OpenFileDialog FD = new OpenFileDialog();
          FD.Multiselect = true;
          if (FD.ShowDialog() == true)
          {

              Treeview.Items.Clear();
              files = FD.SafeFileNames;
              paths = FD.FileNames;

          }

          for (int i = 0; i < files.Length; i++)
          {
              Treeview.Items.Add(files[i]);
          }


But after Code Run the Treeview_selectedItemChange Show the «Indox was out of the bounds array» error. how can i fix this?

What I have tried:

private void Treeview_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
               Temp = paths[Treeview.Items.IndexOf(Treeview.SelectedItem)]);
Posted
Updated 10-Oct-20 6:56am
Comments
Richard MacCutchan 10-Oct-20 7:41am    
Most likely the SelectedItemIndex has not been cleared and there are not the same number if items in the Treeview. So reset the SelectedItemIndex before you refresh the view.

Treeview_selectedItemChange Show the «Indox was out of the bounds array» error.

Reference: IndexOutOfRangeException Class (System) | Microsoft Docs[^]
Quote:
The exception that is thrown when an attempt is made to access an element of an array or collection with an index that is outside its bounds.

Since you have not shared the complete code, I can just assume the following:
1. You have a treeview
2. Bsaed on user folder selected, you populate this tree
3. On node selection of this populated tree, you get an error.
This would mean, you were not able to persist the change or reset the tree between step 2 & 3 - thus loosing change made in step 2. In selected index change code, you would be using an index to find the node and it blows off.

I believe a simple debugging should provide insights to you for next steps:
Tutorial: Debug Visual Basic code - Visual Studio | Microsoft Docs[^]
First look at the debugger - Visual Studio | Microsoft Docs[^]

When you debug, you will be able to get why you are loosing the change. Also, you should make sure that while using index kind of code, you are making sure that it exists.
 
Share this answer
 
Thank you. My Problem Solved with using simple Try Catch code for reset the Index of Array;
But now I have another Problem. I Want move between Item of TreeView With keyboard and Button i Define, But i Do not know how to change the Item with Behind Code.
for Solve This i Create Temp integer for get the index of item selected and move in array,But in this way the item Selected in treeview not Change in Correct way.
I want use this code for change the video in media element and from the list of treeview.
I try This code, But i know this is not the Correct way. please help me.

private int TVI = 0;
    `    private void ForwardBT_Click(object sender, RoutedEventArgs e)
        {  
            try
            {
                mediaPlayer.Source = new Uri(paths[TVI]);
            }
            catch 
            {
                try
                { TVI = 0; mediaPlayer.Source = new Uri(paths[TVI]); }
                catch { }
                
            }
        }

I want use the below Code But I Don't now how to change Item Select with Button or Keyboard.
mediaPlayer.Source = new Uri(paths[TreeView.Items.IndexOf(TreeView.SelectedItem)]);
 
Share this answer
 
v6

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900