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

本文关键字:窗体 单击 按钮 检测 创建 用户 控件 动态 | 更新日期: 2023-09-27 18:37:22

两天来,我一直在试图解决这个问题,并到处寻找解决方案,所以如果这很容易回答,我提前道歉。另外,我对 c# 和编程相当陌生。

我有一个窗体,其中一个按钮创建一个新的用户控件。此用户控件有一个列表视图(现在,在某些时候我可能会将其更改为 datagridview),该视图使用 Access 数据库中的信息进行更新。单击表单上的另一个按钮(保存)时,信息将添加到数据库中。我希望我的用户控件检测何时单击"保存"按钮并更新列表视图。

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

表单内容。

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
    }
}

单击窗体上的"保存"按钮时,用户控件上没有任何反应。只是一大堆什么都没有。如果有人能告诉我我做错了什么或更好的方法来解决这个问题,我将不胜感激!如果我分享得不够多,我也可以发布更多我的代码。

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

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

编辑3:使用Harrison Paine(不确定如何标记用户名)的建议,我将SetParentForm方法添加到UserControl中。由于我的用户控件在按钮单击 Form1 之前不会创建,因此我必须在创建 Form1 后在 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,因为它具有静态修饰符(我主要是猜测我必须这样做,并且仍在努力了解"静态"的确切作用哈)

用户控件

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

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

对于那些想知道为什么我不只创建UserControl并且始终在那里的人,只有一个.Visible设置为TrueFalse,我在大量阅读后确定"更好的做法"是根据需要创建和销毁控件,特别是如果窗体上有很多控件。如果我对此完全偏离基础/疯了,请告诉我,以便我采取简单的方法:)

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

看起来您的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与"原始"对象完全不同,您正在单击其保存按钮。 您需要以某种方式传递原始对象,而不是创建一个全新的对象。 SetParentForm()方法只是执行此操作的一种方法。