首页 > 解决方案 > 悬停事件错误触发

问题描述

堆栈溢出的新手,所以希望有人知道这里出了什么问题。

出于某种原因,每当我将鼠标悬停在我的 c# 表单应用程序中的按钮上时,它都会导致我的代码重置。考虑到我实际上在按钮上没有任何悬停事件,这对我来说似乎很奇怪。我什至尝试添加另一个按钮,即使我什么都不做,悬停仍然会导致重置。

该程序应该用颜色填充水箱,并且可以在填充时更改颜色。一旦它被填满,它就会重置为空。下次与控制填充速度的滑块交互时,它应该开始填充。

它可以正确执行此操作,但是当我将鼠标悬停在按钮上时,它也会再次开始填充。

namespace Assignment5A
{
    public partial class MainForm : Form
    {
        public float streamHeight = 370;
        public float lastWaterHeight = 0;
        public float waterHeight = 0;
        public float waterBottom = 500;
        public float fillSpeed = 300;

        public Color brushColor = Color.LightBlue;
        public Graphics g;
        public Pen pen;
        public Brush brush = new SolidBrush(Color.LightBlue);

        public MainForm()
        {
            InitializeComponent();
            this.Width = 500;
            this.Height =600;
            this.BackColor = Color.Black;
            SpeedTimer.Interval = (int)fillSpeed;
        }

        private void MainForm_Paint(object sender, PaintEventArgs e)
        {
            g = e.Graphics;
            pen = new Pen(Color.White);
            g.DrawLine(pen, 50, 200, 50, 500);
            g.DrawLine(pen, 350, 200, 350, 500);
            g.DrawLine(pen, 50, 500, 350, 500);

            SpeedTimer.Start();
        }

        private void SpeedTimer_Tick(object sender, EventArgs e)
        {
            if (waterHeight < 270)
            {
                brush = new SolidBrush(brushColor);
                g = this.CreateGraphics();

                g.FillRectangle(brush, 108, 136, 20, waterBottom - 136 - waterHeight);
                waterHeight += 1f;
                g.FillRectangle(brush, 51, waterBottom - waterHeight, 299, waterHeight - lastWaterHeight);
                lastWaterHeight = waterHeight;
            }
            else
            {
                SpeedTimer.Stop();
                waterHeight = 0;
                lastWaterHeight = 0;
                brush = new SolidBrush(Color.Black);
                g.FillRectangle(brush, 51, 136, 299, 364);
            }
        }

        private void Speed_Scroll(object sender, EventArgs e)
        {
            if (waterHeight < 270)
            {
                float scrollValue = Speed.Value;
                fillSpeed = 300 / scrollValue;
                SpeedTimer.Interval = (int)fillSpeed;
            }
            else
            {
                brush = new SolidBrush(Color.Black);
                g.FillRectangle(brush, 51, 230, 299, 270);
                SpeedTimer.Start();
            }
        }

        private void ColorButton_Click(object sender, EventArgs e)
        {
            SetColor.ShowDialog();
            brushColor = SetColor.Color;
        }
    }
}

在此处输入图像描述

标签: c#winforms

解决方案


好的,我的第一个答案完全错误,一定是看错了问题,对不起。

不过,答案仍然取决于您的MainForm_Paint活动。每当绘制或重绘表单时都会触发此事件,并在其上包含任何内容(例如您的按钮)。当您的鼠标悬停在一个元素上时,它需要重新绘制,以及它的任何父级,直接回到表单级别。如果表单被调整大小,在隐藏(部分或完全)后返回视图,离开屏幕并重新打开等等,也会发生这种情况。许多不同的事情会触发表单的Paint事件触发。在您的MainForm_Paint活动中,您有这一行:

SpeedTimer.Start();

...这导致计时器启动,一切重新开始。

而不是使用MainForm_Paint,我可能会建议您使用表单的Load事件来设置所有这些初始条件。这只会在表单初始化并显示在屏幕上后触发一次。


推荐阅读