8

I have created a Windows application in C# with two user controls.

When the form loads first user control is loaded(and shown)and when I click Next I load second user control.

Now I want a method from this user control to be called once the user control is visible.

I am not able to do so. If am call the method from Load event it gets fired before the control is visible.

Can someone please guide me on how should I make the call of method after the control is visible.

freshbm
  • 5,540
  • 5
  • 46
  • 75
Sunil Agarwal
  • 4,097
  • 5
  • 44
  • 80
  • 2
    I just tried to do this the other day. For what it's worth, I quickly realized that whatever was forcing me to search for such an event was an indicator that my design was wrong. What are you trying to do here? – Cody Gray - on strike Dec 08 '10 at 14:11

1 Answers1

3

You probably want to use the VisibleChanged event.

For example:

userControl2.VisibleChanged += new EventHandler(this.UserControl2VisibleChanged);

private void UserControl2VisibleChanged(object sender, EventArgs e)
{
   if(userControl2.Visible)
   {
      CallMyMethodIWantToRunWhenUserControl2IsVisibleHere();
   }
}
SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
  • 2
    `VisibleChanged` may not do what the asker is looking for. It is not necessarily raised when the `UserControl` is first loaded because its visibility doesn't actually *change*. (The `Visible` property is "True" by default.) – Cody Gray - on strike Dec 08 '10 at 14:13
  • Updated answer with example of calling a method once the UserControls is visible. – SwDevMan81 Dec 08 '10 at 14:19
  • You can always use userControl2.Visible = true; on init and trigger your method.. :) – Pabuc Dec 08 '10 at 14:20
  • 1
    Am not able to use the code you gave. Its calling the method even before the control is visible. Can you please check the code again – Sunil Agarwal Dec 10 '10 at 06:39