리치텍스트 박스는 Rtf 값(String) 읽으면, 그 안에 폰트나 색상 이런 정보까지 다 포함되어 있습니다.
이걸 그대로 저장했다가, 나중에 보고 싶은 때, 그대로 리치텍스트 박스에 로드하면, 원래 작성했던 형태 그대로 나옵니다.
리치텍스트 박스에 색깔이나 폰트를 하나만 쓰는 것도 아닌데, 그 많은 걸 어떻게 나눠서 저장할 거며, 나중에 불러와서 어떻게 조립할 건가요?
리치텍스트 박스에 1000 자를 치고, 1자나 몇자씩 씩 다 다른 색과 다른 폰트로 지정했어요.
리치텍스트 박스는 이런게 되죠.
질문자가 원하는 정보 추출했다 치고요... 이거 어떻게 디비에 저장할 건가요?
또, 리치텍스트 박스가 폰트나 색깔만 지정할 수 있는게 아니잖아요?
문단 설정도 할 수 있고, 밑줄도 그을 수 있는데... 이런거 다 어떻게 처리하실려고요.
아래 적어 드린 코드 실행해 보세요.
아주 간단하게 작성했습니다.
왼쪽이 리치텍스트 박스고, 오른쪽이 일반 텍스트 박스인데... 이걸 그냥 디비라고 생각하세요.
리치텍스트 박스에 있는 걸 꺼내서, 디비에 저장하는 대신 , 잠시 텍스트 박스에 옮겨 놨다가
다시 리치텍스트 박스로 옮겨 보는 거죠.
----- 코드 실행 후, 테스트 방법 ------
1. MS-Word 말고, 그냥 워드패드(wordpad.exe)에서, 복잡한 문장 작성해 보세요.
색깔도 여러개 넣고, 폰트도 바꾸고,.... 다 해보세요.
그리고, 그걸 그대로 복사해서 여기 리치텍스트 박스에 붙여 넣으세요.
워드패드에 보이던 그대로 보일겁니다.
이 상태에서 리치텍스트 박스에 있는 걸 조금 수정하셔도 되고요.
2.
save 버튼 눌러 봅니다.
오른쪽 텍스트 박스에 저장되죠? 디비에 저장한 셈 치죠.
실제로 Save 메서드 구현을 디비에 저장하는 방식으로 바꾸면 질문자 상황이랑 같아 집니다.
리치텍스트박스에 있는 내용은 지웠습니다.
텍스트박스에 있는 내용 보세요.
이상한 문자 많이 포함되어 있죠?
여기에 폰트며, 크기며, 색상 이런거 다 같이 저장되어 있습니다.
3.
이번엔 LOAD 버튼 눌러보세요.
오른쪽 텍스트박스에 있는 내용을 그대로 리치 텍스트로 옮깁니다.
디비에서 읽어 온 셈 칩니다.
아까 봤던 울긋불근한 텍스트가 그대로 다시 재현됩니다.
------------------------------------------------------------
UI 생성하는 디자인 코드 빼면
나머지 코드 아주 간단합니다.
Save 나 Load 메서드 코드 보세요.
복잡하게 폰트 추출하고 할 필요 없어요.
만약, 커서로 선택한 부분만 저장하고 싶다... 그러면
Save 메서드에서
this._rich.Rtf 대신에 this._rich.SelectedRtf 이거로 바꾸면 됩니다.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace RtfTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void _btnSave_Click(object sender, EventArgs e)
{
Save();
ClearRich();
}
private void _btnLoad_Click(object sender, EventArgs e)
{
Load();
}
private void ClearRich()
{
this._rich.Clear();
}
// 일반 텍스트 박스를 디비라 가정하자. 여기에 잠깐 저장.
private void Save()
{
this._tbx.Text = this._rich.Rtf;
}
// 디비(일반텍스트박스)에 있는 텍스트를 읽어서 리치텍스트에 로드한다.
private void Load()
{
this._rich.Rtf = this._tbx.Text;
}
#region --- Designer Code ---
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this._pnlRich = new System.Windows.Forms.Panel();
this._pnlText = new System.Windows.Forms.Panel();
this._pnlButtons = new System.Windows.Forms.Panel();
this.splitter1 = new System.Windows.Forms.Splitter();
this._rich = new System.Windows.Forms.RichTextBox();
this._tbx = new System.Windows.Forms.TextBox();
this._btnSave = new System.Windows.Forms.Button();
this._btnLoad = new System.Windows.Forms.Button();
this._pnlRich.SuspendLayout();
this._pnlText.SuspendLayout();
this._pnlButtons.SuspendLayout();
this.SuspendLayout();
//
// _pnlRich
//
this._pnlRich.Controls.Add(this._rich);
this._pnlRich.Dock = System.Windows.Forms.DockStyle.Fill;
this._pnlRich.Location = new System.Drawing.Point(0, 49);
this._pnlRich.Name = "_pnlRich";
this._pnlRich.Size = new System.Drawing.Size(712, 473);
this._pnlRich.TabIndex = 0;
//
// _pnlText
//
this._pnlText.Controls.Add(this._tbx);
this._pnlText.Dock = System.Windows.Forms.DockStyle.Right;
this._pnlText.Location = new System.Drawing.Point(410, 49);
this._pnlText.Name = "_pnlText";
this._pnlText.Size = new System.Drawing.Size(302, 473);
this._pnlText.TabIndex = 1;
//
// _pnlButtons
//
this._pnlButtons.Controls.Add(this._btnLoad);
this._pnlButtons.Controls.Add(this._btnSave);
this._pnlButtons.Dock = System.Windows.Forms.DockStyle.Top;
this._pnlButtons.Location = new System.Drawing.Point(0, 0);
this._pnlButtons.Name = "_pnlButtons";
this._pnlButtons.Size = new System.Drawing.Size(712, 49);
this._pnlButtons.TabIndex = 2;
//
// splitter1
//
this.splitter1.Dock = System.Windows.Forms.DockStyle.Right;
this.splitter1.Location = new System.Drawing.Point(407, 49);
this.splitter1.Name = "splitter1";
this.splitter1.Size = new System.Drawing.Size(3, 473);
this.splitter1.TabIndex = 3;
this.splitter1.TabStop = false;
//
// _rich
//
this._rich.Dock = System.Windows.Forms.DockStyle.Fill;
this._rich.Location = new System.Drawing.Point(0, 0);
this._rich.Name = "_rich";
this._rich.Size = new System.Drawing.Size(712, 473);
this._rich.TabIndex = 0;
this._rich.Text = "";
//
// _tbx
//
this._tbx.BackColor = System.Drawing.SystemColors.InactiveBorder;
this._tbx.Dock = System.Windows.Forms.DockStyle.Fill;
this._tbx.Location = new System.Drawing.Point(0, 0);
this._tbx.Multiline = true;
this._tbx.Name = "_tbx";
this._tbx.Size = new System.Drawing.Size(302, 473);
this._tbx.TabIndex = 0;
this._tbx.Text = "\r\n";
//
// _btnSave
//
this._btnSave.Location = new System.Drawing.Point(25, 11);
this._btnSave.Name = "_btnSave";
this._btnSave.Size = new System.Drawing.Size(75, 23);
this._btnSave.TabIndex = 0;
this._btnSave.Text = "SAVE";
this._btnSave.UseVisualStyleBackColor = true;
this._btnSave.Click += new System.EventHandler(this._btnSave_Click);
//
// _btnLoad
//
this._btnLoad.Location = new System.Drawing.Point(117, 11);
this._btnLoad.Name = "_btnLoad";
this._btnLoad.Size = new System.Drawing.Size(75, 23);
this._btnLoad.TabIndex = 1;
this._btnLoad.Text = "LOAD";
this._btnLoad.UseVisualStyleBackColor = true;
this._btnLoad.Click += new System.EventHandler(this._btnLoad_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(712, 522);
this.Controls.Add(this.splitter1);
this.Controls.Add(this._pnlText);
this.Controls.Add(this._pnlRich);
this.Controls.Add(this._pnlButtons);
this.Name = "Form1";
this.Text = "Form1";
this._pnlRich.ResumeLayout(false);
this._pnlText.ResumeLayout(false);
this._pnlText.PerformLayout();
this._pnlButtons.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Panel _pnlRich;
private System.Windows.Forms.RichTextBox _rich;
private System.Windows.Forms.Panel _pnlText;
private System.Windows.Forms.TextBox _tbx;
private System.Windows.Forms.Panel _pnlButtons;
private System.Windows.Forms.Button _btnLoad;
private System.Windows.Forms.Button _btnSave;
private System.Windows.Forms.Splitter splitter1;
#endregion
}
}