Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. delete rows in listView using KeyPressed event and Delete Key

delete rows in listView using KeyPressed event and Delete Key

Scheduled Pinned Locked Moved C#
questioncom
7 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    al3xutzu00
    wrote on last edited by
    #1

    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.”

    C M 2 Replies Last reply
    0
    • A al3xutzu00

      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.”

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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.

      A 1 Reply Last reply
      0
      • C Christian Graus

        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.

        A Offline
        A Offline
        al3xutzu00
        wrote on last edited by
        #3

        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.”

        C N 2 Replies Last reply
        0
        • A al3xutzu00

          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.”

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          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.

          N 1 Reply Last reply
          0
          • C Christian Graus

            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.

            N Offline
            N Offline
            Nagy Vilmos
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • A al3xutzu00

              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.”

              N Offline
              N Offline
              Nagy Vilmos
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • A al3xutzu00

                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.”

                M Offline
                M Offline
                MatheusMK3
                wrote on last edited by
                #7

                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"); }

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups