How to call a method after user control is visible
Asked Answered
R

1

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.

Relegate answered 8/12, 2010 at 14:4 Comment(1)
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?Conduct
K
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();
   }
}
Krick answered 8/12, 2010 at 14:7 Comment(4)
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.)Conduct
Updated answer with example of calling a method once the UserControls is visible.Krick
You can always use userControl2.Visible = true; on init and trigger your method.. :)Kessinger
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 againRelegate

© 2022 - 2024 — McMap. All rights reserved.