mouseenter事件的边界? [英] Boundaries for mouseenter event?
问题描述
我有一个名为Resistor的课程。是的,关于电子产品。但我还在试验。在它内部,在Paint事件中,我添加了一个rezistor.png和一些e.Graphics.FillRectangle颜色和文本的图像。
在这堂课中,我添加公共事件EventHandler MouseEnter;
从Form1我可以称之为罚款:
resistorx1.MouseEnter + = new EventHandler(resistorx1_MouseEnter);
void resistorx1_MouseEnter(object sender,EventArgs e)
{
resistorx1 .text1 =gugu;
}
但是当我用鼠标在它上面运行后,注意到了。没有文字改变!
我想有些边界必须设置为能够感觉当鼠标在里面?我怎样才能做到这一点? - 一个例子非常有用 - 谢谢。
我尝试过的事情:
a很多工作___________________
I have a class named Resistor. Yes, its about electronics. But I am experimenting, still. Inside it, in a Paint event,I add a image of a rezistor.png and some e.Graphics.FillRectangle colors and text.
In this class, I add "public event EventHandler MouseEnter;"
And from Form1 I can call it fine:
resistorx1.MouseEnter += new EventHandler(resistorx1_MouseEnter);
void resistorx1_MouseEnter(object sender, EventArgs e)
{
resistorx1.text1 = "gugu";
}
But noting is happening after I run with my mouse over it. No text changing!
I suppose some boundaries must be set to be able to "feel" when the mouse is inside ? How can I do that? - an example would help immensely - thank you.
What I have tried:
a lot of work ___________________
推荐答案
你不能只是添加一个事件并期望它自动连接 - 如果你想响应系统事件那么这个类需要是一个了解系统事件的人:一个UserControl。
我快速构建了一个,它可以工作。添加一个新的UserControl,将其命名为Resistor并将其CS代码设置为:
You can't just add an event and expect it to be automatically hooked up - if you want to respond to system events then the class needs to be one that knows about system events: a UserControl.
I quickly constructed one, and it works. Add a new UserControl, Call it "Resistor" and set it's CS code to this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GeneralTesting
{
public partial class Resistor : UserControl
{
public Resistor()
{
InitializeComponent();
Text = "100R";
}
private void Resistor_Paint(object sender, PaintEventArgs e)
{
e.Graphics.FillRectangle(Brushes.Pink, e.ClipRectangle);
e.Graphics.DrawString(Text, Font, Brushes.Black, e.ClipRectangle);
}
private void Resistor_MouseEnter(object sender, EventArgs e)
{
Text = "Hello";
Invalidate();
}
private void Resistor_MouseLeave(object sender, EventArgs e)
{
Text = "100R";
Invalidate();
}
}
}
在设计模式下删除我的表单,然后运行应用程序。当鼠标进入时,它会改变文本,当鼠标离开时,它会返回。
Drop one on my form in design mode, and run the app. when the mouse enters, it changes text, when the mouse leaves, it goes back.
这篇关于mouseenter事件的边界?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!