0

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?

tshepang
  • 12,111
  • 21
  • 91
  • 136
King King
  • 61,710
  • 16
  • 105
  • 130

3 Answers3

2

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.

Kuzgun
  • 4,649
  • 4
  • 34
  • 48
  • Well it would be a replacement but does it mean Activated can't be raised? – King King Apr 25 '13 at 14:48
  • It means `Activated` doesn't mean what you think it means; what you want is `Shown`. – Servy Apr 25 '13 at 14:55
  • Activated is an event and I want to know in what condition it will be raised, don't you think this is strange? Or at least explain to me Activated can't be raised in such a case, post some link..., Shown is another workaround but not for (can't replace Activated) for all cases, if not so why Activated does exist??? – King King Apr 25 '13 at 15:05
  • I believe `Activated` will only be raised if the form receives input focus, either by the user clicking on it or you calling `child.Activate()`. – JosephHirn Apr 25 '13 at 16:41
  • 1
    Does the Activated Event create a new instance of the From? – Yash Saraiya Jan 07 '16 at 15:42
0

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.

King King
  • 61,710
  • 16
  • 105
  • 130
0

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

Lore
  • 1,286
  • 1
  • 22
  • 57