在 C# 中运行时创建的对象中处理鼠标事件

发布于 2024-08-12 13:25:11 字数 431 浏览 3 评论 0原文

我正在编写 ac# windows 应用程序,作为一项任务,我正在运行时创建 Panel 对象。我有我的自定义面板,其定义为:

class FlowState : Panel
{
:
:
}

我有一个 init 方法来设置大小、位置等。但是,一旦在 Windows 窗体上创建此面板,我想处理鼠标事件,例如 mouseDown 和 mouseUp。如果您在设计时创建了一个面板并使用 GUI 来定义这些事件,您将获得如下方法(对于名为“panel1”的面板):

private void panel1_MouseDown(object sender, MouseEventArgs e)
{
    //do stuff
}

如何将代码放入扩展 Panel 来处理鼠标事件的 FlowState 对象中比如这个?

I'm writing a c# windows app, and as one task, I am creating Panel objects at runtime. I have my custom Panel which is defined as:

class FlowState : Panel
{
:
:
}

I have an init method to set size, location, etc. However once this panel is created on the windows form, I want to handle mouse events, such as mouseDown and mouseUp. If you created a panel at design time and used the gui to define these events, you would get methods like the following (for a panel named 'panel1'):

private void panel1_MouseDown(object sender, MouseEventArgs e)
{
    //do stuff
}

How do I put code into my FlowState object which extends Panel to handle mouse events such as this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

中二柚 2024-08-19 13:25:11

您可以像这样附加事件...

private void CreatePanel()
}
    var panel = new FlowState();
    panel.MouseDown += new MouseEventHandler(MouseDown);
}

private void MouseDown(object sender, MouseEventArgs e)
{ 
}

you can attach the event like this...

private void CreatePanel()
}
    var panel = new FlowState();
    panel.MouseDown += new MouseEventHandler(MouseDown);
}

private void MouseDown(object sender, MouseEventArgs e)
{ 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文