2016-03-04 47 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引用的一種方法。我會更新答案,使其更清楚一點。 –