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
CodePudding user response:
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.