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

namespace GcDevicePc
{
    public partial class SaveDialog : Form
    {
        public String filename;

        private String checkfolder;
        public SaveDialog()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 带参构造函数
        /// </summary>
        /// <param name="Folder"></param>
        /// <param name="type">0-新建方法 1-方法另存为 2-新建配置</param>
        public SaveDialog(String Folder,int type)
        {
            InitializeComponent();
            this.checkfolder = Folder;
            switch (type)
            {
                case 0:
                    this.Text = "新方法创建";
                    namegroup.Text = "新建方法";
                    break;
                case 1:
                    this.Text = "方法另存为";
                    namegroup.Text = "新建方法";
                    break;
                case 2:
                    this.Text = "新配置创建";
                    namegroup.Text = "新建配置";
                    break;
                default:
                    break;
            }
        }

        bool VerifyName()
        {
            string strtmp;

            DirectoryInfo MethodDir = new DirectoryInfo(this.checkfolder);
            foreach (FileInfo file in MethodDir.GetFiles("*.ini"))
            {
                strtmp = this.savefilename.Text + ".ini";
                if (String.Compare(strtmp, file.Name, true) == 0)
                {
                    return false;
                }
            }
            return true;
        }

        private void savefilename_TextChanged(object sender, EventArgs e)
        {
            if (VerifyName())
            {
                this.nameverify.Text = "文件名可用";
                this.nameverify.ForeColor = Color.Green;
                this.prompt.Text = "";
            }
            else
            {
                this.nameverify.Text = "文件名存在";
                this.nameverify.ForeColor = Color.Red;
                this.prompt.Text = "ps:如文件存在，会打开原有方法而不创建新方法";
            }

            if (this.savefilename.Text == "")
            {
                this.nameverify.Text = "";
            }
        }

        private void okbth_Click(object sender, EventArgs e)
        {
            filename = this.savefilename.Text + ".ini";
        }

        private void cancelbth_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
