C# 传递控件并维护事件

发布于 2024-11-01 19:12:58 字数 1365 浏览 0 评论 0原文

假设我有一个 Form,我们将其称为 ParentForm,它包含一个 Panel,我们将其称为 ContainerPanel代码>.现在,ContainerPanel 包含一个 Panel,我们将其称为 EntityPanel

所以基本上,Form 包含一个Panel,其中包含一个Panel

在 ContainerPanel 中,我们有:

void EntityPanel_MouseDown(object sender, MouseEventArgs e)
{
  ContainerPanel.Controls.Remove(EntityPanel);
  ParentForm.AcceptEntityPanel(EntityPanel);
}

在 MainForm 中,我们有:

void AcceptEntityPanel(Panel panel)
{
  Controls.Add(panel);
  panel.MouseUp += new MouseEventHandler(
    delegate(object sender, MouseEventArgs e)
    {
      MessageBox.Show("Mouse has been released.");
    });
}

注意:这只是示例代码,我刚刚在此处键入,没有验证语法等。我意识到将这两个函数合并为一个函数是微不足道的,然而,在我的应用程序中,这两个函数还执行其他几项操作,并且应该是分开的。

因此,属于 ContainerPanelEntityPanel 需要转移到 ParentForm 当用户按下鼠标时。

当用户释放鼠标时,我仍然需要触发 MouseUp 事件,但它不起作用。

之前,我传递信息 关于面板并在父窗体上创建新面板,并手动调用 MouseDown 方法。

正如您在上面的示例中看到的,我现在正在做的是将完全相同面板传递回父表单。我本来希望这样 MouseDown/MouseUp 能够工作,但事实并非如此。

对于如何构建我的程序的这个模块,我已经没有想法了。 有什么想法吗?

Say I have a Form which we'll call it ParentForm, and it contains a Panel which we'll call ContainerPanel. Now, ContainerPanel contains a Panel, which we'll call EntityPanel.

So basically, the Form contains A Panel which contains a Panel.

In ContainerPanel, we have:

void EntityPanel_MouseDown(object sender, MouseEventArgs e)
{
  ContainerPanel.Controls.Remove(EntityPanel);
  ParentForm.AcceptEntityPanel(EntityPanel);
}

and in MainForm, we have:

void AcceptEntityPanel(Panel panel)
{
  Controls.Add(panel);
  panel.MouseUp += new MouseEventHandler(
    delegate(object sender, MouseEventArgs e)
    {
      MessageBox.Show("Mouse has been released.");
    });
}

Note: This is example code only, which I have just typed in here without verifying syntax, etc. I realise it is trivial to combine these two functions into one, however in my application these two functions do several other things and should be separate.

So the EntityPanel, which belongs to ContainerPanel needs to be transferred to ParentForm when the user presses the mouse down.

When the user releases the mouse, I still need the MouseUp event to be triggered, but it is not working.

Previously, I was passing information about the panel and creating a new panel on the parent form, and manually calling the MouseDown method.

What I'm doing now, as you can see in my above example, is that I'm passing the exact same panel back to the parent form. I had hoped that this way the MouseDown/MouseUp would work, however it didn't.

I'm running out of ideas on how else to structure this module of my program.
Any ideas?

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

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

发布评论

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

评论(1

岁月打碎记忆 2024-11-08 19:12:58

这对我有用:

 void Form1_Load(object sender, EventArgs e)
 {
    var innerPanel = new Panel();
    outerPanel.Controls.Add(innerPanel);

    innerPanel.MouseDown += (a,b) => 
     { 
       outerPanel.Controls.Remove(innerPanel);
       Controls.Add(innerPanel);
       innerPanel.MouseUp += (x,y) => MessageBox.Show("!");
     };
 }

This works for me:

 void Form1_Load(object sender, EventArgs e)
 {
    var innerPanel = new Panel();
    outerPanel.Controls.Add(innerPanel);

    innerPanel.MouseDown += (a,b) => 
     { 
       outerPanel.Controls.Remove(innerPanel);
       Controls.Add(innerPanel);
       innerPanel.MouseUp += (x,y) => MessageBox.Show("!");
     };
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文