c#中单击按钮为何会触发MouseLeave事件

喜欢hellokitty 2009-04-10 11:09:44
c#中单击按钮为何会触发MouseLeave事件

我在Form1上声明了一个button1。并为button1编写了ButtonUp 和 ButtonLeave事件。我发现在单击button1时,OnMouseLeave会首先被触发,百思不得其解。望高手解答。


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.button1.MouseLeave += new System.EventHandler(this.OnMouseLeave);
this.button1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.OnMouseUp);
}

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

private void OnMouseLeave(object sender, EventArgs e)
{
MessageBox.Show("Leave");
}

private void button1_Click(object sender, EventArgs e)
{

}
}
}
...全文
397 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
喜欢hellokitty 2009-04-11
  • 打赏
  • 举报
回复
问题已解决。分没有多少,都给你了!
喜欢hellokitty 2009-04-11
  • 打赏
  • 举报
回复
因为弹出对话框时,焦点就离开按钮了!
cja03 2009-04-10
  • 打赏
  • 举报
回复
试了一下,没有这种情况哦。


private void button1_Click(object sender, EventArgs e)
{
this.textBox1.AppendText("Click\r\n");
}

private void button1_MouseDown(object sender, MouseEventArgs e)
{
this.textBox1.AppendText("Down\r\n");
}

private void button1_MouseEnter(object sender, EventArgs e)
{
this.textBox1.AppendText("Enter\r\n");
}

private void button1_MouseHover(object sender, EventArgs e)
{
this.textBox1.AppendText("Hover\r\n");
}

private void button1_MouseLeave(object sender, EventArgs e)
{
this.textBox1.AppendText("Leave\r\n");
}

private void button1_MouseMove(object sender, MouseEventArgs e)
{
if (this.textBox1.Text.EndsWith("Move\r\n"))//因为Move太频繁了
this.textBox1.AppendText("Move\r\n");
}

private void button1_MouseUp(object sender, MouseEventArgs e)
{
this.textBox1.AppendText("Up\r\n");
}

private void button1_MouseClick(object sender, MouseEventArgs e)
{
this.textBox1.AppendText("MouseClick\r\n");
}



MSDN:

Control.MouseLeave 事件

在鼠标指针离开控件时发生。

备注
鼠标事件按下列顺序发生:

MouseEnter

MouseMove

MouseHover / MouseDown / MouseWheel

MouseUp

MouseLeave
喜欢hellokitty 2009-04-10
  • 打赏
  • 举报
回复
我鼠标绝对没有离开!
喜欢hellokitty 2009-04-10
  • 打赏
  • 举报
回复
这两句是注册 事件相应函数啊,没有任何问题啊!
tianxu0836 2009-04-10
  • 打赏
  • 举报
回复
是不是
this.button1.MouseLeave += new System.EventHandler(this.OnMouseLeave);
this.button1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.OnMouseUp);
这两句导致的?
tianxu0836 2009-04-10
  • 打赏
  • 举报
回复
单击后,鼠标肯定会离开对吧?这不就mouseleave了么,你要是鼠标不离开你看看
喜欢hellokitty 2009-04-10
  • 打赏
  • 举报
回复
我在单击的时候鼠标并没有离开。而且MouseLeave事件先于MouseUp事件被触发。望高人解答

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧