This is a migrated thread and some comments may be shown as answers.

Where is IndexFromPoint?

4 Answers 149 Views
ListControl
This is a migrated thread and some comments may be shown as answers.
Phillip Foster
Top achievements
Rank 1
Phillip Foster asked on 21 Jan 2011, 05:28 PM
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!

4 Answers, 1 is accepted

Sort by
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 21 Jan 2011, 06:23 PM
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
0
Richard Slade
Top achievements
Rank 2
answered on 23 Jan 2011, 05:45 PM
Hello,

did this help? If so please remember to mark as answer. If you need further help please let me know
Richard
0
SomeName
Top achievements
Rank 1
answered on 21 Jul 2011, 01:33 PM

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.

 

0
Peter
Telerik team
answered on 26 Jul 2011, 03:05 PM
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!

Tags
ListControl
Asked by
Phillip Foster
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
SomeName
Top achievements
Rank 1
Peter
Telerik team
Share this question
or