protected override async void OnDragDrop(DragEventArgs e) { base.OnDragDrop(e); if (e.Data.GetData(DataFormats.FileDrop) is string[] files && files.Any()) { EnableControls?.Invoke(false); try { using (var stream = new FileStream(files[0], FileMode.Open, FileAccess.Read, FileShare.Read)) { using (var sr = new StreamReader(stream, Encoding.UTF8, true)) { Text = await sr.ReadToEndAsync(); } } } catch { Text = "Error reading file"; } RefreshUndoRedoButton(); EnableControls?.Invoke(true); } }
private void BwXmlFormat_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if (e.Error != null) { ShowMessage?.Invoke(e.Error.Message); } else { Text = e.Result?.ToString(); RefreshUndoRedoButton(); } ClearAllSelections(XmlFormatButton.GetCurrentParent()); EnableControls?.Invoke(true); }
private async void LoadButtonEvent(object sender, EventArgs e) { if (openFileDialog1?.ShowDialog() == DialogResult.OK) { var fileInfo = new FileInfo(openFileDialog1.FileName); bool loadFile = true; if (fileInfo.Length > 1e7) { var reply = ShowMessage?.Invoke( "File is bigger than 10MB and this tool can have problem to show it. Are you sure?", "Request", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation); loadFile = reply == null || reply == DialogResult.OK; } if (loadFile) { try { EnableControls?.Invoke(false); using (var stream = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read, FileShare.Read)) { using (var sr = new StreamReader(stream, Encoding.UTF8, true)) { Text = await sr.ReadToEndAsync(); } } } catch (Exception ex) { ShowMessage?.Invoke(ex.Message); } finally { RefreshUndoRedoButton(); EnableControls?.Invoke(true); } } } }
private async void SaveButtonEvent(object sender, EventArgs e) { if (saveFileDialog1?.ShowDialog() == DialogResult.OK) { try { EnableControls?.Invoke(false); var buffer = Encoding.Default.GetBytes(Text); using (var fs = new FileStream(saveFileDialog1.FileName, FileMode.Create, FileAccess.Write, FileShare.None, buffer.Length, true)) { await fs.WriteAsync(buffer, 0, buffer.Length); } } catch (Exception ex) { ShowMessage?.Invoke(ex.Message); } finally { EnableControls?.Invoke(true); } } }
private void XmlFormatButton_Event(object sender, EventArgs e) { EnableControls?.Invoke(false); bwXmlFormat.RunWorkerAsync(Text); }