-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
156 lines (137 loc) · 6.13 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using Microsoft.Win32;
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using MassTextModifier.Classess;
using MassTextModifier.Model;
using MassTextModifier.Extension;
namespace MassTextModifier
{
public partial class MainWindow : Window
{
ObservableCollection<FileInfo> myItems = new ObservableCollection<FileInfo>();
public MainWindow()
{
InitializeComponent();
OverWriteRadioButton.IsChecked = true;
SelectOutputLocationButton.IsEnabled = false;
string outputFilePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
//this.outputFilePathLabel.Content = outputFilePath; // what's the difference between this.outputFilePathLabel.Content
outputFilePathLabel.Content = outputFilePath; // and outputFilePathLabel.Content?
// this refer to class MainWindow and so this. all the property of the class
// it means outputFilePathLabel is property of MainWindow
// hence this.outputFilePathLabel and outputFilePathLabel both are the same
}
public void Browse_Button_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Multiselect = true;
openFileDialog.ShowDialog();
var listOfItems = openFileDialog.FileNames.ToList();
foreach (var student in listOfItems)
{
FileInfo fileInfo = new FileInfo();
fileInfo.FilePath = student;
myItems.Add(fileInfo);
}
myListView.ItemsSource = myItems;
}
private void Delete_Button_Click(object sender, RoutedEventArgs e)
{
int myIndex = myListView.SelectedIndex;
try
{
myItems.RemoveAt(myIndex);
myListView.SelectedItem = myListView.Items[myIndex];
}
catch (Exception)
{
// I left it blank because I want the program to perform no action if there is an exception.
}
}
private void Delete_All_Button_Click(object sender, RoutedEventArgs e)
{
if (myItems != null)
{
myItems.Clear();
}
}
private void Sort_A_Z_Button_Click(object sender, RoutedEventArgs e)
{
myItems.Sort((a, b) => { return a.FileName.CompareTo(b.FileName); }); //Gautam2010 wrote this and I'm not 100% sure how it works
sortButton.Click += new RoutedEventHandler(Sort_Z_A_Button_Click);
sortButton.Click -= new RoutedEventHandler(Sort_A_Z_Button_Click);
sortButton.Content = "Sort Z-A";
}
private void Sort_Z_A_Button_Click(object sender, RoutedEventArgs e)
{
myItems.Sort((b, a) => { return a.FileName.CompareTo(b.FileName); }); //Gautam2010 wrote this and I'm not 100% sure how it works
sortButton.Click += new RoutedEventHandler(Sort_A_Z_Button_Click);
sortButton.Click -= new RoutedEventHandler(Sort_Z_A_Button_Click);
sortButton.Content = "Sort A-Z";
}
private void Execute_Button_Click(object sender, RoutedEventArgs e)
{
try
{
if (OverWriteRadioButton.IsChecked == true)
{
foreach (FileInfo itemFilePath in myItems)
{
textModifier.OverwriteFile(itemFilePath.FilePath, itemFilePath.FilePath);
Debug.WriteLine(System.IO.Path.GetFileName(itemFilePath.FilePath));
Debug.WriteLine(System.IO.Path.GetDirectoryName(itemFilePath.FilePath));
}
MessageBox.Show($"{myItems.Count} files have been modified");
myItems.Clear();
}
else
{
foreach (FileInfo itemfilePath in myItems)
{
string newFilePath = System.IO.Path.Combine(Convert.ToString(outputFilePathLabel.Content), System.IO.Path.GetFileName(itemfilePath.FilePath));
textModifier.OverwriteFile(itemfilePath.FilePath, newFilePath);
}
MessageBox.Show($"{myItems.Count} files have been saved at {outputFilePathLabel.Content}");
myItems.Clear();
}
}
catch
{
MessageBox.Show("Unexpected error occured", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void CreateNewRadioButton_Click(object sender, RoutedEventArgs e)
{
SelectOutputLocationButton.IsEnabled = true;
}
private void OverWriteRadioButton_Click(object sender, RoutedEventArgs e)
{
SelectOutputLocationButton.IsEnabled = false;
}
private void MyCheckBox_Click(object sender, RoutedEventArgs e)
{
if (MyCheckBox.IsChecked == true)
{
myListView.DisplayMemberPath = "FileName";
}
else
{
myListView.DisplayMemberPath = "FilePath";
}
}
private void SelectOutputLocationButton_Click(object sender, RoutedEventArgs e)
{
using (var dialog1 = new System.Windows.Forms.FolderBrowserDialog())
{
System.Windows.Forms.DialogResult result1 = dialog1.ShowDialog();
if (result1 == System.Windows.Forms.DialogResult.OK)
{
outputFilePathLabel.Content = dialog1.SelectedPath;
}
}
}
}
}