当我按以下方式加载表单时:
MYFORM f = new MYFORM();
f.MdiParent = this;
f.Show();它的MyForm_Activated事件触发。但是,当我调用表单的激活方法时:
// if form is already loaded just activate it:
f.Activate();MyForm_Activated事件不会触发。这是故意的行为还是我遗漏了什么?当表单被激活时,我希望表单的激活事件能够触发。这有可能吗?谢谢
为清晰而编辑:
我有一个MDI父表单,它启动一个子窗体。子窗体显示报告,并通过构造函数通知要显示哪个报表:
public ReportForm( MyReport RPT)
{
InitializeComponent();
this.reportViewer1.Report = RPT;
this.reportViewer1.RefreshReport();
}父MDI表单这样做是为了启动ReportForm:
ActivateOrLoad action = ActivateOrLoad.Load; // default; a custom enum
foreach (Form ff in this.MdiChildren)
{
if (ff.Name == "ReportForm")
{
action = ActivateOrLoad.Activate;
ff.Activate();
}
}
//load the form only if it is not already loaded
if (action == ActivateOrLoad.Load)
{
ReportForm f = new ReportForm( new MyReports.CustomerList() );
f.MdiParent = this;
f.Show();
}当通过构造函数实例化子ReportForm时,它激活的事件就会触发。但是,当子窗体被简单激活时,子窗体的激活方法就不会触发。换句话说,通过激活方法激活一个子表单并不能真正激活它。微软正在使用“激活”来表示多个,不同的东西。这就是让我困惑的地方。
@Dyppl:当父窗体调用子窗体的激活方法时,父窗体具有焦点。
我希望做的是重用ReportForm来显示各种报告。如果它已经打开,显示客户列表,然后用户选择其他报告,我希望子表单显示另一个报表。我希望分配一个自定义的公共ReportForm.CurrentReport属性,然后简单地(重新)激活子窗体,并让它的激活事件执行以下操作:
ReportForm_Activate()
{
this.reportViewer1.Report = this.CurrentReport;
this.reportViewer1.RefreshReport();
}发布于 2011-06-09 20:47:00
当用户(或程序)将窗口带到前面时(可能在其他程序处于活动状态时单击该窗口),就会引发激活事件。
如果这是活动应用程序,Form.Activate会将其显示在前面,或者如果它不是活动应用程序,则会闪烁窗口标题。MSDN Form.Activate
发布于 2015-02-26 03:11:26
如果在调用OnFormActivated方法之前定义了Show()事件处理程序,则该事件处理程序应在加载表单时触发。考虑下面的例子。
在MyForm类中,声明以下委托:
public delegate void MyFormActivated(object sender);返回实例化和加载MyForm对象的类:
MyForm myForm = new MyForm();
myForm.OnFormActivated += new myFormActivated(myOnFormActivatedEventHandlerMethod);
myForm.MdiParent = this; // do this if the parent class is a form
myForm.Show();事件处理程序需要被调用它们的类访问,例如,实例化和加载MyForm对象的类。
注意,如果在调用Show()方法之后声明事件处理程序,则不会执行事件处理程序。
https://stackoverflow.com/questions/6298960
复制相似问题