private void Form1_VisibleChanged(object sender, EventArgs e) { UserControl1 uc = sender as UserControl1; if (uc != null) { button2.Enabled = uc.IsVisible; } }
private void mainPanel_VisibleChanged(object sender, EventArgs e)//mainPanel==form1_panel1 { UserControl1 uc = sender as UserControl1; if (uc != null) { button2.Enabled = uc.IsVisible; } }
/// <summary> /// Event to indicate VisibiltyChanged /// </summary> public event EventHandler VisibilityChanged; /// <summary> /// Called to signal to subscribers that VisibiltyChanged /// </summary> /// <param name="e"></param> protected virtual void OnVisibilityChanged(EventArgs e) { EventHandler eh = VisibilityChanged; if (eh != null) { eh(this, e); } }
public bool IsVisible { get { return panel2.Visible; } }
public event EventHandler VisibilityChanged; protected virtual void OnVisibilityChanged(EventArgs e) { EventHandler eh = VisibilityChanged; if (eh != null) { eh(this, e); } } public bool IsVisible { get { return panel2.Visible; } }
OnVisibilityChanged(new EventArgs());
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } void UserControl1_VisibilityChanged(object sender, EventArgs e) { UserControl1 uc = sender as UserControl1; if (uc != null) { button2.Enabled = uc.IsVisible; } } private void button1_Click(object sender, EventArgs e) { if (!mainPanel.Controls.Contains(UserControl1.Instance)) { mainPanel.Controls.Add(UserControl1.Instance); UserControl1.Instance.Dock = DockStyle.Fill; UserControl1.Instance.BringToFront(); } else { UserControl1.Instance.BringToFront();// } } private void Form1_Load(object sender, EventArgs e) { } } }