Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I've looked at this thread concerning the exact same problem but that solution didn't work for me.
Basically what I am trying to accomplish is a mouse wheel event when the user is interacting with a chart control on a windows form.
Right now I have tried a few different things.

 public mainForm()
 {
     InitializeComponent();
     this.chData.MouseWheel +=new MouseEventHandler(chData_MouseWheel);
 }

Also I have tried adding this to the mainForm.Designer.cs:

this.chData.TabIndex = 2;
this.chData.Text = "chart2";

this.chData.MouseWheel += 
   new System.Windows.Forms.MouseEventHandler(this.chData_MouseWheel);

this.chData.MouseClick += 
   new System.Windows.Forms.MouseEventHandler(this.chData_MouseClick);

this.chData.MouseDoubleClick += 
   new System.Windows.Forms.MouseEventHandler(this.chData_MouseDoubleClick);

this.chData.MouseMove += 
   new System.Windows.Forms.MouseEventHandler(this.chData_MouseMove);

(I've included the whole block here for demonstration). I also have the function defined as such below:

private void chData_MouseWheel(object sender, MouseEventArgs e)
{
   MessageBox.Show("FJDKS");
}

Unfortunately I can't get the darn thing to fire? Can anyone tell me where I am going wrong? Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
529 views
Welcome To Ask or Share your Answers For Others

1 Answer

The chartcontrol needs to be focused on so the mousewheel event can fire. You can set the focus when the mouse enter in the control, and give the focus to its parent back when it leaves it.

void friendChart_MouseLeave(object sender, EventArgs e)
{
    if (friendChart.Focused)
        friendChart.Parent.Focus();
}

void friendChart_MouseEnter(object sender, EventArgs e)
{
    if (!friendChart.Focused)
        friendChart.Focus();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...