我有一个列表框,我可以用按钮删除项目,但我也想用键盘上的删除键删除,我在谷歌上找不到方法,所以有人可以帮我
编辑 它的一个winform应用程序
这是删除按钮的代码:
private void Button3Click(object sender, EventArgs e)
{
var application = this.GetCurrentApplication();
if (application == null)
{
MessageBox.Show("No Application selected");
return;
}
if (MessageBox.Show("You are about to delete application: " + Environment.NewLine + _applicationListBox.SelectedItem + Environment.NewLine + "Are you sure you want to delete the application?", "", MessageBoxButtons.YesNo) == DialogResult.No)
{
MessageBox.Show("The application will not be deleted.", "", MessageBoxButtons.OK);
}
else if (this._applicationListBox.SelectedIndex >= 0)
{
int index = _applicationListBox.SelectedIndex;
_toepassingIniFile.ToePassingen.Remove(application);
if (index == _toepassingIniFile.ToePassingen.Count)
--index;
application = index < 0 ? null : _toepassingIniFile.ToePassingen[index];
_toepassingIniFile.Save(application);
_applicationListBox.DataSource = null;
_applicationListBox.DataSource = _toepassingIniFile.ToePassingen;
_applicationListBox.SelectedIndex = index;
}
}
回答感谢琼斯
private void ApplicationListBoxPreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode ==Keys.Delete )
{
deletefromlistbox();
}
if (e.KeyCode == Keys.Insert)
{
Refreshapplication();
}
}
刷新应用程序
private void Refreshapplication()
{
var newapplication = new NewApplication(_toepassingIniFile);
if (newapplication.Run())
{
_applicationListBox.DataSource = null;
_applicationListBox.DataSource = _toepassingIniFile.ToePassingen;
_applicationListBox.SelectedIndex = _toepassingIniFile.ToePassingen.Count - 1;
_controllercombobox.DataSource = null;
_controllercombobox.DataSource = _controllerIniFile.Controllers;
}
}