所以這是一個非常愚蠢的問題,當我將同一個 CSV 檔案多次加載到我的程式中時,這會導致訊息框出現多次 (n)。我打開檔案的次數。我會在這里發布一些代碼,希望能看到為什么會發生這種情況。
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);
}
}
所以我有一個 button1,它打開一個檔案資源管理器,我從中選擇一個 .csv 檔案。當我連續加載多個檔案時,我通過單擊文本框獲取我在檔案中加載的次數的 MessageBox。我不知道為什么會發生這種情況,所以我希望你們中的一個人以前遇到過這個問題,或者可以在我的代碼中看到它為什么會發生。
親切的問候, DKT
uj5u.com熱心網友回復:
對于每個 button1_Click,您可以向文本框的 Click 事件添加另一個事件處理程式。所以當button1_Click執行兩次時,每次點擊文本框都會呼叫textbox_Click兩次等等。
解決方案是記住您是否已經添加了事件處理程式并第二次跳過此步驟。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/391064.html
上一篇:無法登錄網站Selenium
