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. Selecting an item in a listbox control

Selecting an item in a listbox control

Scheduled Pinned Locked Moved C#
question
3 Posts 2 Posters 0 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.
  • M Offline
    M Offline
    Muammar
    wrote on last edited by
    #1

    Hi, I'm trying to select an item in a listbox control by right clicking on it, can you please tell me what's wrong with what I'm doing??

    private void listbox1_MouseClick(object sender, MouseEventArgs e)
    {
    listbox1.SelectedItem = listbox1.GetChildAtPoint(listbox1.PointToClient(e.Location));
    }
    

    Thank you guys!


    Smile: A curve that can set a lot of things straight! (\ /) (O.o) (><)

    H 1 Reply Last reply
    0
    • M Muammar

      Hi, I'm trying to select an item in a listbox control by right clicking on it, can you please tell me what's wrong with what I'm doing??

      private void listbox1_MouseClick(object sender, MouseEventArgs e)
      {
      listbox1.SelectedItem = listbox1.GetChildAtPoint(listbox1.PointToClient(e.Location));
      }
      

      Thank you guys!


      Smile: A curve that can set a lot of things straight! (\ /) (O.o) (><)

      H Offline
      H Offline
      Hessam Jalali
      wrote on last edited by
      #2

      Hi I suggest to use MouseDown or MouseUp events instead of Click because they give you the mouse button and position all together and you can find the index of pointed item by using IndexFromPoint method of listBox instance here is the code I tested to see how it can be done

          void listBox1\_MouseDown(object sender, MouseEventArgs e)
          {
              if (e.Button != MouseButtons.Right) return;
      
              int index=this.listBox1.IndexFromPoint(e.Location);
      
              if (index == -1) return;
      
              object selectedObj = this.listBox1.Items\[index\];
      
              // if you want to select that in the list
      
              this.listBox1.SelectedIndex = index;
      
              MessageBox.Show(selectedObj.ToString());//just for test
          }
      

      good luck :)

      M 1 Reply Last reply
      0
      • H Hessam Jalali

        Hi I suggest to use MouseDown or MouseUp events instead of Click because they give you the mouse button and position all together and you can find the index of pointed item by using IndexFromPoint method of listBox instance here is the code I tested to see how it can be done

            void listBox1\_MouseDown(object sender, MouseEventArgs e)
            {
                if (e.Button != MouseButtons.Right) return;
        
                int index=this.listBox1.IndexFromPoint(e.Location);
        
                if (index == -1) return;
        
                object selectedObj = this.listBox1.Items\[index\];
        
                // if you want to select that in the list
        
                this.listBox1.SelectedIndex = index;
        
                MessageBox.Show(selectedObj.ToString());//just for test
            }
        

        good luck :)

        M Offline
        M Offline
        Muammar
        wrote on last edited by
        #3

        GREATE:-D

        listbox1.SelectedIndex = listbox1.IndexFromPoint(e.Location);
        

        Wish I can give you 6:cool: Thanks a million Hessam!


        Smile: A curve that can set a lot of things straight! (\ /) (O.o) (><)

        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