C# 传递控件并维护事件
假设我有一个 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.");
});
}
注意:这只是示例代码,我刚刚在此处键入,没有验证语法等。我意识到将这两个函数合并为一个函数是微不足道的,然而,在我的应用程序中,这两个函数还执行其他几项操作,并且应该是分开的。
因此,属于 ContainerPanel
的 EntityPanel
需要转移到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这对我有用:
This works for me: