private void UnderLineButton_Click(object sender, RoutedEventArgs e) { TextSelection text = this.WhitodoText.Selection; if (!text.IsEmpty) { bool isUnderline = false; try { TextDecorationCollection ul = (TextDecorationCollection)text.GetPropertyValue(TextBlock.TextDecorationsProperty); if (ul == TextDecorations.Underline) { isUnderline = true; } } catch (Exception) { } if (isUnderline) { text.ApplyPropertyValue(TextBlock.TextDecorationsProperty, null); this.UnderLineButton.Background = Brushes.White; } else { text.ApplyPropertyValue(TextBlock.TextDecorationsProperty, TextDecorations.Underline); this.UnderLineButton.Background = Brushes.LightPink; } } this.WhitodoText.Focus(); }
private void cmdBold_Click(object sender, RoutedEventArgs e) { TextSelection selection = richTextBox.Selection; // You could check for no selection, but here the code simply applies the bold formatting // to the empty selection (the insertion point), so that when the user starts typing the // new text will be bold. // GetPropertyValue() returns null if the selection has mixed font bolding. // It's up to you what to do in this case, but here the application simply bolds // the entire selection. FontWeight currentState = FontWeights.Normal; if (selection.GetPropertyValue(Run.FontWeightProperty) != DependencyProperty.UnsetValue) { currentState = (FontWeight)selection.GetPropertyValue(Run.FontWeightProperty); } if (currentState == FontWeights.Normal) { selection.ApplyPropertyValue(Run.FontWeightProperty, FontWeights.Bold); } else { selection.ApplyPropertyValue(Run.FontWeightProperty, FontWeights.Normal); } // A nice detail is to bring the focus back to the text box, so the user can resume typing. richTextBox.Focus(); }
private void MailRichTextFont(int FontProperty, object value) { TextSelection ts = MailRichText.Selection; if (ts != null) { if (FontProperty == 1) { ts.ApplyPropertyValue(TextElement.FontWeightProperty, value); } else if (FontProperty == 2) { ts.ApplyPropertyValue(TextElement.FontStyleProperty, value); } else if (FontProperty == 3) { ts.ApplyPropertyValue(Inline.TextDecorationsProperty, value); } else if (FontProperty == 4) { BrushConverter bc = new BrushConverter(); ts.ApplyPropertyValue(TextElement.ForegroundProperty, bc.ConvertFromString(value.ToString())); } } MailRichText.Focus(); }
private void Scramble() { try { RichBox.SelectAll(); TextSelection sel = RichBox.Selection; int DocSize = RichBox.Document.ContentStart.GetOffsetToPosition(RichBox.Document.ContentEnd); for (int i = 0; i < DocSize - 5; i++) { TextPointer pointer1 = RichBox.Document.ContentStart.GetPositionAtOffset(i * 3); TextPointer pointer2 = RichBox.Document.ContentStart.GetPositionAtOffset(i * 3 + 3); RichBox.Selection.Select(pointer1, pointer2); sel = RichBox.Selection; sel.ApplyPropertyValue(FontFamilyProperty, new FontFamily(RandomFontList[rng.Next(RandomFontList.Count)])); sel.ApplyPropertyValue(FontSizeProperty, 12 + 8 * rng.NextDouble()); sel.ApplyPropertyValue(ForegroundProperty, "#FF" + rng.Next(0, 128).ToString("X2") + rng.Next(0, 128).ToString("X2") + rng.Next(0, 128).ToString("X2")); } } catch { // I the developer think this try-catch is not quite the right solution // but it works and it would probably cost at least 30 minutes to fund a better solution } }
private void Alignment(object sender, RoutedEventArgs e) { // TO - DO - CHANGE THE LOGIC, IF POSSIBLE - ISCHECKED sel = page1.Selection; switch (((System.Windows.Controls.Primitives.ToggleButton)sender).Name) { case "alignmentLeft": sel.ApplyPropertyValue(Paragraph.TextAlignmentProperty, TextAlignment.Left); alignmentCenter.IsChecked = alignmentRight.IsChecked = alignmentJustify.IsChecked = false; break; case "alignmentCenter": sel.ApplyPropertyValue(Paragraph.TextAlignmentProperty, TextAlignment.Center); alignmentLeft.IsChecked = alignmentRight.IsChecked = alignmentJustify.IsChecked = false; break; case "alignmentRight": sel.ApplyPropertyValue(Paragraph.TextAlignmentProperty, TextAlignment.Right); alignmentCenter.IsChecked = alignmentLeft.IsChecked = alignmentJustify.IsChecked = false; break; case "alignmentJustify": sel.ApplyPropertyValue(Paragraph.TextAlignmentProperty, TextAlignment.Justify); alignmentCenter.IsChecked = alignmentLeft.IsChecked = alignmentRight.IsChecked = false; break; } }
private void ItalicButton_Click(object sender, RoutedEventArgs e) { TextSelection text = this.WhitodoText.Selection; if (!text.IsEmpty) { bool isItalic = false; try { FontStyle fs = (FontStyle)text.GetPropertyValue(System.Windows.Controls.RichTextBox.FontStyleProperty); if (fs == FontStyles.Italic) { isItalic = true; } } catch (Exception) { } if (isItalic) { text.ApplyPropertyValue(System.Windows.Controls.RichTextBox.FontStyleProperty, FontStyles.Normal); this.ItalicButton.Background = Brushes.White; } else { text.ApplyPropertyValue(System.Windows.Controls.RichTextBox.FontStyleProperty, FontStyles.Italic); this.ItalicButton.Background = Brushes.LightPink; } } this.WhitodoText.Focus(); }
private void Clear() { RichBox.SelectAll(); TextSelection sel = RichBox.Selection; sel.ApplyPropertyValue(FontFamilyProperty, new FontFamily("Comic Sans MS")); sel.ApplyPropertyValue(FontSizeProperty, (double)12); sel.ApplyPropertyValue(ForegroundProperty, "#FF000000"); }
private void removeTextColor(RichTextBox rtb) { //Select all and remove all the foreground and font weight element property rtb.SelectAll(); TextSelection currentSelection = rtb.Selection; currentSelection.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Black)); currentSelection.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Normal); }
private void clearButton_Click(object sender, RoutedEventArgs e) { TextSelection text = GetSelectionText(); text.ApplyPropertyValue(Run.FontStyleProperty, FontStyles.Normal); text.ApplyPropertyValue(Run.TextDecorationsProperty, null); text.ApplyPropertyValue(Run.FontWeightProperty, FontWeights.Normal); text.ApplyPropertyValue(RichTextBox.FontSizeProperty, 16.0); text.ApplyPropertyValue(Run.ForegroundProperty, new SolidColorBrush(Colors.Black)); }
public void ApplyPropertyValue() { TextSelection ts = rtb.Selection; Assert.Throws <NullReferenceException> (delegate { ts.ApplyPropertyValue(null, new object()); }, "null,object"); Assert.Throws <ArgumentException> (delegate { ts.ApplyPropertyValue(RichTextBox.BorderThicknessProperty, null); }, "DP,null"); }
public void promeniBojuTeksta(bool pritisnutoDugme, TextSelection selekcija, Brush boja) { if (pritisnutoDugme) { selekcija.ApplyPropertyValue(Inline.ForegroundProperty, boja); } else { selekcija.ApplyPropertyValue(Inline.ForegroundProperty, Brushes.Black); } }
public void promeniPozadinuTeksta(bool pritisnutoDugme, TextSelection selekcija, Brush boja) { if (pritisnutoDugme) { selekcija.ApplyPropertyValue(Inline.BackgroundProperty, boja); } else { selekcija.ApplyPropertyValue(Inline.BackgroundProperty, null); } }
private void TxtToUrl(object sender, RoutedEventArgs e) { // TO-DO // Open the URL when holding CTRL + Click string url = page1.Selection.Text; if (url.StartsWith("www.") || url.StartsWith("https://") || url.StartsWith("http://")) { sel.ApplyPropertyValue(Inline.TextDecorationsProperty, TextDecorations.Underline); sel.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Color.FromRgb(33, 124, 255))); } }
public static void ApplyTextStyle(this TextSelection selection, ITextStyle style) { if (selection == null || selection.Start == selection.End) { return; } selection.ApplyPropertyValue(TextElement.FontFamilyProperty, style.FontFamily); selection.ApplyPropertyValue(TextElement.FontSizeProperty, style.FontSize * 96d / 72d); selection.ApplyPropertyValue(TextElement.FontStyleProperty, style.FontStyle); selection.ApplyPropertyValue(TextElement.FontWeightProperty, style.FontWeight); selection.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(style.FontColor)); FlowDocumentHelper.SetStyleName(selection.Start.Parent, style.Name); }
private void FontEdit_click(object sender, RoutedEventArgs e) { FontDialog fontDialog = new FontDialog(); if (fontDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { TextSelection selection = textBox.Selection; if (selection != null && !selection.IsEmpty) { selection.ApplyPropertyValue(TextElement.FontFamilyProperty, fontDialog.Font.FontFamily.Name); selection.ApplyPropertyValue(TextElement.FontSizeProperty, fontDialog.Font.SizeInPoints.ToString()); } } }
private void boldFontButton_Click(object sender, RoutedEventArgs e) { TextSelection selectedText = textBox.Selection; if (selectedText.GetPropertyValue(TextElement.FontWeightProperty) == (object)FontWeights.Bold) { selectedText.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Normal); } else { selectedText.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold); } textBox.Focus(); }
private void underlineFontButton_Click(object sender, RoutedEventArgs e) { TextSelection selectedText = textBox.Selection; if (selectedText.GetPropertyValue(TextElement.FontStyleProperty) == (object)FontStyles.Oblique) { selectedText.ApplyPropertyValue(TextElement.FontStyleProperty, FontStyles.Normal); } else { selectedText.ApplyPropertyValue(TextElement.FontStyleProperty, FontStyles.Oblique); } textBox.Focus(); }
private void fontSizeChanged(object sender, RoutedPropertyChangedEventArgs <double> e) { TextSelection selectedText = textBox.Selection; if (selectedText.Text != "") { selectedText.ApplyPropertyValue(TextElement.FontSizeProperty, fontSizeSlider.Value); } else { selectedText.ApplyPropertyValue(TextElement.FontSizeProperty, fontSizeSlider.Value); } textBox.Focus(); }
private void cursiveFontButton_Click(object sender, RoutedEventArgs e) { TextSelection selectedText = textBox.Selection; if ((FontStyle)selectedText.GetPropertyValue(TextElement.FontStyleProperty) == FontStyles.Italic) { selectedText.ApplyPropertyValue(TextElement.FontStyleProperty, FontStyles.Normal); } else { selectedText.ApplyPropertyValue(TextElement.FontStyleProperty, FontStyles.Italic); } textBox.Focus(); }
/// <summary> /// Background colour document. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BackgroundColour_Click(object sender, RoutedEventArgs e) { System.Windows.Forms.ColorDialog colorDialog = new System.Windows.Forms.ColorDialog(); colorDialog.AllowFullOpen = true; // If ok. if (colorDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { System.Windows.Media.Color col = new System.Windows.Media.Color(); col.A = colorDialog.Color.A; col.B = colorDialog.Color.B; col.G = colorDialog.Color.G; col.R = colorDialog.Color.R; // Get the selected colour. System.Windows.Media.Brush brush = new SolidColorBrush(col); //Current word at the pointer // Get the selected text. TextSelection text = mainRTB.Selection; if (!text.IsEmpty) { text.ApplyPropertyValue(TextElement.BackgroundProperty, brush); } } }
private void Subscript(object sender, RoutedEventArgs e) { sel = page1.Selection; if ((bool)superscript.IsChecked) { superscript.IsChecked = false; } sel.ApplyPropertyValue(Inline.BaselineAlignmentProperty, BaselineAlignment.Subscript); if (!(bool)subscript.IsChecked && !(bool)superscript.IsChecked) { sel.ApplyPropertyValue(Inline.BaselineAlignmentProperty, BaselineAlignment.Baseline); } }
public void promeniVelicinuFonta(TextSelection selekcija, int velicina) { if (!selekcija.IsEmpty) { selekcija.ApplyPropertyValue(TextElement.FontSizeProperty, velicina.ToString()); } }
public void promeniFont(TextSelection selekcija, FontFamily f) { if (selekcija != null && f != null) { selekcija.ApplyPropertyValue(Inline.FontFamilyProperty, f); } }
private void selectFontStyle_Select(object sender, RoutedEventArgs e) { TextSelection selectedText = textBox.Selection; selectedText.ApplyPropertyValue(FontFamilyProperty, Fonts.SystemFontFamilies.ElementAt(fontStylesComboBox.SelectedIndex)); textBox.Focus(); }
internal static void ApplyNewValueToFormattingProperty <T>( TextSelection selection, DependencyProperty formattingProperty, T newValue, Func <T, T, Boolean> equals) { Object propertyValue = GetSelectionPropertyValue(selection, formattingProperty); Boolean shouldUpdate; if (propertyValue == null) { shouldUpdate = (Object)newValue != null; } else if (propertyValue == DependencyProperty.UnsetValue) { shouldUpdate = (Object)newValue != DependencyProperty.UnsetValue; } else if (propertyValue is T) { shouldUpdate = !equals((T)propertyValue, newValue); } else { throw new InvalidOperationException(); } if (shouldUpdate) { selection.ApplyPropertyValue(formattingProperty, newValue); } }
internal static void ToggleSelectionFormattingProperty <T>( TextSelection selection, DependencyProperty formattingProperty, T valueOn, T valueOff, Func <T, T, Boolean> equals) { Object propertyValue = GetSelectionPropertyValue(selection, formattingProperty); if (propertyValue is T) { propertyValue = equals((T)propertyValue, valueOn) ? valueOff : valueOn; } else if (propertyValue == null || propertyValue == DependencyProperty.UnsetValue) { propertyValue = valueOn; } else { throw new InvalidOperationException(); } selection.ApplyPropertyValue(formattingProperty, propertyValue); }
private void SizSlider(object sender, RoutedEventArgs e) { SizeTextBlock.Text = Convert.ToInt32(((Slider)sender).Value).ToString(); SizeText.Text = Convert.ToInt32(((Slider)sender).Value).ToString(); TextSelection textSelection = richTextBox.Selection; textSelection.ApplyPropertyValue(FontSizeProperty, ((Slider)sender).Value); }
private void colorPicker_Select(object sender, RoutedEventArgs e) { TextSelection selectedText = textBox.Selection; SolidColorBrush color = new SolidColorBrush(colorPicker.SelectedColor.Value); selectedText.ApplyPropertyValue(TextElement.ForegroundProperty, color); textBox.Focus(); }
private void underline_click(object sender, RoutedEventArgs e) { TextSelection selection = textBox.Selection; if (selection != null && !selection.IsEmpty) { selection.ApplyPropertyValue(Inline.TextDecorationsProperty, TextDecorations.Underline); } }
private void italic_click(object sender, RoutedEventArgs e) { TextSelection selection = textBox.Selection; if (selection != null && !selection.IsEmpty) { selection.ApplyPropertyValue(TextElement.FontStyleProperty, FontStyles.Italic); } }