■ RichEditControl 클래스에서 특정 문자열을 포함하는 문단을 볼드체로 설정하는 방법을 보여준다.
▶ MainForm.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
using System.Collections.Generic; using System.Drawing; using DevExpress.XtraEditors; using DevExpress.XtraRichEdit; using DevExpress.XtraRichEdit.API.Native; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : XtraForm { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.richEditControl.ActiveViewType = RichEditViewType.Simple; this.richEditControl.Options.HorizontalScrollbar.Visibility = RichEditScrollbarVisibility.Hidden; this.richEditControl.Options.VerticalScrollbar.Visibility = RichEditScrollbarVisibility.Auto; this.richEditControl.Document.DefaultCharacterProperties.FontName = "나눔고딕코딩"; this.richEditControl.Document.DefaultCharacterProperties.FontSize = 12f; this.richEditControl.Document.DefaultCharacterProperties.ForeColor = Color.Black; this.richEditControl.Document.ParagraphStyles[0].FontName = "나눔고딕코딩"; this.richEditControl.Document.ParagraphStyles[0].FontSize = 12f; this.richEditControl.Document.ParagraphStyles[0].ForeColor = Color.Black; this.richEditControl.Document.InsertText(this.richEditControl.Document.Range.End, "가나다\r\n"); this.richEditControl.Document.InsertText(this.richEditControl.Document.Range.End, "라마바\r\n"); this.richEditControl.Document.InsertText(this.richEditControl.Document.Range.End, "사아자\r\n"); this.richEditControl.Document.InsertText(this.richEditControl.Document.Range.End, "차카타\r\n"); SetFontBold(this.richEditControl, new List<string> { "가", "자" }); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 폰트 볼드체 만들기 - SetFontBold(richEditControl, sourceList) /// <summary> /// 폰트 볼드체 만들기 /// </summary> /// <param name="richEditControl">리치 에디트 컨트롤</param> /// <param name="sourceList">소스 리스트</param> private void SetFontBold(RichEditControl richEditControl, List<string> sourceList) { if(sourceList == null || sourceList.Count == 0) { return; } Document document = richEditControl.Document; for(int i = 0; i < richEditControl.Document.Paragraphs.Count; i++) { DocumentRange documentRange = richEditControl.Document.Paragraphs[i].Range; string text = richEditControl.Document.GetText(documentRange); foreach(string source in sourceList) { if(text.Contains(source)) { CharacterProperties characterProperties = document.BeginUpdateCharacters(documentRange); characterProperties.Bold = true; document.EndUpdateCharacters(characterProperties); } } } } #endregion } } |