我有一个textbox.Leave事件处理程序,现在我无法跳出文本框(我仍然可以点击)。
这是事件的添加方式:
[...来自表格......]
EKFG_Sig.Config.AutocompleteManager am = new EKFG_Sig.Config.AutocompleteManager(this.GetType().FullName);
foreach (Control c in this.Controls) {
if (c is TextBox) {
TextBox tb = (TextBox)c;
am.AddAutocomplete(tb);
}
}
[...来自autocompleteManager ...]
public void AddAutocomplete(TextBox control) {
AutoCompleteStringCollection collection = new AutoCompleteStringCollection();
DataTable dt = new DataTable("AutoComplete");
dt.Columns.Add("Name");
dt.Columns.Add("Values");
try {
dt.ReadXml(getAutocompleteFile());
} catch (Exception) {
initialize();
}
DataRow[] values = dt.Select("Name = '" + Name + "." + control.Name + "'");
foreach (DataRow row in values) {
collection.Add(row[1].ToString());
}
control.AutoCompleteMode = AutoCompleteMode.Suggest;
control.AutoCompleteSource = AutoCompleteSource.CustomSource;
control.AutoCompleteCustomSource = collection;
control.Leave += Autocomplete_Leave;
}
private void Autocomplete_Leave(object sender, EventArgs args){
((TextBox)sender).AutoCompleteCustomSource.Add(((TextBox)sender).Text);
writeAutocomplete(Name + "." + ((TextBox)sender).Name, ((TextBox)sender).Text);
}
private static void writeAutocomplete(String name, String addition) {
DataTable dt = new DataTable("AutoComplete");
dt.Columns.Add("Name");
dt.Columns.Add("Values");
try {
dt.ReadXml(getAutocompleteFile());
} catch (Exception) {
initialize();
}
DataRow[] values = dt.Select("Name = '" + name + "'");
foreach (DataRow row in values) {
if (row[1].ToString() == addition) {
return;
}
}
dt.Rows.Add(new object[] { name, addition });
try {
dt.WriteXml(getAutocompleteFile());
} catch (IOException) {
System.Threading.Thread.Sleep(500);
try {
dt.WriteXml(getAutocompleteFile());
} catch (IOException) {
//failed to write. ignore
}
}
}
知道是什么导致了这个问题,我该如何解决?
如果我发表评论((TextBox)sender).AutoCompleteCustomSource.Add(((TextBox)sender).Text);
,那就有效了。知道为什么吗?我怎样才能保持这条线,它仍然有效?
答案 0 :(得分:0)
注意代码,唯一可疑的是Thread.Sleep。是不是每次都能碰到它,从而使它看起来不会离开?
如果您尚未完成其中任何一项,还有其他一些指示...... 考虑取出代码并逐行/逐块添加它,直到看到意外行为。这至少会让你缩小范围。
对于导致悲伤的文本框的Leave事件是否附加了其他内容?
也许在Enter事件上设置一个事件,以查看是否强行获得焦点,而不是焦点从不离开。