Example #1
0
        private void saveBtn_Click(object sender, RoutedEventArgs e)
        {
            List <ComputerRecord> toSave = records
                                           .OrderBy(x => x.SchoolClass)
                                           .ThenBy(x => x.Surname)
                                           .ThenBy(x => x.Name).ToList();

            string[] duplicates = toSave.Select(i => i.CName)
                                  .GroupBy(x => x)
                                  .Where(g => g.Count() > 1)
                                  .Select(y => y.Key)
                                  .ToArray();

            if (duplicates.Length > 0)
            {
                MessageBox.Show(
                    "Seznam nelze uložit protože obsahuje duplicitní záznamy:"
                    + Environment.NewLine
                    + Environment.NewLine
                    + String.Join(Environment.NewLine, duplicates),
                    "Chyba: duplicitní záznamy"
                    );
                return;
            }

            DateTime thisDay         = DateTime.Today;
            string   defaultFilename = String.Format("Licence_{0}_{1:00}_{2:00}", thisDay.Year, thisDay.Month, thisDay.Day);

            try
            {
                SaveFileDialog dlg = new SaveFileDialog();
                dlg.FileName   = defaultFilename;
                dlg.DefaultExt = ".xml";                       // Default file extension
                dlg.Filter     = "XML documents (.xml)|*.xml"; // Filter files by extension

                // Show save file dialog box
                Nullable <bool> result = dlg.ShowDialog();

                // Process save file dialog box results
                if (result == true)
                {
                    // Save document
                    string filename = dlg.FileName;
                    XMLSerialization.WriteToXmlFile(dlg.FileName, toSave);
                }
            }
            catch
            {
                string           message = "Chyba zápisu do souboru";
                string           caption = "Chyba";
                MessageBoxButton buttons = MessageBoxButton.OK;
                MessageBoxImage  icon    = MessageBoxImage.Error;
                MessageBoxResult result  = MessageBox.Show(message, caption, buttons, icon);
            }
        }
Example #2
0
        private void load_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.DefaultExt = ".xml";                       // Default file extension
            openFileDialog.Filter     = "XML documents (.xml)|*.xml"; // Filter files by extension
            if (openFileDialog.ShowDialog() == true)
            {
                try
                {
                    records = XMLSerialization.ReadFromXmlFile <List <ComputerRecord> >(openFileDialog.FileName);
                }
                catch
                {
                    string           message = "Nelze načíst soubor";
                    string           caption = "Chyba";
                    MessageBoxButton buttons = MessageBoxButton.OK;
                    MessageBoxImage  icon    = MessageBoxImage.Error;
                    MessageBoxResult result  = MessageBox.Show(message, caption, buttons, icon);
                }
            }
            showData();
        }