C#

C# richtextbox를 이용한 예제(복사,붙여넣기,바꾸기)

J개발자 2020. 12. 3. 00:24

Form1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 기말고사_공부하기
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
           
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
           richTextBox1.Copy();
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            richTextBox1.Paste();
        }

        private void toolStripMenuItem4_Click(object sender, EventArgs e)
        {
            richTextBox1.Cut();
        }

        private void toolStripMenuItem5_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectAll();
        }

        private void toolStripMenuItem6_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2(this);
            f2.Show();
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            richTextBox1.Undo();
        }
    }
}

Form2

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 기말고사_공부하기
{
    public partial class Form2 : Form
    {
        Form1 f1;
        int index = -1;
        bool findflag = false;
        public Form2(Form1 f)
        {
            f1 = f;
            InitializeComponent();



        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (index == -1)
            {
                string s1 = textBox1.Text;
                string s2 = textBox2.Text;
                string s3 = f1.richTextBox1.Text;

                index = f1.richTextBox1.Text.IndexOf(s1);
                if (index == -1)
                {
                    MessageBox.Show("찾을 단어 없음");

                }
                else
                {
                    index = f1.richTextBox1.Text.IndexOf(s1, 0);
                    int len = textBox1.TextLength;
                    f1.richTextBox1.Focus();
                    f1.richTextBox1.Select(index, len);

                }
            }
            else
            {
                StringBuilder buffer = new StringBuilder();
                string s1 = textBox1.Text;
                string s2 = textBox2.Text;
                string s3 = f1.richTextBox1.Text;

                int len = s1.Length;
                buffer.Append(s3);  //버퍼에 초기 문자들을 다 저장하고 그위에서 삭제 삽입하자.
                buffer.Remove(index, len);   //index에 찾는 단어가 있고 len은 그 찾는 단어의 길이.
                buffer.Insert(index, s2);
                f1.richTextBox1.Text = buffer.ToString();
                index = f1.richTextBox1.Text.IndexOf(textBox1.Text, index);

                if (index == -1)
                {
                    MessageBox.Show("문서의 끝까지 바꾸기 수행함");

                }
                else
                {
                    int len1 = textBox1.TextLength;   //다음 바꾸기할 단어를 위해 찾는 단어의 길이를 저장
                    f1.richTextBox1.Focus();   //초판에 초점
                    f1.richTextBox1.Select(index, len1); //초판에 index부터 찾는 단어 길이만큼 선택

                }
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if (findflag == false)
            {
                if (f1.richTextBox1.Text.IndexOf(textBox1.Text, 0) != -1)
                {
                    index = f1.richTextBox1.Text.IndexOf(textBox1.Text, 0);
                    int len = textBox1.TextLength;
                    f1.richTextBox1.Focus();
                    f1.richTextBox1.Select(index, len);
                    findflag = true;

                }
                else
                {
                    MessageBox.Show("찾을 단어가 없음");

                }
            }
            else
            {
                index = f1.richTextBox1.Text.IndexOf(textBox1.Text, index + 1);
                if (index == -1)
                {
                    MessageBox.Show("끝까지 검색됨");
                    findflag = false;

                }
                else
                {
                    int len = textBox1.TextLength;
                    f1.richTextBox1.Focus();
                    f1.richTextBox1.Select(index, len);
                }
            } 
            
        }   

        private void button3_Click(object sender, EventArgs e)
        {
           if(textBox1.Text !="" && textBox2.Text != "")
            {
                string s1 = textBox1.Text;
                string s2 = textBox2.Text;
                string s3 = f1.richTextBox1.Text;
                StringBuilder buffer = new StringBuilder();
                buffer.Append(s3);
                buffer.Replace(s1, s2);
                f1.richTextBox1.Text = buffer.ToString();
                
            }
        }
    }
}

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

직렬화,역직렬화  (0) 2020.12.05
C# OpenFileDialog 사용하기  (0) 2020.12.04
C# 파일과 디렉토리  (0) 2020.12.03
4.델리게이트,이벤트,애트리뷰트  (0) 2020.09.18