为什么代码在鼠标事件上不起作用?

问题描述 投票:0回答:1

为什么代码不起作用?为什么代码在鼠标事件上不起作用?

    bool Click_Button = false;

    private void button1_MouseDown(object sender, MouseEventArgs e)
    {
        Click_Button = true;
    }

    private void button1_MouseUp(object sender, MouseEventArgs e)
    {
        Click_Button = false;
    }
    private void button4_MouseEnter(object sender, EventArgs e)
    {
        if (Click_Button)
            textBox1.Text += ((Button)sender).Text;
    }
c# windows winforms button mouseevent
1个回答
0
投票

您的代码不能工作的原因主要有两个:

  1. 如果单击该按钮,则会显示MouseDown,MouseUp事件,因此布尔值Click_Button始终为false。
  2. 即使您按住鼠标按钮并移至按钮4没有MouseMove事件,因为第一个按钮捕获了鼠标事件以保留其界面行为(即按IE显示状态直到您释放按钮)

据我所知,没有办法使此代码有效。

Microsoft Docs: Mouse Capture

© www.soinside.com 2019 - 2024. All rights reserved.