加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 悬停事件错误触发

发布时间:2020-12-15 22:54:09 所属栏目:百科 来源:网络整理
导读:堆栈溢出的新功能,希望有人知道这里出了什么问题. 出于某种原因,当我将鼠标悬停在我的c#表单应用程序中的按钮上时,它会导致我的代码重置.考虑到我实际上按钮上没有任何悬停事件,这对我来说似乎很奇怪.我甚至尝试添加另一个按钮,即使我什么都不做,悬停仍会导
堆栈溢出的新功能,希望有人知道这里出了什么问题.

出于某种原因,当我将鼠标悬停在我的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,500);
            g.DrawLine(pen,350,500,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,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,230,270);
                SpeedTimer.Start();
            }
        }

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

enter image description here

解决方法

好的,我的第一个答案是完全错误的,一定是读错了,对不起.

答案仍然归结为您的MainForm_Paint事件.无论何时绘制或重绘表单,都会触发此事件,并在其上包含任何内容(如按钮).当鼠标悬停在某个元素上时,需要重新绘制它以及它的任何父元素,然后再返回到表单级别.如果表单被调整大小,在隐藏(部分或完全),屏幕关闭以及重新开启等等后返回视图,也会发生这种情况.许多不同的事情将触发表单的Paint事件触发.在你的MainForm_Paint事件中,你有这一行:

SpeedTimer.Start();

…导致计时器启动,一切都重新开始.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读