2012-08-08 4 views
3

RichTextBox에서 선택한 텍스트의 스타일 (예 : 글꼴, 글꼴 크기, 브러시)을 변경하려면 어떻게해야합니까?RichTextBox에서 선택한 텍스트의 스타일 변경

업데이트 :RichTextBox과 툴바가 있다고 가정 해 봅니다. 사용자가 와서 RichTextBox 상자 안의 텍스트를 선택하고 툴바에서 글꼴 크기를 변경합니다. 선택한 텍스트의 스타일을 변경하고 싶습니다.

+0

검색했지만 유용한 예제 DJ를 찾을 수 없습니다. – saber

+0

정말 좋아, 너 웹에서 찾을 수 없었던 것을하고 싶니? 아마도 당신은 당신의 질문을 업데이트하고 당신이하고 싶은 것에 대한 정확한 예를 들어야합니다 .. – MethodMan

+0

잘하면 그걸로 충분할 것입니다. 당신은 시작했습니다 .. – MethodMan

답변

9

WPF

if (this.TextEditor.Selection.IsEmpty) 
    this.TextEditor.CurrentFontFamily = SelectedFont; 
else 
    this.TextEditor.Selection.ApplyPropertyValue(TextElement.FontFamilyProperty, SelectedFont); 

또는 다른 WPF 예 여기

private void ChangeTextProperty(DependencyProperty dp, string value) 
    { 
     if (mainRTB == null) return; 

     TextSelection ts = richTextBox.Selection; 
     if (ts!=null) 
      ts.ApplyPropertyValue(dp, value); 
     richTextBox.Focus(); 
    } 

은 몇 가지 예 윈도우 있습니다 (WPF되지 않음) 글꼴 & 글꼴 색상을

richTextBox1.SelectionFont = new Font("Tahoma", 12, FontStyle.Bold); 
richTextBox1.SelectionColor = System.Drawing.Color.Red; 

아래 또 다른 예 변경 (wpf가 아님)

private void WriteTextToRichTextBox() 
{ 
    // Clear all text from the RichTextBox; 
    richTextBox1.Clear(); 
    // Set the font for the opening text to a larger Arial font; 
    richTextBox1.SelectionFont = new Font("Arial", 16); 
    // Assign the introduction text to the RichTextBox control. 
    richTextBox1.SelectedText = "The following is a list of bulleted items:" + "\n"; 
    // Set the Font for the first item to a smaller size Arial font. 
    richTextBox1.SelectionFont = new Font("Arial", 12); 
    // Specify that the following items are to be added to a bulleted list. 
    richTextBox1.SelectionBullet = true; 
    // Set the color of the item text. 
    richTextBox1.SelectionColor = Color.Red; 
    // Assign the text to the bulleted item. 
    richTextBox1.SelectedText = "Apples" + "\n"; 
    // Apply same font since font settings do not carry to next line. 
    richTextBox1.SelectionFont = new Font("Arial", 12); 
    richTextBox1.SelectionColor = Color.Orange; 
    richTextBox1.SelectedText = "Oranges" + "\n"; 
    richTextBox1.SelectionFont = new Font("Arial", 12); 
    richTextBox1.SelectionColor = Color.Purple; 
    richTextBox1.SelectedText = "Grapes" + "\n"; 
    // End the bulleted list. 
    richTextBox1.SelectionBullet = false; 
    // Specify the font size and string for text displayed below bulleted list. 
    richTextBox1.SelectionFont = new Font("Arial", 16); 
    richTextBox1.SelectedText = "Bulleted Text Complete!"; 
} 
+3

SelectionFont 나 SelectionColor와 같은 속성이 없습니다. WPF입니다. – saber

+0

잠시만 기다려주십시오. – MethodMan

+0

DJ에게 감사드립니다. – saber

2

WPF RichTextBox의 경우 TextRange에 ApplyPropertyValue 메서드를 사용해야합니다. RichTextBox 인스턴스의 Selected 속성을 사용하여 선택한 TextRange를 가져올 수 있습니다.

var selection = myRichTextBox.Selection; 
if (!selection.IsEmpty) 
    selection.ApplyPropertyValue(TextElement.FontSizeProperty, 10.0); 
+0

이것은 WinForm이 아닌 WPF 응용 프로그램입니다. – saber

관련 문제