Skip to content

Commit 2944a8b

Browse files
committed
Add missing image, Add another KB
1 parent f6c5e0a commit 2944a8b

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

knowledge-base/chartview-scatterseries-closest-point-mouseclick.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ ticketid: 1686235
1919

2020
This article will demonstrate how to determine the nearest ScatterDataPoint to a mouse click location using the mouse click position.
2121

22+
![WinForms RadChartView ClosestDataPoint](images/chartView-closestdatapoint.png)
23+
2224
## Solution
2325

2426
To achieve this, calculate the axis values corresponding to the mouse click position on the chart. Then iterate over the data points in the series and determine the closest one.
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
---
2+
title: Highlight the Last Selected Menu Item In RadMenu
3+
description: Learn how to keep selected menu items highlighted in RadMenu for WinForms by implementing custom code.
4+
type: how-to
5+
page_title: Highlighting Selected Menu Items in RadMenu for WinForms
6+
slug: menu-highlight-last-click-item
7+
tags: menus, winforms, menu-item, highlight
8+
res_type: kb
9+
ticketid: 1687046
10+
---
11+
12+
## Environment
13+
14+
|Product Version|Product|Author|
15+
|----|----|----|
16+
|2025.1.211|RadMenu for WinForms|[Dinko Krastev](https://www.telerik.com/blogs/author/dinko-krastev)|
17+
18+
## Description
19+
20+
A common requirement is to highlight the last selected RadMenuItem. This article will demonstrate how this can be achieved.
21+
22+
## Solution
23+
24+
RadMenu for WinForms does not support this behavior out of the box. To achieve it, use custom code to manage the highlighting of the menu items. Subscribe to the `Click` event of each `RadMenuItem` and implement logic to change the `BackColor` property of the clicked item while resetting the previous item's background.
25+
26+
### Example Code
27+
28+
Below is a code example demonstrating the custom solution:
29+
30+
````C#
31+
32+
public partial class Form2 : Form
33+
{
34+
public Form2()
35+
{
36+
InitializeComponent();
37+
DesertTheme desertTheme = new DesertTheme();
38+
ThemeResolutionService.ApplicationThemeName = desertTheme.ThemeName;
39+
foreach (RadMenuItem item in this.radMenu1.Items)
40+
{
41+
item.Click += RadMenuItem_Click;
42+
foreach (RadMenuItem subItem in item.Items)
43+
{
44+
subItem.Click += RadMenuItem_Click;
45+
}
46+
}
47+
}
48+
RadMenuItem lastSelectedMenuItem = null;
49+
RadMenuItem lastSelectedSubMenuItem = null;
50+
51+
void ResetColor(RadMenuItem radMenuItem)
52+
{
53+
if (radMenuItem != null)
54+
{
55+
radMenuItem.FillPrimitive.BackColor = Color.Transparent;
56+
radMenuItem.FillPrimitive.GradientStyle = GradientStyles.Linear;
57+
}
58+
}
59+
60+
void ResetColor(RadMenuItem radMenuItem)
61+
{
62+
if (radMenuItem != null)
63+
{
64+
radMenuItem.FillPrimitive.ResetValue(Telerik.WinControls.Primitives.FillPrimitive.BackColorProperty, Telerik.WinControls.ValueResetFlags.Local);
65+
radMenuItem.FillPrimitive.ResetValue(Telerik.WinControls.Primitives.FillPrimitive.GradientStyleProperty, Telerik.WinControls.ValueResetFlags.Local);
66+
}
67+
}
68+
69+
private void RadMenuItem_Click(object sender, EventArgs e)
70+
{
71+
var newSelectedMenuItem = sender as RadMenuItem;
72+
73+
if (newSelectedMenuItem != null)
74+
{
75+
if (lastSelectedMenuItem == null)
76+
{
77+
lastSelectedMenuItem = newSelectedMenuItem;
78+
ChangeColor(lastSelectedMenuItem);
79+
}
80+
else
81+
{
82+
var text1 = this.lastSelectedMenuItem.Text;
83+
var text2 = newSelectedMenuItem.Text;
84+
85+
if (newSelectedMenuItem.HierarchyParent is RadMenuItem parentMenuItem)
86+
{
87+
if (lastSelectedSubMenuItem != newSelectedMenuItem)
88+
{
89+
ResetColor(lastSelectedSubMenuItem);
90+
}
91+
lastSelectedSubMenuItem = newSelectedMenuItem;
92+
ChangeColor(lastSelectedSubMenuItem);
93+
94+
if (lastSelectedMenuItem != null)
95+
{
96+
ResetColor(lastSelectedMenuItem);
97+
}
98+
99+
lastSelectedMenuItem = parentMenuItem;
100+
ChangeColor(parentMenuItem);
101+
102+
}
103+
else
104+
{
105+
if (lastSelectedMenuItem == newSelectedMenuItem)
106+
{
107+
return;
108+
}
109+
if (lastSelectedSubMenuItem != null)
110+
{
111+
ResetColor(lastSelectedSubMenuItem);
112+
}
113+
114+
ResetColor(lastSelectedMenuItem);
115+
116+
lastSelectedMenuItem = newSelectedMenuItem;
117+
ChangeColor(newSelectedMenuItem);
118+
}
119+
}
120+
}
121+
}
122+
}
123+
124+
````
125+
126+
### Notes
127+
128+
- The code supports one level of subitems. For deeper hierarchies, extend the logic to support multiple levels of hierarchy.
129+
130+
## See Also
131+
132+
- [RadMenu Documentation](https://docs.telerik.com/devtools/winforms/controls/menus/menu/overview)

0 commit comments

Comments
 (0)