Exemplo n.º 1
0
        public void CreateFilter()
        {
            FilterList.ClearSelected();
            for (int x = 0; x < filters.Count; x++)
            {
                Filter f = filters.ToArray()[x];
                for (int i = 0; i < FilterList.SelectedItems.Count; i++)
                {
                    String Name = FilterList.SelectedItems[i].ToString();
                    if (f.Name.Equals(Name))
                    {
                        filters.Remove(f);
                        FilterList.Items.Remove(FilterList.SelectedItems[i]);
                    }
                }
            }

            Filter filter = new Filter();

            filter.Name            = FilterNameTextBox.Text;
            filter.filterOption    = (IncludeCheckBox.Checked == true ? filterOption.Include : filterOption.Exclude);
            filter.isCaseSenestive = CaseSensetiveCheckBox.Checked;
            filter.ToApply         = ColumnBox.SelectedItem.ToString();
            filter.Text            = TextToCompare.Text;
            filters.Add(filter);
            addToList(filter.Name);
            ResetFields();

            try
            {
                String FilterDir = @".\Filters\" + filter.Name;
                using (Stream stream = new FileStream(FilterDir, FileMode.Create, FileAccess.ReadWrite, FileShare.None))
                {
                    BinaryFormatter formater = new BinaryFormatter();
                    formater.Serialize(stream, filter);
                    stream.Close();
                }
            }
            catch (IOException)
            {
                var opinion = MessageBox.Show("There is alrady a filter with this name\n would you like to overwrite it?");
                switch (opinion)
                {
                case DialogResult.Yes:
                    String FilterDir = @".\Filters\" + filter.Name;
                    using (Stream stream = new FileStream(FilterDir, FileMode.Truncate, FileAccess.ReadWrite, FileShare.None))
                    {
                        BinaryFormatter formater = new BinaryFormatter();
                        formater.Serialize(stream, filter);
                        stream.Close();
                    }
                    break;
                }
            }
        }