动态创建的用户控件检测按钮从主窗体单击

Dynamically created User Control detecting button click from main Form

在两天的大部分时间里,我一直在努力解决这个问题,并且到处搜索以找到解决方案,所以如果这个问题很容易回答,我先表示歉意。另外,我对 c# 和一般编程还很陌生。

我有一个 wherein one button creates a new 。这个用户控件有一个列表视图(现在,在某些时候我可能会将其更改为数据网格视图),它使用来自 Access 数据库的信息进行更新。单击表单上的另一个按钮(保存)时,信息将添加到数据库中。我想让我的 UserControl 检测何时单击“保存”按钮并更新列表视图。

下面是我的代码示例,已精简到我希望的重要部分。

表单内容。

public partial class Form1 : Form
{
    public event EventHandler saveClick;

    public Form1()
    {
        InitializeComponent();
    }

    public void buttonSave_Click(object sender, EventArgs e)
    {
        //Stuff happens here that saves all input to the database
        if (this.saveClick != null)
            this.saveClick(this, e);            
    }

    //Creates the UserControl TodayCallHistory as tch and expands Form1 window to accomodate the new control
    private void butListShow_Click(object sender, EventArgs e)
    {
        if (!expanded)
        {
            expanded = true;
            butListShow.Text = "\u25C0";
            TodayCallHistory tch = new TodayCallHistory();
            tch.Name = "tch";
            tch.SetParentForm(this); //This was added per the answer below
            List<int> pos = new List<int>();
            foreach (Control x in this.Controls)
            {
                int right = x.Right;
                pos.Add(right);
            }
            tch.Location = new System.Drawing.Point(pos.Max() + 10, 10);
            formWidth = this.Width;
            this.Width = this.Width + tch.Width + 10;
            this.Controls.Add(tch);
        }
        else
        {
            expanded = false;
            butListShow.Text = "\u25B6";
            foreach (Control x in this.Controls)
            {
                if (x.Name == "tch")
                    this.Controls.Remove(x);
            }
            this.Width = formWidth;
        }
    }
}

用户控件内容。

public partial class TodayCallHistory : UserControl
{
    private Form1 frm = new Form1();

    public TodayCallHistory()
    {
        InitializeComponent();
        //frm.saveClick += new EventHandler(saveWasClicked); Removed based on answer below
    }

    //This following part was added per the marked answer below
    public void SetParentForm(Form1 parent)
    {
        frm = parent;
        frm.saveClick += new EventHandler(saveWasClicked);
    }

    private void saveWasClicked(object sender, EventArgs e)
    {
        refreshList();
    }

    private void refreshList()
    {
        //Sends query to database and then populates a listview with this information
    }
}

当单击窗体上的保存按钮时,UserControl 上没有任何反应。只是一大堆什么都没有。如果有人能告诉我我做错了什么或更好的方法来解决这个问题,我将非常感激!如果我分享的不够多,我还可以 post 更多我的代码。

编辑 1:我还应该提到这是一个 WinForm,使用 Visual Studio Express 2015 for Windows Desktop 用 c# 编写。

编辑 2:添加了我的代码,用于在单击 Form1 上的按钮时创建 UserControl。同样的按钮也删除了控件。基本上我想有一个 "expand window"(我不知道实际的术语应该是什么)功能作为我的 Form1 的一部分。

编辑 3:使用下面 Harrison Paine(不确定如何标记用户名)的建议,我将 SetParentForm 方法添加到 UserControl。由于在单击 Form1 上的按钮之前不会创建我的 UserControl,因此我必须在创建后在 Form1 上添加 SetParentForm。

紧接着

tch.Name = "tch";

我加了

tch.SetParentForm(this);

编辑 4:为了避免创建新的 Form1,我做了以下更改。

表格 1

public static event EventHandler saveClick; //Added static modifier 

public void buttonSave_Click(object sender, EventArgs e)
{
    //Stuff happens here that saves all input to the database
    if (Form1.saveClick != null)
        Form1.saveClick(this, e); //Changed this.saveClick to Form1.saveClick 
}

private void butListShow_Click(object sender, EventArgs e)
{
tch.Parent = this; //All the other stuff from above, plus this now
}

我将 this.saveClick 更改为 Form1.saveClick 因为它具有 static 修饰符(我主要猜测我必须这样做并且仍在努力理解 'static' 的确切作用哈哈)

用户控件

    public TodayCallHistory()
    {
        Form1.saveClick += new EventHandler(saveWasClicked);
        InitializeComponent();
    }

我删除了 Form1 frm = new Form1(); 以及整个 SetParentForm 方法。

对于那些想知道为什么我不只是创建 UserControl 并始终存在的人,只是将 .Visible 设置为 TrueFalse,我在大量阅读后确定"better practice" 是根据需要创建和销毁控件,尤其是当表单上有很多控件时。如果我对此完全 off-base/insane,请告诉我,以便我可以采取简单的方法:)

您的 TodayCallHistory 似乎正在创建自己的 Form1 并监听该对象上的事件。

您需要传入具有用户控件的实际 Form1,然后在该对象上注册事件处理程序。

像这样:

TodayCallHistory.cs中:

public void SetParentForm(Form1 parent)
{
    frm = parent;
    frm.SaveClick += new EventHandler(saveWasClicked);
}

Form1.cs中:

public Form1
{
    InitializeComponent();
    this.myTodayCallHistory.SetParentForm(this);
}

问题源于以下原因:

public partial class TodayCallHistory : UserControl
{
    private Form1 frm = new Form1();

此处创建的 Form1 与您单击其保存按钮的 "original" 完全不同。您需要以某种方式传递原始对象,而不是创建一个全新的对象。 SetParentForm() 方法只是执行此操作的一种方法。