C3204
2025-12-18 d85cbf0ccd61d95b96695756e0c90db8b7679545
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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<string> banStr = new List<string>() { ",", ".", "\\", "/", ":", "*", "?", "\"", "<", ">", "|", "\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();
 
        }
    }
}