ContextMenu for ListBox Item

11 posts, 1 answers
  1. Grant Drury-Green
    Grant Drury-Green avatar
    23 posts
    Member since:
    Jul 2009

    Posted 17 Feb 2010 Link to this post

    I have a list box with some items in it. I have a ContextMenu assigned to the ListBox

    I want to add logic to the "DropDownOpening" event so that the menu only displays when an ITEM is right clicked, and that is contextual for that item.

    I don't seem to be able to work out how to retrieve the item over which the right click was made.

    I've tried listBox.SelectedItem but that doesn't work as the right click isn't moving the selection
    I've also tried listBox.GetChildAtPoint(contextMenu.DropDown.Location) - but that always returns null

    Any help greatly appreciated.

    Grant
  2. Answer
    Victor
    Admin
    Victor avatar
    1351 posts

    Posted 17 Feb 2010 Link to this post

    Hello Grant Drury-Green,

    Thank you for contacting us. Here is some sample code that demonstrates how you can get the clicked ListBoxItem object:

    public partial class Form1 : Form
    {
        RadListBox listBox = new RadListBox();
        public Form1()
        {
            InitializeComponent();
            this.Controls.Add(listBox);
     
            listBox.DataSource = new string[] { "1", "2", "3" };
            RadContextMenu menu = new RadContextMenu();
            RadContextMenuManager mgr = new RadContextMenuManager();
            mgr.SetRadContextMenu(listBox, menu);
     
            menu.DropDownOpening += new System.ComponentModel.CancelEventHandler(menu_DropDownOpening);
        }
     
        void menu_DropDownOpening(object sender, System.ComponentModel.CancelEventArgs e)
        {
            RadContextMenu menu = (RadContextMenu)sender;
            menu.Items.Clear();
            RadElement elementAtPoint = this.listBox.ElementTree.GetElementAtPoint(this.listBox.ListBoxElement.PointFromScreen(Control.MousePosition));
            if (elementAtPoint is RadListBoxItem)
            {
                // Add menu options based on the item.
                menu.Items.Add(new RadMenuItem((elementAtPoint as RadListBoxItem).Text));
            }
        }
    }

    Do not hesitate to write me back if you have further questions.

    Kind regards,
    Victor
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Grant Drury-Green
    Grant Drury-Green avatar
    23 posts
    Member since:
    Jul 2009

    Posted 17 Feb 2010 Link to this post

    Perfect - many thanks
  5. bruno
    bruno avatar
    15 posts
    Member since:
    Sep 2010

    Posted 29 Sep 2010 Link to this post

    Maybe because some updates, now this does not works.

    Based on this post:
    http://www.telerik.com/community/forums/winforms/menus/contextmenu-on-listcontrol.aspx

    I changed it to someting like this:
    private void menu_DropDownOpening(object sender, CancelEventArgs e)
    {
      RadContextMenu menu = (RadContextMenu)sender;<br>
      menu.Items.Clear();
     
      RadListVisualItem elementAtPoint =
    this.listBox.ElementTree.GetElementAtPoint(this.listBox.ListElement.PointFromScreen(Control.MousePosition)) as RadListVisualItem;

     
    if (elementAtPoint != null)
      {
        RadListDataItem dataItem = elementAtPoint.Data;
        menu.Items.Add(
    new RadMenuItem(dataItem.Text));
      }

      else

        e.Cancel =
    true;
    }

    This works fine for me.

    Hope it helps someone :)

    Best Regards,

    Bruno Almeida
  6. Peter
    Admin
    Peter avatar
    1148 posts

    Posted 04 Oct 2010 Link to this post

    Hello bruno,

    Thanks for sharing your solution with us.

    Your Telerik points have been updated for your community effort.

    If you have additional feedback to share, feel free to contact me.

    Sincerely yours,
    Peter
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  7. Sangeetha
    Sangeetha avatar
    28 posts
    Member since:
    Oct 2010

    Posted 10 Jan 2011 Link to this post

    Hi

    The code below works only for RadListCOntrol, for RadListBox it returns null.
    RadListVisualItem elementAtPoint = this.listBox.ElementTree.GetElementAtPoint(this.listBox.ListElement.PointFromScreen(Control.MousePosition)) as RadListVisualItem;

    Can someone please suggest working code for the same scenario?

    Regards,
    Sangeetha. G

  8. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 11 Jan 2011 Link to this post

    Hi Sangeetha,

    Are you trying to do the same on a RadDropDownList?
    Let me know and I'll do my best to help
    Richard
  9. Sangeetha
    Sangeetha avatar
    28 posts
    Member since:
    Oct 2010

    Posted 11 Jan 2011 Link to this post

    Hi

    Thanks for your reply.

    The control is RadListBox.
    code trying to execute:
     private void m_radListFunction_MouseDown(object sender, MouseEventArgs e)
    {
             RadElement elementAtPoint = m_radListFunction.ElementTree.GetElementAtPoint(m_radListFunction.ListBoxElement.PointFromScreen(e.Location)) as RadElement;
             if (elementAtPoint != null)
                {
                }
    }

    This returns null.

    Regards,
    Sangeetha. G
  10. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 12 Jan 2011 Link to this post

    Hello Sangeetha,

    You should use the code that was provided in the aforementioned forum post for this

    private void radListControl1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            RadListVisualItem clickedItem = radListControl1.ElementTree.GetElementAtPoint(e.Location) as RadListVisualItem;
            if (clickedItem != null)
            {
                RadListDataItem dataItem = clickedItem.Data;
                MessageBox.Show("Right Clicked " + dataItem.Text);
            }
        
    }

    Hope that helps
    Richard
  11. Sangeetha
    Sangeetha avatar
    28 posts
    Member since:
    Oct 2010

    Posted 12 Jan 2011 Link to this post

    RadListVisualItem is not supported in the telerik version used by me.
    So I tried with RadListBoxElement which returns the entire list box instead of the selected one.
  12. Peter
    Admin
    Peter avatar
    1148 posts

    Posted 13 Jan 2011 Link to this post

    Hello Grant Drury-Green,

    Thank you for writing.

    You should use the following code snippet. 

    this.radListBox1.MouseDown += new MouseEventHandler(radListBox1_MouseDown);
        
          private void radListBox1_MouseDown(object sender, MouseEventArgs e)
          {
              if (e.Button == MouseButtons.Right)
              {
                  RadElement clickedItem = radListControl2.ElementTree.GetElementAtPoint(e.Location) as RadElement;
                  if (clickedItem != null)
                  {
                      RadListBoxItem dataItem = clickedItem as RadListBoxItem;
                      MessageBox.Show("Right Clicked " + dataItem.Text);
                  }
              }
          }

    If you have any other questions, do not hesitate to contact us.

    Richard, once again, thank you for your efforts.

    All the best,
    Peter
    the Telerik team
    Q3’10 SP1 of RadControls for WinForms is available for download; also available is the Q1'11 Roadmap for Telerik Windows Forms controls.
Back to Top
UI for WinForms is Visual Studio 2017 Ready