Jump to content
Software FX Community

Paint box when dragging mouse over chart


Recommended Posts


I need a way to select intervals in a chart. To make it easier for the user I want to click the chart and drag the mouse to either direction. There should appear a box in the background showing the interval selected. More intervals can be created by repeating the procedure.

I tried first painting on the graphics object returned from Chart.GetGraphics. But that did not work very well since I could only place code in the Post Paint operation and that one did not fire when I needed (yes maybe I could have set a property in the graph object to force the repaint). I believe the Chart uses a cash otherwise.

The solution I'm working on now: using a CustomGridLine.

If I set the position of this gridline and width I can make a "selection". Problem is it seems that width is not scaled at all when the form is resized. And there is some bias between MouseClick position and Drag position (this one is calculated the first drag).

Anyone have any other ideas of how to do this?

Some code snippets:

void chart_DragOver(object sender, DragEventArgs e)


if (!bias.HasValue) bias = (e.X - _from.X);



CustomGridLine line = (CustomGridLine)e.Data.GetData(typeof(CustomGridLine));

line.Width = Math.Abs(((e.X - _from.X) - bias.Value));

line.Value = chart.AxisX.PixelToValue(_from.X) + chart.AxisX.PixelToValue((e.X - _from.X) - bias.Value) / 2;



private void chart_MouseDown(object sender, HitTestEventArgs e)


if (!chart.Zoom && e.Button == MouseButtons.Left)




if (ModifierKeys == Keys.Shift)


_from = new Point(e.X, e.Y);

CustomGridLine gridLine = new CustomGridLine(chart.AxisX.PixelToValue(e.X), "");

gridLine.ExtraStyle = CustomGridLineStyles.BackOnly;


chart.DoDragDrop(gridLine, DragDropEffects.Move);


catch { }


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...