2016-03-04 43 views
1

我一直在试图找出这两天的好日子,并搜索到处寻找解决方案,所以如果这很容易回答我道歉前面。另外,我对c#和编程一般都很陌生。动态创建用户控件检测按钮单击主窗体

我有一个其中一个按钮创建一个新的。这个用户控件有一个列表视图(现在,在某些时候,我可能会将其更改为datagridview),该列表视图使用来自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; 
     } 
    } 
} 

UserControl stuff。

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上没有任何反应。没什么大不了的。如果有人能告诉我我做错了什么或更好的方法来解决这个问题,我将非常感激!如果我的分享不够,我也可以发布更多的代码。

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

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

编辑3:使用哈里森潘恩(不知道如何标记用户名)建议下面,我将SetParentForm方法添加到UserControl。由于我的UserControl并没有被创建,直到按钮单击Form1,我必须在Form1创建后添加SetParentForm。

右后

tch.Name = "tch"; 

我加

tch.SetParentForm(this); 

编辑4:这样才能避免建立新的Form1中,我做了如下修改。

Form1中

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.saveClickForm1.saveClick,因为它是有static修饰符(我主要是猜我不得不这样做,现在还在上理解什么“静态”确实哈工作)

用户控件

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

我除去Form1 frm = new Form1();以及整个SetParentForm米ethod。

对于那些想知道为什么我不只是有用户控件创建并总是存在的,只是一个.Visible设置为TrueFalse,我下定决心后,喜欢读的“更好的做法”是创建和销毁控件你需要他们,特别是如果你有很多他们在一个窗体上。如果我对此完全没有基础/疯狂,请让我知道,所以我可以采取简单的方式:)

回答

0

它看起来像你的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()方法只是一种方法。

+0

我得到以下错误的“this.tch.SetParentForm(this);”部分...'Form1'不包含'tch'的定义,并且没有找到接受'Form1'类型的第一个参数的扩展方法'tch'(您是否缺少使用指令或程序集引用?) 我上面更新了我的代码,以显示我的UserControl的创建,因为它在按钮单击之前不存在。这可能是问题吗? – koosh

+0

我想出了你的帮助 - 非常感谢!我只需要在创建UserControl之后将SetParentForm部件移动到。严重的是,这让我疯狂,为什么它不起作用,我不能感谢你给我一个简单明了,容易理解和详细的答案。我现在正在研究父母形式的工作方式。 – koosh

+0

@koosh“父母形式”不是一个真正的概念。你的情况是你创建了两个不同的'Form1'实例:第一个包含你的'TodayCallHistory'实例,它的构造函数创建了一个名为'frm'的'Form1'的新实例。名为'frm'的对象是相同的_type_,但不是同一个对象。 我提出的'SetParentForm()'方法只是给'TodayCallHistory'提供对Form1引用的一种方法。我会更新答案,使其更清楚一点。 –