// exports credit cards in txt fiile
 private void btnExport_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         XMLSerialization.Export(users, "exportInfo.txt");
     }
     catch (DirectoryNotFoundException ex)
     {
         MessageBox.Show(
             string.Format("There was a problem exporting these files {0}", ex.Message),
             string.Format("Error of type {0}", e.GetType()));
     }
     catch (PathTooLongException ex)
     {
         MessageBox.Show(
             string.Format("There was a problem exporting these files {0}", ex.Message),
             string.Format("Error of type {0}", e.GetType()));
     }
     catch (Exception ex)
     {
         MessageBox.Show(
                 string.Format("there was a problem exporting these files {0}", ex.Message),
                 string.Format("Error of type {0}", e.GetType()));
     }
 }
Beispiel #2
13
        /// <summary>
        /// Handles the select/de-select of a row.
        /// </summary>
        /// <param name="rowNumber">The row number.</param>
        /// <param name="selectionSource">Selection source.</param>
        /// <param name="e">Routed event arguments.</param>
        private void HandleSelect(int rowNumber, SelectionSource selectionSource, RoutedEventArgs e)
        {
            bool controlKeyDown;
            bool shiftKeyDown;          

            if (this.mainView.View.Rows.Count < 1 || rowNumber >= this.mainView.View.Rows.Count || rowNumber < 0)
            {
                return;
            }

            DataBoundRow hitRow = this.mainView.View.Rows[rowNumber];
            List<DataBoundRow> removedItems = new List<DataBoundRow>();
            List<DataBoundRow> addedItems = new List<DataBoundRow>();
            removedItems = new List<DataBoundRow>();
            addedItems = new List<DataBoundRow>();

            KeyboardHelper.GetMetaKeyState(out controlKeyDown, out shiftKeyDown);

            this.selectedIndex = rowNumber;
            if (shiftKeyDown == false)
            {
                this.AnchorRowIndex = rowNumber;
            }

            bool multiSelectAllowed = (this.SelectionMode == SelectionMode.MultipleExtended || this.SelectionMode == SelectionMode.MultipleSimple);

            // Multi-select?
            if ((controlKeyDown && multiSelectAllowed && selectionSource != SelectionSource.Keyboard) || (this.forceSelect && multiSelectAllowed))
            {
                if (this.spacebarKeyDown)
                {
                    return;
                }

                //// De-select via Control Key and mouse
                if (this.SelectedItems.Contains(hitRow) == true)
                {
                    removedItems.Add(hitRow);                    
                    this.SelectedItems.Remove(hitRow);                                    
                    this.SelectedItem = this.SelectedItems.Count > 0 ? this.SelectedItems[this.SelectedItems.Count - 1] : null;
                    hitRow.Select(false);
                    hitRow.Highlight(true);  
                    hitRow.RowFocus = true;
                    hitRow.RowSelected = false;
                }
                else
                {
                    //// Select via Control Key and mouse
                    addedItems.Add(hitRow);
                    this.SelectedItems.Add(hitRow);
                    this.SelectedItem = hitRow;
                    hitRow.RowFocus = true;
                    hitRow.RowSelected = false;
                }

                if (this.lastRowHavingFocusIndex != -1 && hitRow.Index != this.lastRowHavingFocusIndex)
                {
                    this.MainView.Rows[this.lastRowHavingFocusIndex].Highlight(false);
                }               
            }           
            else if (controlKeyDown && multiSelectAllowed && selectionSource == SelectionSource.Keyboard)
            {
                hitRow.Highlight(true);
                hitRow.RowFocus = true;
                this.handlingControlMultiSelection = true;
                if (this.spacebarKeyDown)
                {
                    //// Select via Control Key and space-bar
                    if (this.SelectedItems.IndexOf(hitRow) == -1)
                    {
                        this.SelectedItems.Add(hitRow);
                        hitRow.Highlight(false);
                        hitRow.Select(true);                       
                        hitRow.RowSelected = true;
                        hitRow.RowFocus = true;
                    }
                    else
                    {
                        //// De-select via Control Key and space-bar
                        this.SelectedItems.Remove(hitRow);
                        hitRow.Select(false);
                        hitRow.Highlight(true);
                        hitRow.RowSelected = false;
                        hitRow.RowFocus = true;
                    }
                }
                else if (this.SelectedItems.IndexOf(hitRow) != -1)
                {
                    hitRow.Select(true);
                    hitRow.RowFocus = true;
                }
               
                if (this.lastHitRow != null)
                {
                    if (this.lastHitRow != hitRow)
                    {
                        this.lastHitRow.Highlight(false);
                        this.lastHitRow.RowFocus = false;
                    }

                    if (this.SelectedItems.IndexOf(this.lastHitRow) != -1)
                    {
                        this.lastHitRow.Select(true);
                    }
                }

                this.lastHitRow = hitRow;
            }
            else if ((shiftKeyDown && multiSelectAllowed))
            {
                //// Handle shift based selected
                int startRow = this.anchorRowIndex <= rowNumber ? this.anchorRowIndex : rowNumber;
                int endRow = this.anchorRowIndex <= rowNumber ? rowNumber : this.anchorRowIndex;
                DataBoundRow unselectingRow;
               
                // Items before selection range...
                for (int beforeRowIndex = 0; beforeRowIndex < startRow; beforeRowIndex++)
                {
                    unselectingRow = this.mainView.View.Rows[beforeRowIndex];
                    if (this.SelectedItems.Contains(unselectingRow) == true)
                    {                        
                        this.SelectedItems.Remove(unselectingRow);
                        removedItems.Add(unselectingRow);
                        unselectingRow.Select(false);
                        unselectingRow.RowSelected = false;
                        unselectingRow.RowFocus = false;
                    }
                }

                // Items in selection range...
                for (int selectRowIndex = startRow; selectRowIndex <= endRow; selectRowIndex++)
                {
                    DataBoundRow selectingRow = this.mainView.View.Rows[selectRowIndex];
                    if (this.SelectedItems.Contains(selectingRow) == false)
                    {
                        this.SelectedItems.Add(selectingRow);
                        addedItems.Add(selectingRow);
                        selectingRow.RowSelected = true;
                        selectingRow.RowFocus = false;
                    }

                    this.SelectedItem = selectingRow;
                }

                // Items after selection range...
                for (int afterRowIndex = endRow + 1; afterRowIndex < this.mainView.View.Rows.Count - 1; afterRowIndex++)
                {
                    unselectingRow = this.mainView.View.Rows[afterRowIndex];
                    if (this.SelectedItems.Contains(unselectingRow) == true)
                    {                        
                        this.SelectedItems.Remove(unselectingRow);
                        removedItems.Add(unselectingRow);
                        unselectingRow.Select(false);
                        unselectingRow.RowSelected = false;
                        unselectingRow.RowFocus = false;
                    }
                }

                if (e.GetType() == typeof(System.Windows.Input.KeyEventArgs))
                {
                    if (endRow + 2 == this.mainView.View.Rows.Count && ((KeyEventArgs)e).Key == Key.Up)
                    {
                        unselectingRow = this.mainView.View.Rows[endRow + 1];
                        if (this.SelectedItems.Contains(unselectingRow) == true)
                        {                           
                            this.SelectedItems.Remove(unselectingRow);
                            removedItems.Add(unselectingRow);
                            unselectingRow.Select(false);
                            unselectingRow.RowSelected = false;
                            unselectingRow.RowFocus = false;
                        }
                    }
                }
                else if (e.GetType() == typeof(System.Windows.Input.MouseButtonEventArgs))
                {
                    if (endRow + 1 != this.mainView.View.Rows.Count)
                    {
                        unselectingRow = this.mainView.View.Rows[this.mainView.View.Rows.Count - 1];
                        if (this.SelectedItems.Contains(unselectingRow) == true)
                        {                           
                            this.SelectedItems.Remove(unselectingRow);
                            removedItems.Add(unselectingRow);
                            unselectingRow.Select(false);
                            unselectingRow.RowSelected = false;
                            unselectingRow.RowFocus = false;
                        }
                    }
                }
            }            
            else
            {
                if (this.lastHitRow != null)
                {
                    if (this.lastHitRow != hitRow)
                    {
                        this.lastHitRow.Highlight(false);
                        this.lastHitRow.RowSelected = false;
                        this.lastHitRow.RowFocus = false;
                    }
                }

                this.ClearSelections();
                removedItems.AddRange(this.selectedItems);
                this.SelectedItems.Clear();
                removedItems.Remove(hitRow);
                addedItems.Add(hitRow);
                this.SelectedItems.Add(hitRow);
                this.SelectedItem = hitRow;
                hitRow.RowFocus = true;
                hitRow.RowSelected = true;
            }

            if (!this.handlingControlMultiSelection)
            {
                this.EnsureSelections();
            }

            this.spacebarKeyDown = false;
            this.handlingControlMultiSelection = false;
            this.EnsureRowVisible(rowNumber);
            this.lastRowHavingFocusIndex = rowNumber;          
            this.HandleSelectedRowCollection();
            if (this.OnSelectionChanged != null)
            {
#if SILVERLIGHT
                SelectionChangedEventArgs newSelectionChangedEventArgs = new SelectionChangedEventArgs(removedItems, addedItems);
#else
                // TODO: Check whether this implementation is correct
                SelectionChangedEventArgs newSelectionChangedEventArgs = new SelectionChangedEventArgs(e.RoutedEvent, removedItems, addedItems);
#endif
                this.OnSelectionChanged(this, newSelectionChangedEventArgs);
            }
        }