■ RichTextBox 클래스에서 테이블을 추가하는 방법을 보여준다.
▶ RTFTable.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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
using System.Linq; using System.Text; namespace TestProject { /// <summary> /// RTF 테이블 /// </summary> public class RTFTable { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 내부 마진 /// </summary> public int InternalMargin = 180; /// <summary> /// 행 카운트 /// </summary> public int RowCount; /// <summary> /// 열 카운트 /// </summary> public int ColumnCount; /// <summary> /// 열 너비 배열 /// </summary> public int[] ColumnWidthArray = null; /// <summary> /// 컨텐트 배열 /// </summary> public string[,] ContentArray = null; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - RTFTable(rowCount, colunmnCount, internalMargin) /// <summary> /// 생성자 /// </summary> /// <param name="rowCount">행 카운트</param> /// <param name="colunmnCount">열 카운트</param> /// <param name="internalMargin">내부 마진</param> public RTFTable(int rowCount, int colunmnCount, int internalMargin) { RowCount = rowCount; ColumnCount = colunmnCount; InternalMargin = internalMargin; ColumnWidthArray = Enumerable.Repeat(1440, ColumnCount).ToArray(); ContentArray = new string[RowCount, ColumnCount]; for(int row = 0; row < RowCount; row++) { for(int column = 0; column < ColumnCount; column++) { ContentArray[row, column] = string.Empty; } } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 열 너비 배열 설정하기 - SetColumnWidthArray(int[] columnWidthArray) /// <summary> /// 열 너비 배열 설정하기 /// </summary> /// <param name="columnWidthArray">열 너비 배열</param> public void SetColumnWidthArray(params int[] columnWidthArray) { for(int column = 0; column < ColumnCount; column++) { ColumnWidthArray[column] = columnWidthArray[column]; } } #endregion #region 문자열 구하기 - ToString() /// <summary> /// 문자열 구하기 /// </summary> /// <returns>문자열</returns> public override string ToString() { StringBuilder stringBuilder = new StringBuilder(); string columnWidthString = GetColumnWidthString(); for(int row = 0; row < RowCount; row++) { stringBuilder.Append(@"\trowd"); stringBuilder.Append(@"\trgaph" + InternalMargin.ToString()); stringBuilder.Append(columnWidthString); for(int c = 0; c < ColumnCount; c++) { stringBuilder.Append(@"\pard\intbl{" + ContentArray[row, c].Replace(@"\", @"\\") + @"}\cell"); } stringBuilder.Append(@"\row"); } return stringBuilder.ToString(); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 열 너비 문자열 구하기 - GetColumnWidthString() /// <summary> /// 열 너비 문자열 구하기 /// </summary> /// <returns>열 너비 문자열</returns> private string GetColumnWidthString() { StringBuilder stringBuilder = new StringBuilder(); int totalWidth = 0; for(int column = 0; column < ColumnCount; column++) { totalWidth += ColumnWidthArray[column]; stringBuilder.Append(@"\cellx" + totalWidth.ToString()); } return stringBuilder.ToString(); } #endregion } } |
▶ 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 |
using System; using System.Windows.Forms; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.insertButton.Click += insertButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 테이블 추가 버튼 클릭시 처리하기 - insertButton_Click(sender, e) /// <summary> /// 테이블 추가 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void insertButton_Click(object sender, EventArgs e) { RTFTable table = new RTFTable(4, 3, 100); table.SetColumnWidthArray(900, 900, 900); for(int row = 0; row < 4; row++) { for(int column = 0; column < 3; column++) { table.ContentArray[row, column] = "(" + row.ToString() + ", " + column.ToString() + ")"; } } this.richTextBox.Rtf = this.richTextBox.Rtf.Replace("@@@", table.ToString()); } #endregion } } |