用户控制 BringToFront() 事件?

User Control BringToFront() Event?

我想刷新我的用户控件上的数据网格视图,我用 BringToFront() 调用用户控件;在我的主窗体上执行操作,是否有任何事件可以在用户控件设置为前面后刷新 datagridview?类似于:

自定义控件:(有数据网格视图:dgvCustomers)

namespace Healthcare
{
    public partial class Overview : UserControl
    {
        public Overview()
        {
            InitializeComponent();
        }

        public void RefreshGrid()
        {
            using (DatabaseContext db = new DatabaseContext())
            {
                customersBindingSource.DataSource = db.Customers.ToList();
            }
        }

        private void Overview_Load(object sender, EventArgs e)
        {
            RefreshGrid();
        }
    }
}

主要形式:

private void btnOverview_Click(object sender, EventArgs e)
{
    ccOverview.BringToFront();
    txtTitle.Text = "Healthcare - Overview";
    RefreshGrid(); // Does not exist in the current context
}

将这两种方法组合在一起似乎更容易。它简单明了,易于维护,您不必通过其他事件来做神奇的事情

例子

public void RefreshMyAwesomeGrid()
{
    using (DatabaseContext db = new DatabaseContext())
    {
        customersBindingSource.DataSource = db.Customers.ToList();
    }
}

...

MyUserControl.BringToFront();
RefreshMyAwesomeGrid();

更新

The thing is that I can not acces the datagrid from my main form, on my main form I have a button and once that button is pressed it brings the user control to front so I have to put the datagridview refresh action in the User Control but I want to trigger it with a main form action

假设您的 DataGrid 在您的 userControl 中...不幸的是 BringToFront 无法被覆盖。但是,您有几个选择。

最简单的方法就是在 UserControl class RefreshMyAwesomeGrid 上添加一个 public 方法。当你调用 BringToFront 调用 RefreshMyAwesomeGrid

或者,或者只是让你的 OwnBringToFrontPlusRefresh <-- 看起来有点无聊

或者您可以使用解耦消息,具体取决于您是否使用任何框架

更新 2

只需深入了解您的用户控件

private void btnOverview_Click(object sender, EventArgs e)
{
    ccOverview.BringToFront();
    txtTitle.Text = "Healthcare - Overview";
    ccOverview.RefreshGrid(); // exists now
}
class Form1 : Form
{
    button1_MouseClick()
    {
        ucr1.BringToFront();
        ucr1.Focus(); // 포커스를 해줘야만 Enter 이벤트가 발동한다. 중요하다.

    }
}

class ucr1 : Usercontrol
{
    private void ucr1_Enter(object sender, eventargs e)
    {
        //do something
    }
}