我有一個事件設置爲在ListView中的項目被選中時觸發。該事件調用一個函數來更新我的表單中的各種控件。除此之外,我需要根據檢查的項目數來啓用或禁用按鈕。這個功能非常昂貴。完成多個事件後的運行代碼
例子:
private void listView_ItemCheck(object sender, ItemCheckEventArgs e)
{
UpdateForm();
}
現在,當用戶想要在一次檢查多個項目的問題出現了。這會導致應用程序在一段時間內沒有響應。
所以,我想打電話UpdateForm()
畢竟檢查項目,而不是每次檢查一個項目。
編輯:
這裏是UpdateForm()
部分:
private void UpdateForm()
{
// Puts all files in the mod list in a new list
List<string> modListFiles = new List<string>(lvFiles.Items.Count);
foreach (ListViewItem lvi in lvFiles.Items)
{
modListFiles.Add(lvi.Text);
}
// Adds found files to the file list
foreach (string file in Files)
{
lvFiles.Items.Add(new ListViewItem(file));
}
// Removes files from mod list that no longer exist
List<string> deleteQue = new List<string>(lvFiles.Items.Count);
foreach (string file in modListFiles)
{
// If a file in the list doesn't exist anymore, que it to delete
if (!Files.Contains(file))
{
deleteQue.Add(file);
}
}
// Remove queued files
foreach (string file in deleteQue)
{
foreach (ListViewItem lvi in lvFiles.Items)
{
if (lvi.Text == file)
{
lvFiles.Items.Remove(lvi);
break;
}
}
}
// Grays out mod list if a profile is installed
if (InstalledProfile == null)
{
lvFiles.BackColor = SystemColors.Window;
lvFiles.ForeColor = SystemColors.WindowText;
}
else
{
lvFiles.BackColor = SystemColors.Control;
lvFiles.ForeColor = SystemColors.ControlDark;
}
// Fills out the game path if it exists
if (Directory.Exists(GamePath))
{
txtGamePath.Text = GamePath;
}
else
{
txtGamePath.Text = "Game directory does not exist!";
}
// Makes sure that the cbxProfiles_SelectedIndexChanged doesn't run UpdateForm() again
handleProfileChanged = false;
// Adds profiles to the combobox
foreach (string profile in Profiles)
{
if (!cbxProfiles.Items.Contains(profile))
{
cbxProfiles.Items.Add(profile);
}
}
// Removes nonexistant profiles from the combobox
foreach (string profile in cbxProfiles.Items)
{
if (!Profiles.Contains(profile))
{
cbxProfiles.Items.Remove(profile);
}
}
if (InstalledProfile == null)
{
btnInstallUninstall.Text = "Install";
}
else
{
btnInstallUninstall.Text = "Uninstall";
}
if (Directory.Exists(GamePath) && lvFiles.CheckedItems.Count > 0)
{
btnInstallUninstall.Enabled = true;
}
else
{
btnInstallUninstall.Enabled = false;
}
}
我已經簡化了一些事情,所以請原諒我可能已經取得的任何錯誤。
一些情況下: 我試圖做一個程序,從設置目錄\ mods複製文件到用戶指定的目錄GamePath
。它顯示\ mods中的所有文件,然後允許用戶檢查其中的一些文件。點擊btnInstall
將把這些文件複製到GamePath
。在所謂的安裝之後,可以通過再次單擊btnInstall
來刪除複製的文件。
我所做的所有屬性(Profiles
,GamePath
)都使用磁盤上的XML文件來獲取和設置它們的值。主ListView被稱爲lvFiles
,有時在註釋中被稱爲mod列表或文件列表。
首先,我建議你考慮使用異步/等待,以防止你的GUI抱死。 –
也許使用「應用」按鈕而不是ItemCheck事件來調用UpdateForm()方法。如果您希望它更像桌面應用程序,您也可以使用「確定」和「取消」按鈕。 – gmlacrosse
聽起來你需要一個單獨的按鈕讓用戶在完成選擇項目時點擊。 –