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 VisionControl.Forms { public partial class RenameForm : Form { // 定义一个委托和事件 public delegate void UpdateStringHandler(string oriString, string newString); public event UpdateStringHandler UpdateStringEvent; public bool bRename = false; public string strOriName { get; set; } public string strNewName { get; set; } public bool bCheckBan = true; public RenameForm(string strOrigin = "", bool bCheckBan = true) { InitializeComponent(); // 禁止修改窗口大小 this.FormBorderStyle = FormBorderStyle.FixedDialog; this.txt_OriginalName.Text = strOrigin; this.txt_NewName.Text = strOrigin; this.bCheckBan = bCheckBan; } private void btn_Cancel_Click(object sender, EventArgs e) { this.Close(); } List banStr = new List() { ",", ".", "\\", "/", ":", "*", "?", "\"", "<", ">", "|", "\0" }; private void btn_Rename_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(this.txt_NewName.Text.Trim())) { MessageBox.Show("名称不允许为空", "异常"); return; } if (bCheckBan) { foreach (string str in banStr) { if (this.txt_NewName.Text.Contains(str)) { MessageBox.Show("名称不允许出现, . \\ / : * ? \" < > | 等字符", "异常"); return; } } } // 触发事件,通知父窗体更新string UpdateStringEvent?.Invoke(this.txt_OriginalName.Text, this.txt_NewName.Text); bRename = true; strOriName = this.txt_OriginalName.Text; strNewName = this.txt_NewName.Text; this.Close(); } } }