我正在尝试实施自定义事件,但由于某些原因,我无法退订事件
我正在使用带组合框单元格的datagridview。我还在该组合框上创建了一个自定义事件来处理我的数据。如果在新的特定列上按键盘上的空格键,将出现一个搜索表单。问题是,如果我第一次按该搜索表单,则会出现一次。如果我在不同的行但相同的列上第二次再次按它,则搜索表单将出现两次。如果我再按一次,它将显示3倍,并且每次按空格时,它将增加越来越多的内容。我的退订事件似乎由于某种原因而无法正常运行。我尝试将文本框用作列,但存在相同的问题。您能指出我做错了什么吗?是我的退订代码错误还是datagridview editcontrol上显示某些我不知道的东西。
private void dtgClientInfo_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
var senderGrid = (DataGridView)sender;
//declare my custom event
KeyPressEventHandler mykeypress = delegate (object sender1, KeyPressEventArgs e1)
{
ShowSearchbox_KeyPress(sender1, e1, senderGrid.CurrentCell);
};
//e.Control.KeyPress -= new KeyPressEventHandler(mykeypress); -- this code is not working
e.Control.KeyPress -= mykeypress; // for some reason its not working
if (senderGrid.CurrentCell.ColumnIndex == senderGrid.Columns["mainmemberid"].Index )
{
ComboBox searchBox = e.Control as ComboBox;
if (searchBox != null)
{
searchBox.DropDownStyle = ComboBoxStyle.DropDown;
searchBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
// searchBox.KeyPress -= mykeypress; // unsubscribing to event before subscribing again but its not working
searchBox.KeyPress += mykeypress; // subscribe to event
}
}
}
//custom event
private void ShowSearchbox_KeyPress(object sender, KeyPressEventArgs e, DataGridViewCell cell)
{
//custom keypress to determine who calls and returns
if (e.KeyChar == (char)Keys.Space) //when spacebar is pressed
{
//when spacebar is press then this event will occur
ClientSearchFrm search = new ClientSearchFrm();
//instatiate form to search
search.ShowDialog();
DataRow SearchedRow = search.getMemberInfo();
if (SearchedRow == null)
{
cell.DataGridView.Rows[cell.RowIndex].Cells[cell.ColumnIndex].Value = null;
}
else
{
cell.DataGridView.Rows[cell.RowIndex].Cells[cell.ColumnIndex].Value = SearchedRow["memberid"].ToString();
}
cell.DataGridView.EndEdit();
search.Dispose();
e.Handled = true;
}
}
即使我在另一行上多次按空格键,输出也只能显示一次