RichTextBox 붙여넣기

2020. 1. 15. 13:14C#

namespace WindowsFormsApp10

{

    public partial class Form1 : Form

    {

 

/* RichTextBox 문자 시작점 */

private int _TextStartIndex = 0;

 

/* RichTextBox  붙여넣기한 데이터 저장 */

private string _strPasteText = null;

 

 

        public Form1()

        {

            InitializeComponent();

 

/* PreviewKeyDown 이벤트 등록 */           

            richTextBox1.PreviewKeyDown += new PreviewKeyDownEventHandler(RichTextBox_PreviewKeyDown);

 

/* TextChanged 이벤트 등록 */

            richTextBox1.TextChanged += new EventHandler(RichTextBox_TextChanged);           

        }

               

        private void RichTextBox_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)

        {

            if( e.Control && e.KeyCode == Keys.V )

            {

                RichTextBox txt = (RichTextBox)sender;

 

                /* RichTextBox 의 현재 문자열 길이가 0 일 경우 줄넘기 발생시킴 */

                if (txt.TextLength == 0)

                {

                    txt.AppendText("\r\n");

                }

 

                /* 문자 붙여넣기 전의 RichTextBox 문자 길이 저장 */

                _TextStartIndex = txt.TextLength;

            }

        }

 

        private void RichTextBox_TextChanged(object sender, EventArgs e)

        {

            if (_TextStartIndex != 0)

            {

                RichTextBox txt = (RichTextBox)sender;

 

                /* 붙여넣기된 문자 선택 */

                txt.Select(_TextStartIndex, txt.TextLength);

 

                /* 붙여넣기된 문자만 저장 */

                _strPasteText = txt.SelectedText;

 

                /* 커서 맨 끝으로 이동 */

                txt.Select(txt.TextLength, txt.TextLength);

 

                /* 시작점 초기화 */

                _TextStartIndex = 0;

 

                Console.WriteLine(_strPasteText);

            }

        }

}

}

'C#' 카테고리의 다른 글

Timer Event ( System.Windows_Forms.Timer )  (0) 2020.01.15