0

I am currently using MSCharts in one of my windows forms. One of the quirky things about MSCharts is that you cannot trigger a MouseWheel event in the chart unless the chart has focus. To combat this, most people are saying that one should add a MouseEnter event to the chart and then Focus() the chart to allow one's MouseWheel events to fire (see here: Enabling mouse wheel zooming in a Microsoft Chart Control).

Let's say that I pull up a completely different window (call it Window A) that just so happens to be partially in front of my chart (call it window is Window B). If I accidentally move the mouse over the chart in Window B for even 10 milliseconds, Window B will take focus and Window A will be placed behind it, which is incredibly frustrating.

I've explored different options.

  • Setting Window B's TopMost property to true. The problem with this is that the user has to either close the window or minimize it to hide it. If there are a lot of windows up, it seems to be just as frustrating as the initial issue.
  • Instead of giving the MouseEnter event the ability to Focus(), let the MouseClick or MouseHover event to Focus(). The problem with MouseClick is that the user will always have to click on the chart first to zoom, which isn't bad, but can be annoying. MouseHover is okay, but the time that the event considers to be a hover is really short.

In the end, I want it so that I can put my mouse over the chart and scroll in without having to do anything (mouse clicks, or anything else). In addition to this, I don't want the form that contains the chart to jack the focus back to it if I accidentally move my mouse over it for just a second.

EDIT:

It seems that according to @TaW, the chart doesn't need focus to trigger MouseWheel events in Window 10. This is not the case in Windows 7, unfortunately.

john
  • 139
  • 1
  • 8
  • _I am currently using MSCharts in one of my windows forms. One of the quirky things about MSCharts is that you cannot trigger a MouseWheel event in the chart unless the chart has focus._ __I can't confirm this.__ Using Win10 I find that MouseWheel is triggered no matter on which control or form the focus is, as long as the mouse is over the chart. __Nor__ will setting focus to the Chart bring up the Formit is on. Not sure about other windows versions, but on the current W10 64 all is well! – TaW Jul 31 '17 at 15:11

1 Answers1

0

This may seem slightly hacky, but it works in this case:

This works through the use of the FindForm method. I never knew it was a thing until now. You can read more about it here: https://msdn.microsoft.com/en-us/library/system.windows.forms.control.findform(v=vs.110).aspx

myChart.MouseEnter+= delegate(object sender, EventArgs args) //add a mouse enter event to your chart
{
    if (!chart.Focused) //if chart isn't focused
    {
        if (chart.FindForm().ContainsFocus) //check if the form the chart is in contains focus
            chart.Focus(); //if the chart isn't focused, but the form is focused, focus on the chart
    }
};

This will still give the chart focus when you move your mouse into it and it will not allow the form that contains the chart to jack the focus from the form you're in.

john
  • 139
  • 1
  • 8