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.

1
  • 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
    Dec 8, 2010 at 14:11

1 Answer 1

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();
   }
}
4
  • 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
    Dec 8, 2010 at 14:13
  • Updated answer with example of calling a method once the UserControls is visible.
    – SwDevMan81
    Dec 8, 2010 at 14:19
  • You can always use userControl2.Visible = true; on init and trigger your method.. :)
    – Pabuc
    Dec 8, 2010 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 Dec 10, 2010 at 6:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.