Where is IndexFromPoint?

5 posts, 1 answers
  1. Phillip Foster
    Phillip Foster avatar
    58 posts
    Member since:
    Apr 2010

    Posted 21 Jan 2011 Link to this post

    I have a RadListBox on my form. I would like when the user right clicks on the list, the item under the cursor is selected before the context menu is displayed. I know with a standard Listbox, you can use "IndexFromPoint" in the mousedown method. 

    What is the solution here?

    Thanks!
  2. Answer
    Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 21 Jan 2011 Link to this post

    Hi Philip,

    there may be an easier way to do this, but this example will select the list item in the list before showing the context menu.

    Designer File
    namespace RadListBoxes
    {
        partial class Form1
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
      
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
      
            #region Windows Form Designer generated code
      
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                Telerik.WinControls.UI.RadListDataItem radListDataItem1 = new Telerik.WinControls.UI.RadListDataItem();
                Telerik.WinControls.UI.RadListDataItem radListDataItem2 = new Telerik.WinControls.UI.RadListDataItem();
                this.radListControl1 = new Telerik.WinControls.UI.RadListControl();
                ((System.ComponentModel.ISupportInitialize)(this.radListControl1)).BeginInit();
                this.SuspendLayout();
                // 
                // radListControl1
                // 
                this.radListControl1.CaseSensitiveSort = true;
                radListDataItem1.Text = "ListItem 1";
                radListDataItem1.TextWrap = true;
                radListDataItem2.Text = "ListItem 2";
                radListDataItem2.TextWrap = true;
                this.radListControl1.Items.Add(radListDataItem1);
                this.radListControl1.Items.Add(radListDataItem2);
                this.radListControl1.Location = new System.Drawing.Point(12, 12);
                this.radListControl1.Name = "radListControl1";
                this.radListControl1.Size = new System.Drawing.Size(160, 123);
                this.radListControl1.TabIndex = 0;
                this.radListControl1.Text = "radListControl1";
                this.radListControl1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.radListControl1_MouseDown);
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(184, 180);
                this.Controls.Add(this.radListControl1);
                this.Name = "Form1";
                this.Text = "Form1";
                ((System.ComponentModel.ISupportInitialize)(this.radListControl1)).EndInit();
                this.ResumeLayout(false);
      
            }
      
            #endregion
      
            private Telerik.WinControls.UI.RadListControl radListControl1;
        }
    }

    Form1.cs
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Telerik.WinControls;
    using Telerik.WinControls.UI;
    using Telerik.WinControls.RadControlSpy;
      
    namespace RadListBoxes
    {
        public partial class Form1 : Form
        {
            private ContextMenu m_MyMenu = new ContextMenu();
      
      
            public Form1()
            {
                InitializeComponent();
                this.radListControl1.ContextMenu = m_MyMenu;
                m_MyMenu.Popup += new System.EventHandler(this.PopUp);
            }
      
            private void PopUp(object sender, EventArgs e)
            {
                if (this.radListControl1.Items.Count > 0)
                {
                    m_MyMenu.MenuItems.Clear();
                    MenuItem meniItem = new MenuItem("Click Me");
                    meniItem.Click += new System.EventHandler(this.MenuItemClicked);
                    m_MyMenu.MenuItems.Add(meniItem);
                }
            }
      
            private void MenuItemClicked(object sender, EventArgs e)
            {
                MessageBox.Show("You clicked the ClickMe context menu item");
            }
      
            private void radListControl1_MouseDown(object sender, MouseEventArgs e)
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {
                    RadListVisualItem item = (RadListVisualItem)this.radListControl1.ElementTree.GetElementAtPoint(e.Location);
                    Int32 i = 0;
                    foreach (RadListVisualItem vItem  in item.Parent.Children)
                    {
                        if (vItem == item)
                        {
                            break;
                        }
                        i++;
                    }
                    this.radListControl1.Items[i].Selected = true;
                }
            }
      
        }
    }

    Hope that helps.
    Richard
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 23 Jan 2011 Link to this post

    Hello,

    did this help? If so please remember to mark as answer. If you need further help please let me know
    Richard
  5. SomeName
    SomeName avatar
    19 posts
    Member since:
    Jul 2011

    Posted 21 Jul 2011 Link to this post

    It helped, but it only works when the user doesnt scroll the listbox.

    Thats how i fixed this problem, any better way?

    RadListVisualItem item = (RadListVisualItem)(sender as RadListControl).ElementTree.GetElementAtPoint(e.Location);
    foreach (RadListDataItem lstItem in  (sender as RadListControl).Items)
    {
        if (lstItem.VisualItem == item)
        {
            lstItem.Selected = true;
            break;
        }
    }

    The problem was that the RadListVisualItem Parent.Children has a different count as the whole listbox, so the i++ didnt pick the correct item. So with stepping threw the whole ListBoxItems and checking the visualitem, it works as expected.

    In the given example from previous post, replace the code in the radListControl1_MouseDown Event with this Code.

     

  6. Peter
    Admin
    Peter avatar
    1148 posts

    Posted 26 Jul 2011 Link to this post

    Hello Somename,

    Thank you for sharing your solution with the community.

    This control is virtualized and a few visual items are reused by many logical items - so your workaround is correct.

    All the best,
    Peter
    the Telerik team

    Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

Back to Top
UI for WinForms is Visual Studio 2017 Ready