Windows Forms: Activate method and Activated event
When I load a form as follows:
MYFORM f = new MYFORM();
f.MdiParent = this;
f.Show();
its MyForm_Activated event fires. But when I invoke the form's Activate method:
// if form is already loaded just activate it:
f.Activate();
MyForm_Activated event does not fire. Is this behavior by design or am I missing something? I would like the form's Activated event to fire when the form is activated. Is that possible? Thanks
EDIT FOR CLARITY:
I have a MDI parent form which launches a child form. The child form displays a report, and it is told which report to display via its constructor:
public ReportForm( MyReport RPT)
{
InitializeComponent();
this.reportViewer1.Report = RPT;
this.reportViewer1.RefreshReport();
}
The parent MDI form had done this to launch the 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();
}
When the child ReportForm is instantiated via its constructor, its Activated event fires. But when the child form is simply activated, then the child form's Activate method does not fire. In other words, activating a child form via its Activate method does not actually activate it. Microsoft is using "activate" to mean multiple, different things. That's what was/is confusing me.
@Dyppl: When the parent form invokes the child form's Activate method, the parent form has the focus.
What I am hoping to do is REUSE the ReportForm to display various reports. If it is already open displaying the Customers list, say, and then the user selects some other report, I want the child form to display the other report. I was hoping to assign a custom public ReportForm.CurrentReport property and then simply (re)activate the child form, and have its activate event do this:
ReportForm_Activate()
{
this.reportViewer1.Report = this.CurrentReport;
开发者_如何转开发 this.reportViewer1.RefreshReport();
}
The activated event is raised when the user (or program) brings the window to the front (perhaps by clicking it when a different program is active).
Form.Activate brings it to the front if this is the active application, or it flashes the window caption if this is not the active application. MSDN Form.Activate
If you define the OnFormActivated
event handler before you call the Show()
method, the event handler should fire when the form is loaded. Consider the following example.
In the MyForm class, declare the following delegate:
public delegate void MyFormActivated(object sender);
Back in the class that instantiates and loads the MyForm object:
MyForm myForm = new MyForm();
myForm.OnFormActivated += new myFormActivated(myOnFormActivatedEventHandlerMethod);
myForm.MdiParent = this; // do this if the parent class is a form
myForm.Show();
The event handler will need to be accessible by the class that calls them, e.g., the class that instantiates and loads the MyForm object.
Note that if you declare the event handlers after you call the Show()
method, the event handlers will not be executed.
精彩评论