Quantcast
Viewing all articles
Browse latest Browse all 18858

Why does this wpf custom component fire twice?

This class (below) is a custom component. its supposed to just search through the folders by clicking on the list inside the expander.

ExpanderList is the list in the expander

DirMaker is the actuall expander

using System; using System.Collections.Generic; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.IO; namespace BigHand { /// <summary> /// Interaction logic for DirMakerControl.xaml /// </summary> public partial class DirMakerControl : UserControl { string currentDirectory = "c:/"; DirectoryInfo di; bool directoryEmpty; public DirMakerControl() { this.InitializeComponent(); getFolders(); updateHeader(); } private void getFolders() { ExpanderList.Items.Clear(); try { di = new DirectoryInfo(currentDirectory); foreach (DirectoryInfo d in di.GetDirectories()) { ExpanderList.Items.Add(d.ToString()); } } catch (UnauthorizedAccessException) { //goBack() keeps display the same goBack(); MessageBox.Show(" SORRY!!!! \nYou dont have the correct \npermisions to use these files."); } if (ExpanderList.Items.Count == 0) { directoryEmpty = true; ExpanderList.IsEnabled = false; ExpanderList.Items.Add("No Folders Here"); } else { directoryEmpty = false; ExpanderList.IsEnabled = true; } updateHeader(); } private void ListClicked(object sender, SelectionChangedEventArgs e) { // If selected index is -1 (no selection) do nothing if (ExpanderList.SelectedIndex != -1) { //update list and variables if (!directoryEmpty) { ExpanderList.IsEnabled = true; currentDirectory = currentDirectory + ExpanderList.SelectedItem + "/"; // Reset selected index to -1 (no selection) ExpanderList.SelectedIndex = -1; getFolders(); } } } private void goBack() { currentDirectory = currentDirectory.Substring(0, getCurrentFolderStringIndex()); getFolders(); } private void Button_Click(object sender, RoutedEventArgs e) { goBack(); } //returns an integer representing where the current folder name starts in the current folder string public int getCurrentFolderStringIndex() { char[] currentD = currentDirectory.ToCharArray(); for (int index = currentD.Length - 2; index >= 0; index--) { if (currentD[index] == '/') { return index + 1; } } //if this gets called(should at least start back at the c:/) return 0; } private void updateHeader() { //Change Header //get temp int to save calling the method twice int index = getCurrentFolderStringIndex(); if (index == 0) DirMaker.Header = currentDirectory; else DirMaker.Header = currentDirectory.Substring(index, currentDirectory.Length - index); }

//attemp at fixing my problem, not implemented anywhere at the moment should be removed private void selectionMade(ListBox sender, System.Windows.Input.MouseButtonEventArgs e) { // TODO: Add event handler implementation here. // If selected index is -1 (no selection) do nothing if (ExpanderList.SelectedIndex == -1) return; //update list and variables if (!directoryEmpty) { ExpanderList.IsEnabled = true; currentDirectory = currentDirectory + ExpanderList.SelectedItem + "/"; // Reset selected index to -1 (no selection) ExpanderList.SelectedIndex = -1; getFolders(); } } } }


It works really well, lol apart from the private void ListClicked(object sender, SelectionChangedEventArgs e) which sometimes fires more then once which has the effect of skipping through to deeper files then one level as intended. This behaviour doesn't seem to repeat when i place a breakpoint though on the if (ExpanderList.SelectedIndex != -1)  line which always returns -1 and prevents this if its not supposed to happen. I have seen evidence to suggest there is a bug with this method?, although i do not have the experience or front to accuse someone else..... its definitely me although this option has been playing on my mind more as i can not find a reason for it to be repeatedly firing.

really crucial i get to the bottom of this asap its driving me crazy 

Thank you in advance for any help

john harris


Viewing all articles
Browse latest Browse all 18858

Trending Articles