Amit Kumar
Amit Kumar

Reputation: 1059

How to add a shown event in user control

I have a user control that i have to add a shown event or there is another event for user control which behaves like shown event of windows form.

Upvotes: 3

Views: 3392

Answers (1)

LarsTech
LarsTech

Reputation: 81655

You can fake your own shown method by using the Paint event of the usercontrol:

public UserControl1() {
  InitializeComponent();
}

public event EventHandler Shown;
private bool wasShown = false;

protected override void OnPaint(PaintEventArgs e) {
  base.OnPaint(e);
  if (!wasShown) {
    wasShown = true;
    if (Shown != null) {
      Shown(this, EventArgs.Empty);
    }
  }
}

Upvotes: 9

Related Questions