Domanda

I don't understand why it could be that, I thought Activated should be raised when the form is shown. In fact my form has TopLevel set to false and it's added to another form. When the main form shows, it's also visible, and I can interact with its controls but I tested and saw that the Activated is not raised.

public MainForm(){
     InitializeComponent();
     Form child = new Form();
     child.Activated += (s,e) => {
        MessageBox.Show("Activated!");
     };
     child.Size = new Size(200,100);
     child.TopLevel = false;
     child.Show();
     child.Parent = this;
}

After running the MainForm the child form is appeared inside the main one and there isn't any MessageBox displayed with the message "Activated!".

What is the additional job to do to make it raise?

È stato utile?

Soluzione

If the second form comes to screen for the first time, you can use Shown event.

Activate event is only fired when a form gets focus, but that does not contain showing for the first time. But, if the previous form which is active is outside of your app, it will not raise activate event. I mean it is valid when only viewing forms of same project.

Altri suggerimenti

Here is my answer, I noticed that only Form has Activated event, other controls don't have and once the TopLevel of Form is set to false, I think it's treated as a normal control and in that case, Activate() method will do nothing and Activated event won't be raised in any case. I think this is the reason why Activated is not raised.

Thank Kuzgun for a suggestion of using Shown instead, but this is focused on explaining why the Activated is not raised!

This answer is just my guess, even the MSDN page about Form.Activated event doesn't mention this. It should not be missed that way especially in an official documentation page.

Once the TopLevel property of Form is set to false then the form becomes a normal control, hence Activated() event will not fire.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top