delete rows in listView using KeyPressed event and Delete Key
-
Hi Guys, I have been trying to implement the KeyPress event on a listView so that when i use the DELETE key, i will delete the selected rows (MultiSelect=true) The code works fine when i use a button to call it from :
private void btnDeleteRow\_Click(object sender, EventArgs e) { while (listView1.SelectedIndices.Count > 0) { listView1.Items.RemoveAt(listView1.SelectedIndices\[0\]); } }
However in MSDN documentation it is explicitly said that e.KeyChar can't be used for DELETE and several other special function keys like F1-F12 , PageUp etc... http://msdn.microsoft.com/en-us/library/system.windows.forms.keypresseventargs.keychar.aspx[^] How can i use the DELETE KEY to delete items in listViews? Kind regards, Alex:confused:
“Be the change you want to see in the world.”
-
Hi Guys, I have been trying to implement the KeyPress event on a listView so that when i use the DELETE key, i will delete the selected rows (MultiSelect=true) The code works fine when i use a button to call it from :
private void btnDeleteRow\_Click(object sender, EventArgs e) { while (listView1.SelectedIndices.Count > 0) { listView1.Items.RemoveAt(listView1.SelectedIndices\[0\]); } }
However in MSDN documentation it is explicitly said that e.KeyChar can't be used for DELETE and several other special function keys like F1-F12 , PageUp etc... http://msdn.microsoft.com/en-us/library/system.windows.forms.keypresseventargs.keychar.aspx[^] How can i use the DELETE KEY to delete items in listViews? Kind regards, Alex:confused:
“Be the change you want to see in the world.”
Perhaps the key up or key down event gets more eventargs ?
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
-
Perhaps the key up or key down event gets more eventargs ?
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
Thank you Christian, it works perfect with KeyDown and e.KeyCode. Here is the implementation :
private void listView1\_KeyDown(object sender, KeyEventArgs e) { if (listView1.SelectedItems.Count != 0 && e.KeyCode == Keys.Delete) { while (listView1.SelectedIndices.Count > 0) { listView1.Items.RemoveAt(listView1.SelectedIndices\[0\]); } } }
“Be the change you want to see in the world.”
-
Thank you Christian, it works perfect with KeyDown and e.KeyCode. Here is the implementation :
private void listView1\_KeyDown(object sender, KeyEventArgs e) { if (listView1.SelectedItems.Count != 0 && e.KeyCode == Keys.Delete) { while (listView1.SelectedIndices.Count > 0) { listView1.Items.RemoveAt(listView1.SelectedIndices\[0\]); } } }
“Be the change you want to see in the world.”
I don't know why keypressed doesn't get the same eventargs, but I had a feeling that was the case. There may be a systemkeypress event or similar, but I find keydown or keyup easier to deal with.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
-
I don't know why keypressed doesn't get the same eventargs, but I had a feeling that was the case. There may be a systemkeypress event or similar, but I find keydown or keyup easier to deal with.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
For some reason[1], the normal control and navigation keys[2] are not exposed except in key down/up events. It's been this way as long as I can remember[3] and I can't see it changing soon. That would be sensible[4]. [1] I think Bill was drunk that day. [2] The ones you are most likely to want to override. [3] Ten years even after lunch. [4] Should you actually want to write code.
Panic, Chaos, Destruction. My work here is done.
-
Thank you Christian, it works perfect with KeyDown and e.KeyCode. Here is the implementation :
private void listView1\_KeyDown(object sender, KeyEventArgs e) { if (listView1.SelectedItems.Count != 0 && e.KeyCode == Keys.Delete) { while (listView1.SelectedIndices.Count > 0) { listView1.Items.RemoveAt(listView1.SelectedIndices\[0\]); } } }
“Be the change you want to see in the world.”
You should take care with this, it's not a good idea to make the change on the key down. I would recommend setting a flag when the button is pressed and checking for the flag when it's released, that is closer to the key pess event.
Panic, Chaos, Destruction. My work here is done.
-
Hi Guys, I have been trying to implement the KeyPress event on a listView so that when i use the DELETE key, i will delete the selected rows (MultiSelect=true) The code works fine when i use a button to call it from :
private void btnDeleteRow\_Click(object sender, EventArgs e) { while (listView1.SelectedIndices.Count > 0) { listView1.Items.RemoveAt(listView1.SelectedIndices\[0\]); } }
However in MSDN documentation it is explicitly said that e.KeyChar can't be used for DELETE and several other special function keys like F1-F12 , PageUp etc... http://msdn.microsoft.com/en-us/library/system.windows.forms.keypresseventargs.keychar.aspx[^] How can i use the DELETE KEY to delete items in listViews? Kind regards, Alex:confused:
“Be the change you want to see in the world.”
I had the same problem and i used this code:
private void listView1\_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Delete) { listView1.Items.RemoveAt(listView1.SelectedIndices\[0\]); } }
I hope this help you...
public void showSignature(object sender, MyForumSignatureEventArgs e) { signatureLabel.Text = e.Signature("MatheusMK3", "codeproject.com", "Forum"); }