private void RichTextBox_Drop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) { string[] docPath = (string[])e.Data.GetData(DataFormats.FileDrop); // By default, open as Rich Text (RTF). var dataFormat = DataFormats.Rtf; // If the Shift key is pressed, open as plain text. if (e.KeyStates == DragDropKeyStates.ShiftKey) { dataFormat = DataFormats.Text; } System.Windows.Documents.TextRange range; System.IO.FileStream fStream; if (System.IO.File.Exists(docPath[0])) { try { // Open the document in the RichTextBox. range = new System.Windows.Documents.TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd); fStream = new System.IO.FileStream(docPath[0], System.IO.FileMode.OpenOrCreate); range.Load(fStream, dataFormat); fStream.Close(); } catch (System.Exception) { MessageBox.Show("File could not be opened. Make sure the file is a text file."); } } } }
public static string XamlToRtf(string xamlText) { var richTextBox = new System.Windows.Controls.RichTextBox(); var textRange = new System.Windows.Documents.TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd); // Create a MemoryStream of the xaml content using (var xamlMemoryStream = new MemoryStream()) { using (var xamlStreamWriter = new StreamWriter(xamlMemoryStream)) { xamlStreamWriter.Write(xamlText); xamlStreamWriter.Flush(); xamlMemoryStream.Seek(0, SeekOrigin.Begin); // Load the MemoryStream into TextRange ranging from start to end of RichTextBox. textRange.Load(xamlMemoryStream, DataFormats.Xaml); } } using (var rtfMemoryStream = new MemoryStream()) { textRange = new System.Windows.Documents.TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd); textRange.Save(rtfMemoryStream, DataFormats.Rtf); rtfMemoryStream.Seek(0, SeekOrigin.Begin); using (var rtfStreamReader = new StreamReader(rtfMemoryStream)) { return(rtfStreamReader.ReadToEnd()); } } }
/// <summary> /// Reads rtf-file into a RichTextBox. Worked with small documents. /// In larger documents, Paragraphs and Blocks did not match. Couldn't match the rtf-data with the position in the document. /// </summary> /// <param name="rtfFileName"></param> /// <returns></returns> private static IEnumerable <string> RtfFileToParagraphs(string rtfFileName) { var result = new List <string>(); var tb = new System.Windows.Controls.RichTextBox(); using (var fs = new FileStream(rtfFileName, FileMode.Open)) { tb.Selection.Load(fs, System.Windows.DataFormats.Rtf); } int inl = 0; foreach (var source in tb.Document.Blocks) { inl++; using (var stream = new MemoryStream()) { var sourceRange = new System.Windows.Documents.TextRange(source.ContentStart, source.ContentEnd); sourceRange.Save(stream, System.Windows.DataFormats.Rtf); var rtf = Encoding.Default.GetString(stream.ToArray()); if (!string.IsNullOrEmpty(rtf)) { //Add new line at end of paragraph. rtf = rtf.Substring(0, rtf.LastIndexOf('}')) + @"\line" + @"}"; result.Add(rtf); } else { result.Add(@"{\line}"); } } } return(result); }
private void loadScreenData(SchemaInfo commonSchema) { logger.Info("loadScreenData called"); //string tempIndefaceName = input.ClassInfos[index].NameSpaceAndInterfaceName; //lblInterfaceName.Text = tempIndefaceName; //ClassInfo classInfo = commonSchema.DepandancyClasses.FirstOrDefault((ClassInfo x) => x.NameSpaceAndInterfaceName == tempIndefaceName); var txt = new System.Windows.Documents.TextRange(txtInput.Document.ContentStart, txtInput.Document.ContentEnd); var tempData = input.LastRunFound != null? Json.Encode(input.LastRunFound.InputValues) : Json.Encode(input.SchemaInfo.InputValues); tempData = tempData.Replace("}", "}\n").Replace("\"DefaultValue", "\n\"DefaultValue"); txt.Text = tempData; //CboProjectName_SelectionChanged(cboProjectName, null); }
private void Ok_Click(object sender, RoutedEventArgs e) { logger.Info("ok_Click called"); try { input.SchemaInfo.StartAppProject = new [] { input.SchemaInfo.StartAppProject[cboProjectName.SelectedIndex] }; var inputValues = new System.Windows.Documents.TextRange(txtInput.Document.ContentStart, txtInput.Document.ContentEnd).Text; input.SchemaInfo.InputValues = Json.Decode <InputValue[]>(inputValues.Replace("\n", "")); input.CallbackOption.Invoke(); } catch (Exception ex) { logger.Error("ok_Click error", ex); MessageBox.Show(string.Format(CultureInfo.CurrentUICulture, "please fix you json input data."), "error"); } }
public void AddConsoleColorText(string text, string color) { // same as AddConsoleText, but in color if (Console == null) { return; } AddConsoleText(text); SolidColorBrush brush; switch (color) { case "yellow": brush = Brushes.Yellow; break; case "red": brush = Brushes.Red; break; case "green": brush = Brushes.LimeGreen; break; case "blue": brush = Brushes.DodgerBlue; break; default: brush = Brushes.Yellow; break; } for (int i = Console.Document.Blocks.Count - 1; i > 0; i--) { var paragraph = Console.Document.Blocks.ElementAt(i); var text2 = new System.Windows.Documents.TextRange(paragraph.ContentStart, paragraph.ContentEnd).Text; if (text2 == text) { paragraph.Foreground = brush; break; } } }