连续多次 (n) 加载文件导致我的消息框消息显示 (n) 次

Loading in a file multiple times (n) in a row causes my Message Box messages to show up (n) times

所以这是一个非常愚蠢的问题,当我将同一个 CSV 文件多次加载到我的程序中时,这会导致消息框出现多次 (n)。我打开文件的次数。我将 post 一些代码在这里希望看到为什么会这样。

   public partial class Form1 : Form
   {
       [DllImport("shlwapi.dll")]
       public static extern int ColorHLSToRGB(int H, int L, int S);
       public
           bool First = true;
       
       public Form1()
       {
           InitializeComponent();

       }
public void button1_Click(object sender, EventArgs e) #button1 is used to start OpenFileDialog
       {
 OpenFileDialog openFileDialog1 = new OpenFileDialog
           {
               InitialDirectory = @"D:\",
               Title = "Browse Text Files",

               CheckFileExists = true,
               CheckPathExists = true,

               DefaultExt = "csv",
               Filter = "csv files (*.csv)|*.csv",
               FilterIndex = 2,
               RestoreDirectory = true,

               ReadOnlyChecked = true,
               ShowReadOnly = true
           };

           if (openFileDialog1.ShowDialog() == DialogResult.OK)
           {
               textBox1.Text = openFileDialog1.FileName;
               PickCount.Clear();
               PickCountProduct.Clear();
               PickCountSchap.Clear();
               LocatieProduct.Clear();
           }


              
               foreach (Control c in Controls) #I have ~400 clickable textboxes which display extra information when clicked. Therefore I store them
               {
                   if (c is TextBox)
                   {
                       textBoxes.Add(c);
                   }
                }

                   foreach (var c in textBoxes)
                   { c.Click += textbox_Click;
}
}
public void textbox_Click(object sender, EventArgs e)
       {
           string location = ((TextBox)sender).Text;
           int pickcount = 0;
           string product = "";
           bool Test3 = PickCountSchap.TryGetValue(location, out int test);
           if (Test3)
           {
               pickcount = test;
           }
           else pickcount = 0;
           bool Test2 = LocatieProduct.TryGetValue(location, out HashSet<string> test2);
           if (Test2)
           {
               foreach (var output in test2)
               {

                   int i = output.IndexOf(" ") + 1;
                   string str = output.Substring(i);
                 //  Console.WriteLine(str);
                   bool Test4 = PickCountProduct.TryGetValue(str, out int test3);
                   if (Test4 == false)
                   {
                    product = product + "\n" + output + " PickCount: onbekend";
                       
                   }
               
                   else product = product + "\n" + output + " PickCount: " + PickCountProduct[str];
               }
           }
           else product = "Geen informatie over producten op deze locatie gevonden";
           if (First)
           {
               MessageBox.Show("Informatie over schap " + location + "\nPickCount: " + pickcount + "\nProducten op deze locatie: " + product);
             
           }
           

       }

所以我有一个按钮 1,它打开一个文件资源管理器,我从中选择一个 .csv 文件。当我连续加载多个文件时,我通过单击文本框获得 MessageBox,这是我在文件中加载的次数。我不知道为什么会这样,所以我希望你们中的一个人以前遇到过这个问题,或者可以在我的代码中看到为什么会这样。

亲切的问候, DKT

对于每个 button1_Click,您都将另一个事件处理程序添加到文本框的 Click 事件中。因此,当 button1_Click 被执行两次时,每次单击文本框都会调用 textbox_Click 两次,依此类推

解决方案是记住您是否已经添加了事件处理程序并在第二次跳过此步骤。