区分“用户检查”和“编程方式检查”
本文关键字:检查 编程 方式 用户检查 用户 区分 | 更新日期: 2023-09-27 18:33:04
我有一个检查列表,当程序加载时,我需要将字符串和布尔值的列表加载到清单框中。但是在设置布尔值的同时
checkedListBoxControl1.SetItemChecked(i, checkedList[i]);;
checkedListBoxControl1_ItemCheck
事件触发。我不希望这样,因为当它触发时,它会刷新我的数据库并且需要很长时间才能完成。我只希望在用户更改检查列表检查状态时触发它。
注意:我有
目前我正在使用A标志来做到这一点,它很丑陋,在这里给我带来了很多其他问题
private void checkedListBoxControl1_ItemCheck(object sender, DevExpress.XtraEditors.Controls.ItemCheckEventArgs e) //fires second on check
{
int index = e.Index;
bool isChecked = e.State == CheckState.Checked;
this.mediaCenter.ItemManager.SetDirectoryCheck(index, isChecked);
if (this.IsUserClick)
BuildDatabaseAsync();
this.IsUserClick = false;
}
private bool IsUserClick;
private void checkedListBoxControl1_Click(object sender, EventArgs e) //Fires first on check
{
if (checkedListBoxControl1.SelectedItem == null) return;
IsUserClick = true;
}
可能是我填充列表框控件的方法首先很奇怪。但是由于沿途有很多不必要的变化。我这样做如下
private void BuildCheckListControl(string[] dirs)
{
IsUserClick = false;
this.checkedListBoxControl1.DataSource = dirs;
for (int i = 0; i < dirs.Length; i++)
checkedListBoxControl1.SetItemChecked(i, checkedList[i]);
}
checkedList[]
包含对应于 dirs 数组的布尔值数组
您可以在初始化期间分配一个布尔变量(类成员而不是局部变量)。在 ItemCheck 事件中,检查 bool 变量并决定继续数据库检查。初始化完成后,将 bool 变量设置为 true。
如果您不想创建布尔值,则会检查 如果您更改BuildCheckListControl
,您可以(如注释中所述)删除/添加事件处理程序 -方法如下:
private void BuildCheckListControl(string[] dirs)
{
checkedListBoxControl1.ItemCheck -= checkedListBoxControl1_ItemCheck; //Will remove your Eventhandler
//IsUserClick = false; //You shouldn't need that anymore.
this.checkedListBoxControl1.DataSource = dirs;
for (int i = 0; i < dirs.Length; i++)
checkedListBoxControl1.SetItemChecked(i, checkedList[i]);
checkedListBoxControl1.ItemCheck += checkedListBoxControl1_ItemCheck; //Will add your Eventhandler again
}