UserControl 中的动态事件
我使用 GUI 创建了一个自定义 UserControl,但无法让它接受自定义类中动态添加的事件。 很抱歉,如果我凭记忆弄错了确切的代码,但你明白了要点。 C# .NET 2008,Winform
我有一个“容器类”来存储我的所有信息。
主 WinForm 有一个数组,每个数组都有一个摘要面板。 主窗体还有一个“工作区”,可让您访问容器类。
这个想法是,与 CustomUserControl 交互将导致 ContainerClass 内发生一些事情。该控件使用来自那里的信息,我希望它从那里更新。
ContainerClass
{
CustomUserControl tempControl;
public ContainerClass()
{
//do stuff
tempControl = new CustomUserControl([send information]);
tempControl.Click += new Event(localClickEvent);
}
public void localClickEvent(object sender, Event e)
{
//do stuff
}
}
。
public class Form1
{
public Form1()
{
//create several container objects
//for each container object, get it's SummaryPanel
//and add it to the FlowLayoutPanel
CustomUserControl tempControl = ContainerObject.GetCustomControl();
flp_summaryPanel.Controls.Add(tempControl);
}
}
当我执行此代码时,动态事件永远不会触发。 我以前做过这类事情,但总是使用继承我需要的控件的自定义类。我从未继承过 UserControl,所以我怀疑我遗漏了一些东西。 我怀疑这是一个保护问题,但编译器没有捕获它。
更多信息
这不知何故被标记为 ASP.net 问题。事实并非如此,我正在使用 Winforms,尽管我从未明确说过。
另一件可能很重要的事情是:动态事件不在另一个控件中。它位于一个充当大型容器对象的自定义类中。
。 。
一如既往,非常感谢您的帮助! :)
I've created a custom UserControl using the GUI, and I can't get it to accept dynamically added events from within a custom class.
Sorry if I get the exact code wrong, going from memory, but you get the gist.
C# .NET 2008, Winform
I have a "container class" that stores all my information.
The main WinForm has an array of these, and they each have a summary panel.
The main form also has a "work zone" that will let you access the container class.
The idea is, interacting with the CustomUserControl will cause stuff to happen within the ContainerClass. The control uses info from there, and I want it to update from within there.
ContainerClass
{
CustomUserControl tempControl;
public ContainerClass()
{
//do stuff
tempControl = new CustomUserControl([send information]);
tempControl.Click += new Event(localClickEvent);
}
public void localClickEvent(object sender, Event e)
{
//do stuff
}
}
.
public class Form1
{
public Form1()
{
//create several container objects
//for each container object, get it's SummaryPanel
//and add it to the FlowLayoutPanel
CustomUserControl tempControl = ContainerObject.GetCustomControl();
flp_summaryPanel.Controls.Add(tempControl);
}
}
When I execute this code, the dynamic event never fires.
I've done this sort of thing before, but it was always with custom classes that inherited the control I needed. I've never inherited UserControl, so I suspect I left something out.
I susepct this is an protection issue, but the compiler isn't catching it.
MORE INFO
This somehow got labeled as an ASP.net question. It's not, I'm using Winforms, though I never specifically said so.
One other thing that may be important: The dynamic event isn't in another control. It's in a custom class that functions as a large container object.
.
.
As always, thanks so much for your help! :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试在将控件添加到
Controls
集合后附加事件处理程序这可能与以下事实有关:除非将控件添加到 NamingContainer,否则无法正确生成 ClientID。
Try attaching event handler after adding control to
Controls
collectionIt may be related to the fact that unless the control is added to NamingContainer the ClientID cannot be generated properly.
您使用的是哪个版本的 C#?试试这个:
您不想在构造函数中添加控件和附加事件。看看 ASP.NET 是如何做到这一点的,您就会明白原因。
Which version of C# are you using? Try this:
You don't want to add controls and attach events in the constructor. Look at how ASP.NET does it and you'll see why.