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

namespace WindowsApplication1
{
    public partial class Form8 : Form
    {
        string filepath = null;        
        //文件路径，为空时，点保存调用另存为方法，其值在打开文件和保存时改变
        bool isChanged = false;       
        //文本框是否改变
        public string findKey = null;  
        //为空时代表没有被查找过，点击查找下一个时，调用查找的方法，在查找时其值改变
        public bool isToDown = true;            
        //查找方向，true代表向下
        public bool mathingStyle = false;         
        //区分大小写，true为区分

        public Form8()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 新建文本文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem6_Click(object sender, EventArgs e)
        {
            if (this.isChanged == true)
            {
                DialogResult result = MessageBox.Show("文本已更改，是否保存", "提示", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    this.toolStripMenuItem8.PerformClick();
                }
            }
            this.textBox1.Text = "";
            this.isChanged = false;
            this.filepath = null;
        }
        /// <summary>
        /// 保存文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem8_Click(object sender, EventArgs e)
        {
            if (this.filepath == null)
            {
                this.toolStripMenuItem9.PerformClick();
            }
            else
            {
                StreamWriter sw = new StreamWriter(filepath, false);
                sw.Write(this.textBox1.Text);
                sw.Flush();
                sw.Close();
                this.isChanged = false;
            }
        }
        /// <summary>
        /// 另存为程序代码部分
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem9_Click(object sender, EventArgs e)
        {
            DialogResult result = saveFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                this.filepath = this.saveFileDialog1.FileName;
                StreamWriter sw = new StreamWriter(filepath, false);
                sw.Write(this.textBox1.Text);
                sw.Flush();
                sw.Close();
            }
        }
        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem10_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        /// <summary>
        /// 撤销
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem11_Click(object sender, EventArgs e)
        {
            this.textBox1.Undo();
        }
        /// <summary>
        /// 剪切
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem12_Click(object sender, EventArgs e)
        {
            this.textBox1.Cut();
        }
        /// <summary>
        /// 复制
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem13_Click(object sender, EventArgs e)
        {
            this.textBox1.Copy();
        }
        /// <summary>
        /// 粘贴
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem14_Click(object sender, EventArgs e)
        {
            this.textBox1.Paste();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem15_Click(object sender, EventArgs e)
        {
            this.textBox1.SelectedText = "";
        }
        /// <summary>
        /// 全选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem20_Click(object sender, EventArgs e)
        {
            this.textBox1.SelectAll();
        }
        /// <summary>
        /// 添加时间
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem21_Click(object sender, EventArgs e)
        {
            this.textBox1.AppendText(DateTime.Now.ToString());
        }
        /// <summary>
        /// 查找
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem16_Click(object sender, EventArgs e)
        {
            Form9 objFrmFind = new Form9();
            objFrmFind.mainForm = this;
            //将本身对象的指针赋给frmFind的mainForm
            objFrmFind.Show();
        }
        /// <summary>
        /// 查找下一个
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem17_Click(object sender, EventArgs e)
        {
            if (findKey == null)
            {
                toolStripMenuItem16.PerformClick();
            }
            else
            {
                string source;
                string finding;
                if (mathingStyle == false)
                {
                    source = textBox1.Text.ToLower();
                    finding = findKey.ToLower();
                }
                else
                {
                    source = textBox1.Text;
                    finding = findKey;
                }

                if (isToDown)
                {
                    int temp = source.IndexOf(finding, textBox1.SelectionStart + textBox1.SelectedText.Length);
                    if (temp >= 0)
                    {
                        textBox1.Select(temp, finding.Length);
                        textBox1.ScrollToCaret();
                    }
                    else
                    {
                        MessageBox.Show("没有找到" + "'" + findKey + "'");
                    }
                }
                else
                {
                    int temp = -1;
                    try
                    {
                        temp = source.LastIndexOf(finding, textBox1.SelectionStart - 1);
                    }
                    catch { }
                    if (temp >= 0)
                    {
                        textBox1.Select(temp, finding.Length);
                        textBox1.ScrollToCaret();
                    }
                    else
                    {
                        MessageBox.Show("没有找到" + "'" + findKey + "'");
                    }
                }
            }
        }
        /// <summary>
        /// 打开文本文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem7_Click(object sender, EventArgs e)
        {
           DialogResult result = this.openFileDialog1.ShowDialog();
           if (result == DialogResult.OK)
           {
               this.filepath = this.openFileDialog1.FileName;
               StreamReader sr = new StreamReader(filepath, Encoding.Default);
               this.textBox1.Text = sr.ReadToEnd();
               sr.Close();
               this.isChanged = false;
           }
        }
        /// <summary>
        /// 自动换行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem22_Click(object sender, EventArgs e)
        {
            if (this.toolStripMenuItem7.Checked == true)
            {
                this.toolStripMenuItem7.Checked = false;
                this.textBox1.WordWrap = false;
            }
            else
            {
                this.toolStripMenuItem7.Checked = true;
                this.textBox1.WordWrap = true;
            }
        }
        /// <summary>
        /// 字体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem24_Click(object sender, EventArgs e)
        {
            DialogResult result = fontDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                this.textBox1.Font = fontDialog1.Font;
            }
        }
        /// <summary>
        /// 颜色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem25_Click(object sender, EventArgs e)
        {
            DialogResult result = colorDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                this.textBox1.ForeColor = colorDialog1.Color;
            }
        }

        private void toolStripMenuItem18_Click(object sender, EventArgs e)
        {
            Form10 objFrmFind = new Form10();
            objFrmFind.mainForm = this;
            //将本身对象的指针赋给frmFind的mainForm
            objFrmFind.Show();
        }
    }
}