
Grant Drury-Green
Top achievements
Rank 1
Grant Drury-Green
asked on 17 Feb 2010, 06:46 AM
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
10 Answers, 1 is accepted
0
Accepted
Hello Grant Drury-Green,
Thank you for contacting us. Here is some sample code that demonstrates how you can get the clicked ListBoxItem object:
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.
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));
}
}
}
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.
0

Grant Drury-Green
Top achievements
Rank 1
answered on 17 Feb 2010, 10:33 PM
Perfect - many thanks
0

bruno
Top achievements
Rank 1
answered on 29 Sep 2010, 06:19 PM
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:
This works fine for me.
Hope it helps someone :)
Best Regards,
Bruno Almeida
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
0
Hello bruno,
Peter
the Telerik team
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.
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
0

Sangeetha
Top achievements
Rank 1
answered on 11 Jan 2011, 05:49 AM
Hi
The code below works only for RadListCOntrol, for RadListBox it returns null.
RadListVisualItem elementAtPoint =
Can someone please suggest working code for the same scenario?
Regards,
Sangeetha. G
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
0

Richard Slade
Top achievements
Rank 2
answered on 11 Jan 2011, 04:35 PM
Hi Sangeetha,
Are you trying to do the same on a RadDropDownList?
Let me know and I'll do my best to help
Richard
Are you trying to do the same on a RadDropDownList?
Let me know and I'll do my best to help
Richard
0

Sangeetha
Top achievements
Rank 1
answered on 12 Jan 2011, 05:03 AM
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
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
0

Richard Slade
Top achievements
Rank 2
answered on 12 Jan 2011, 10:34 AM
Hello Sangeetha,
You should use the code that was provided in the aforementioned forum post for this
Hope that helps
Richard
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
0

Sangeetha
Top achievements
Rank 1
answered on 12 Jan 2011, 12:41 PM
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.
So I tried with RadListBoxElement which returns the entire list box instead of the selected one.
0
Hello Grant Drury-Green,
If you have any other questions, do not hesitate to contact us.
Peter
the Telerik team
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