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

DKT

So this is a really stupid problem, When I load up the same CSV file multiple times into my program this results in a messagebox appearing multiple times (n). The times I've opened a file. I'll post some code here to hopefully see why this is happening.

   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);
             
           }
           

       }

So I have a button1, which opens a file explorer, from which I choose a .csv file. When I load in multiple files in a row I get the MessageBox from clicking a textbox the amount of times I've loaded in a file. I can't find out why this is happening, so I hope one of you has had this issue before or can see in my code why it is happening.

Kind regards, DKT

Klaus Gütter

With each button1_Click you add another event handler to the Click event of the text boxes. So when button1_Click is executed twice, each click on the text box will invoke textbox_Click twice etc.

The solution is to remember if you already added the event handlers and skip this step the second time.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Message box appears multiple times

Bulk Insert the same row multiple (N) times

Excel VBA Worksheet Change Event Causes Message Box to Appear Multiple Times

Copy each row n times

My rspec test does not pass, I expect my method to receive message expectation n times, but it keeps getting n+1 times

Accidentally loading angularjs multiple times causes strange behavior

How to count the amount of times an object in a list appears N times in a row?

How to capture repeated group up to N times?

How to combine parsers up to n times in Haskell?

Make INDEX return a row n times

Replicate Spark Row N-times

repeat the row n times with a separator in-line

Repeat each row N times in Google Sheets

How to copy a row n times in sqlite?

Reading file stream multiple times causes "stream not readable" error

How to tryCatch the same function call multiple times (N times) in R

Duplicate each number in a row n times, where n is the number in the row

Loading the same image multiple times

Create tuple of multiple items n Times in Python

How to request multiple inputs, n number of times

Why Does my Message Box Show Up in Another Language?

write a file multiple times

Saving a file multiple times

copy file n times and rename it with specific names

Linux command to concatenate a file to itself n times

Duplicate/Copy same file N times

ActiveMQ processing message multiple times

Copy and paste the row a N number of times (N based on a value in the cell)

pandas: for each row in df copy row N times with slight changes