Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
540 views
in Technique[技术] by (71.8m points)

c# - Mousewheel event not firing

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

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

1 Reply

0 votes
by (71.8m points)

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();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...