2016-03-02 46 views
1

我的應用程序獲取的文件列表創建與刪除按鈕的每個文件面板將所有面板在flowPanel佈局。而 它正在創建它顯示加載面板的面板。請參閱下面的鏈接。所有這一切都很好。我的問題是一旦文件被刪除我需要重新加載流程面板從我們剛剛刪除文件的更新目錄。的WinForms - 重裝動態創建用戶控件

因爲我必須讓我的onclick靜態保持RunAsyncProcess()方法高興。我不能回到我的用戶界面來重新加載控件。我希望這是有道理的。我很努力甚至找到正確的谷歌搜索條件。

參考到裝載面板的方法,如RunAsyncOperation(MyDelegate1);在這裏找到。 http://www.codeproject.com/Articles/24044/AJAX-style-Asynchronous-Progress-Dialog-for-WinFor

這是我控制的加載方式。

private void button1_Click(object sender, EventArgs e) 
    { 
     SuspendLayout(); 
     Control ctrl = Parent.Parent.Controls.Find("MainControlPanel", false).First(); 
     MyControl myControl = new MyControl(); 
     myControl .Dock = DockStyle.Fill; 
     Parent.Controls.Remove(this);//removes a menu control 
     ctrl.Controls.Add(myControl); 
     ResumeLayout(); 
    } 

這裏是控制

bool isLoaded {get; set;} 
List<Panel> panels {get; set;} 

protected override void OnLoad(EventArgs e) 
{ 
     isLoaded = false; 
     panels = new List<Panel>(); 
     RunAsyncOperation(MyDelegate1); 
     RunAsyncOperation(MyDelegate2); 
     while (!isLoaded) { //just wait } 
      foreach (Panel panel in panels) 
      { 
       flowLayoutPanel1.Controls.Add(panel); 
      } 
} 

AsyncProcessDelegate MyDelagate = delegate() 
{ 
    //setting up the ui 
    string[] list = ... 
     foreach(in list) 
     { 
      Panel p1 = new Panel(); 
      Button btn = new Button() 
      btn.Click += new EventHandler(MyEvent); // my event must be static 
      p1.Controls.Add(btn)     
      panels.Add(p1); 
     } 

    isloaded = true; 

} 

AsyncProcessDelegate MyDelagate2 = delegate() 
{ 
      while (!isLoaded) 
      { 
       //showing the loading ui 
       Thread.Sleep(5000); 
      } 
}; 

public static void MyEvent(object sender, EventArgs e) 
{ 

      Modifies string[] list and needs to reload 
      is there a way to call onload again? 
      How can I make it reload 

    //this does not work 
    flowLayoutPanel1.Controls.Clear(); 
    RunAsyncOperation(MyDelegate1); 
    RunAsyncOperation(MyDelegate2); 
    while (!isLoaded) { //just wait } 
     foreach (Panel panel in panels) 
     { 
      flowLayoutPanel1.Controls.Add(panel); 
     } 

} 

回答

0

添加標誌,以顯示更改後的數據

static bool reload { get; set; } 

加入到爲onload

stopwatch = new Stopwatch(); 
      reload = false; 
      stopwatch.Start(); 
      PollUpdate(); 

新增調查的方法來檢查我們是否應該重新加載

public void PollUpdate() 
     { 
      while (true) 
      { 
       if (reload) 
       { 
        stopwatch.Stop(); 
        reload = false; 
        SuspendLayout(); 
        Control ctrl = Parent.Parent.Controls.Find("MainControlPanel", false).First(); 
        AudioLibraryControl cr = new AudioLibraryControl(); 
        cr.Dock = DockStyle.Fill; 
        Parent.Controls.Remove(this); 
        ctrl.Controls.Add(cr); 
        ResumeLayout(); 
       } 
       Application.DoEvents(); 

      } 
     }