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 Form9 : Form
    {
        public Form8 mainForm;
        public Form9()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 点击查询下一个后
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFindNext_Click(object sender, EventArgs e)
        {
            mainForm.findKey = txtContent.Text;
            mainForm.isToDown = radToDown.Checked;
            mainForm.mathingStyle = checkBox1.Checked;
            string source;   //被查找的文本
            string finding;  //查找的关键字

            //当不区分大小写时，将内容都转换为小写
            if (this.checkBox1.Checked == false)
            {
                source = this.mainForm.textBox1.Text.ToLower();
                finding = this.txtContent.Text.ToLower();
            }
            else
            {
                source = this.mainForm.textBox1.Text;
                finding = this.txtContent.Text;
            }

            //向下查找
            if (this.radToDown.Checked)
            {
                //SelectionStart能获取光标起始点
                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("没有找到" + "'" + txtContent.Text + "'");
                }
            }
            //向上查找
            else
            {
                int temp = -1;
                try
                {
                    temp = source.LastIndexOf(finding, mainForm.textBox1.SelectionStart - 1);
                }
                catch { }
                if (temp >= 0)
                {
                    this.mainForm.textBox1.Select(temp, finding.Length);
                    mainForm.textBox1.ScrollToCaret();//当屏幕显示不了时，实现滚动
                    this.mainForm.Focus();
                }
                else
                {
                    MessageBox.Show("没有找到" + "'" + txtContent.Text + "'");
                }
            }
        }
        /// <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 txtContent_TextChanged(object sender, EventArgs e)
        {
            if (txtContent.Text.Length > 0)
            {
                btnFindNext.Enabled = true;
            }
            else
            {
                btnFindNext.Enabled = false;
            }
        }
        /// <summary>
        /// 表单初始化事件 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form9_Load(object sender, EventArgs e)
        {
            txtContent.Text = mainForm.textBox1.SelectedText;
        }

    }
}