Beispiel #1
0
        private void SaveToFileButton_Click(object sender, EventArgs e)
        {
            ProtocolFullModel protocol = new ProtocolFullModel
            {
                ID             = IDprotocolTextBox.Text,
                Name           = nameProtocolTextBox.Text,
                ProtocolHeader = headerProtocolTextBox.Text,
                Conclusion     = conclusionTextBox.Text,
                Elements       = protocolElements.ToList()
            };
            string         jsonFile = JsonConvert.SerializeObject(protocol, Newtonsoft.Json.Formatting.Indented);
            SaveFileDialog saveFile = new SaveFileDialog
            {
                Filter           = "JSon протокол (*.json) | *.json",
                Title            = "Сохранение протокола",
                InitialDirectory = settings.DefaultProtocolPath
            };

            if (saveFile.ShowDialog() == DialogResult.OK)
            {
                using (StreamWriter writer = new StreamWriter(saveFile.FileName))
                {
                    writer.Write(jsonFile);
                }
            }
            else
            {
                return;
            }
            fileName = saveFile.FileName
                       .Remove(0, saveFile.FileName.LastIndexOf('\\') + 1)
                       .Replace(".json", string.Empty);
            this.Text = "Creator - " + fileName;
            MessageBox.Show("Сохранение файла прошло успешно", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
Beispiel #2
0
        private void OpenFromFileButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog
            {
                Filter           = "JSon протокол (*.json) | *.json",
                Title            = "Открыть протокол",
                InitialDirectory = settings.DefaultProtocolPath
            };
            string buffer = string.Empty;

            if (openFile.ShowDialog() == DialogResult.OK)
            {
                using (StreamReader reader = new StreamReader(openFile.FileName))
                {
                    buffer = reader.ReadToEnd();
                }
            }
            else
            {
                return;
            }
            fileName = openFile.FileName
                       .Remove(0, openFile.FileName.LastIndexOf('\\') + 1)
                       .Replace(".json", string.Empty);
            this.Text = "Creator - " + fileName;

            if (protocolElements.Count != 0)
            {
                if (MessageBox.Show("Текущий протокол будет удален, сохранить перед закрытием?", "Внимание", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    saveToFileButton.PerformClick();
                }
            }
            ProtocolFullModel protocol = JsonConvert.DeserializeObject <ProtocolFullModel>(buffer);

            IDprotocolTextBox.Text     = protocol.ID;
            nameProtocolTextBox.Text   = protocol.Name;
            headerProtocolTextBox.Text = protocol.ProtocolHeader;
            conclusionTextBox.Text     = protocol.Conclusion;

            protocolElements.Clear();
            foreach (ProtocolElementModel item in protocol.Elements)
            {
                protocolElements.Add(item);
            }
        }