프로그래밍 TIP/.NET [C#] RichTextBox 안에 컨트롤 삽입하고 스크롤 하기 여름나라겨울이야기 2009. 2. 16. 10:40 728x90 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 TestFormn { public partial class Form1 : Form { // RichTextBox 에 Add 시킬 버튼 private Button btn = new Button(); // 버튼의 초기 Location private Point deltaPoint = new Point(200, 100); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { StringBuilder sb = new StringBuilder(); sb.Append("우선 답변 감사하구용~\r\n"); sb.Append("제가 약간 질문을 어설프게 올렸나봐요..^^;;\r\n"); sb.Append("글자를 비교하는 방법 말고는 다른 방법은 없는지요?\r\n"); sb.Append("예를 들면 버튼등을 RichTextBox안에 종속 시키고 싶은데..\r\n"); sb.Append("control.add 해서 RichTextBox안에 넣는 방법은 찾았는데..\r\n"); sb.Append("해당 버튼의 위치를 어떻게 잡아야 할지를 모르겠네요..\r\n"); richTextBox1.Text = sb.ToString(); richTextBox1.AppendText(sb.ToString()); richTextBox1.AppendText(sb.ToString()); richTextBox1.AppendText(sb.ToString()); btn.Text = "모르겠어요"; btn.Location = deltaPoint; richTextBox1.Controls.Add(btn); } private void richTextBox1_VScroll(object sender, EventArgs e) { // 상하 스크롤시에 새로운 Location Point newLocation = new Point(); newLocation.X = btn.Location.X; // 첫글자와의 간격을 이용해 새로운 Y 위치 계산 newLocation.Y = richTextBox1.GetPositionFromCharIndex(0).Y + deltaPoint.Y; btn.Location = newLocation; } private void richTextBox1_HScroll(object sender, EventArgs e) { // 좌우 스크롤시에 새로운 Location Point newLocation = new Point(); // 첫글자와의 간격을 이용해 새로운 X 위치 계산 newLocation.X = richTextBox1.GetPositionFromCharIndex(0).X + deltaPoint.X; newLocation.Y = btn.Location.Y; btn.Location = newLocation; } } } 반응형