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

namespace WindowsApplication1
{
    public partial class Form10 : Form
    {
        public Form8 mainForm;
        public Form10()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// 查找下一个
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFindNext_Click(object sender, EventArgs e)
        {
            string source;
            string finding;
            if (this.checkBox1.Checked == false)
            {
                source = this.mainForm.textBox1.Text.ToLower();
                finding = this.txtFindContent.Text.ToLower();
            }
            else
            {
                source = this.mainForm.textBox1.Text;
                finding = this.txtFindContent.Text;
            }

            int temp = source.IndexOf(finding, mainForm.textBox1.SelectionStart + mainForm.textBox1.SelectedText.Length);
            if (temp >= 0)
            {
                this.mainForm.textBox1.Select(temp, finding.Length);
                mainForm.textBox1.ScrollToCaret();
                this.mainForm.Focus();
            }
            else
            {
                MessageBox.Show("没有找到" + "'" + txtFindContent.Text + "'");
            }
        }
        /// <summary>
        /// 替换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReplace_Click(object sender, EventArgs e)
        {
            if (txtFindContent.Text == mainForm.textBox1.SelectedText)
            {
                mainForm.textBox1.SelectedText = txtReplace.Text;
            }
            else
            {
                btnFindNext.PerformClick();
            }
        }
        /// <summary>
        /// 全部替换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReplaceAll_Click(object sender, EventArgs e)
        {
            int count = 0;
            int position;
            string source;
            string finding;
            while (true)
            {
                //下面为查找代码
                if (this.checkBox1.Checked == false)
                {
                    source = this.mainForm.textBox1.Text.ToLower();
                    finding = this.txtFindContent.Text.ToLower();
                }
                else
                {
                    source = this.mainForm.textBox1.Text;
                    finding = this.txtFindContent.Text;
                }

                if (count == 0)
                {
                    position = source.IndexOf(finding, 0);
                }
                else
                {
                    position = source.IndexOf(finding, mainForm.textBox1.SelectionStart + mainForm.textBox1.SelectedText.Length);
                }

                if (position >= 0)
                {
                    this.mainForm.textBox1.Select(position, finding.Length);
                    mainForm.textBox1.ScrollToCaret();
                    this.mainForm.Focus();
                    count++;
                }


                //下面为替换代码
                if (mainForm.textBox1.SelectedText.Length > 0)
                {
                    btnReplace.PerformClick();
                }
                else
                {
                    break;
                }
            }
            MessageBox.Show("替换了" + count + "个地方");
        }
        /// <summary>
        /// 表单初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form10_Load(object sender, EventArgs e)
        {
            txtFindContent.Text = mainForm.textBox1.SelectedText;
        }
    }
}