C# 悬停事件不正确地触发

C# 悬停事件不正确地触发,c#,winforms,C#,Winforms,新的堆栈溢出,所以希望有人知道这里出了什么问题 出于某种原因,每当我将鼠标悬停在c#forms应用程序中的某个按钮上时,就会导致代码重置。考虑到按钮上没有任何悬停事件,这对我来说似乎很奇怪。我甚至尝试添加另一个按钮,即使我什么也不做,悬停仍然会导致重置 程序应该用颜色填充油箱,并且在填充时可以更改颜色。一旦填满,它将重置为空。下次与控制填充速度的滑块交互时,应开始填充 它这样做是正确的,但它也开始填补再次当我悬停在按钮上 namespace Assignment5A { public p

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

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

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

它这样做是正确的,但它也开始填补再次当我悬停在按钮上

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;
        }
    }
}
名称空间分配5a
{
公共部分类主窗体:窗体
{
公众浮子高度=370;
公共浮体高度=0;
公众浮水高度=0;
公共浮式水底=500;
公共浮动速度=300;
公共颜色brushColor=Color.LightBlue;
公共图形g;
公共钢笔;
公共笔刷=新的SolidBrush(颜色为浅蓝色);
公共表格(
{
初始化组件();
这个。宽度=500;
这个。高度=600;
this.BackColor=Color.Black;
SpeedTimer.Interval=(int)fillSpeed;
}
私有void MainForm_Paint(对象发送器,PaintEventArgs e)
{
g=e.图形;
钢笔=新钢笔(颜色为白色);
g、 抽绳(钢笔,50,200,50,500);
g、 抽绳(钢笔,350200350500);
g、 抽绳(钢笔,50500350500);
SpeedTimer.Start();
}
私有void SpeedTimer_Tick(对象发送方,事件参数e)
{
如果(水面高度<270)
{
笔刷=新的SolidBrush(笔刷颜色);
g=this.CreateGraphics();
g、 圆角矩形(画笔,108,136,20,水底-136-水底高度);
水面高度+=1f;
g、 FillRectangle(画笔,51,waterBottom-waterHeight,299,waterHeight-lastWaterHeight);
lastWaterHeight=水位高度;
}
其他的
{
SpeedTimer.Stop();
水面高度=0;
lastWaterHeight=0;
笔刷=新的SolidBrush(颜色为黑色);
g、 圆角矩形(画笔,51136299364);
}
}
私有无效速度滚动(对象发送器、事件参数e)
{
如果(水面高度<270)
{
浮动滚动值=速度值;
fillSpeed=300/滚动值;
SpeedTimer.Interval=(int)fillSpeed;
}
其他的
{
笔刷=新的SolidBrush(颜色为黑色);
g、 圆角矩形(画笔,51230299270);
SpeedTimer.Start();
}
}
私有void ColorButton_单击(对象发送者,事件参数e)
{
SetColor.ShowDialog();
brushColor=SetColor.Color;
}
}
}

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

不过,答案仍然取决于您的
MainForm\u Paint
事件。无论何时绘制或重绘表单,都会触发此事件,并包括表单上的任何内容(如按钮)。当鼠标悬停在某个元素上时,需要重新绘制该元素及其任何父元素,并将其重新绘制回表单级别。如果窗体被调整大小、隐藏(部分或完全隐藏)后返回视图、离开屏幕并重新打开等等,也会发生这种情况。许多不同的事情会触发窗体的
绘制事件。在您的
MainForm\u Paint
事件中,您有以下行:

SpeedTimer.Start();
…这会导致计时器启动,一切都会重新开始


与其使用
MainForm\u Paint
,我可能建议您使用表单的
Load
事件来设置所有这些初始条件。只有在表单初始化并显示在屏幕上后才会触发一次。

欢迎使用。我冒昧地删除了
visual studio
标记,因为该标记用于询问有关visual studio本身的问题(与使用VS编写的代码相反)。悬停时可能是绘画事件。这就是“重置”发生的地方吗?你有没有可能提供正在发生的事情的视频?在我的机器上似乎工作得很好。感谢您的回复!非常感谢。我真的很担心发生了什么,所以修复它真是太棒了。谢谢,欢迎来到Stack OVerflow!如果这回答了您的问题,请将其标记为正确答案,如果您愿意,请选择向上投票。这对我很有帮助,并为将来可能会来寻找类似答案的其他人改进了这一点。祝你的编程之旅好运。