WinForms - 重新加载动态创建的UserControl

时间:2016-03-02 16:57:23

标签: c# multithreading user-interface

我的应用程序获取文件列表,使用删除按钮为每个文件创建一个面板,并将所有面板放在一个flowPanel布局中。而 它正在创建面板,它显示了一个加载面板。请参阅以下链接。一切都很好。我的问题是一旦文件被删除我需要从刚刚删除文件的更新目录重新加载flowpanel。

因为我必须使我的onclick静态以保持RunAsyncProcess()方法的快乐。我无法回到我的UI重新加载控件。我希望这是有道理的。我很难为谷歌找到合适的搜索条件。

引用加载面板方法,如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);
        }

}

1 个答案:

答案 0 :(得分:0)

添加了显示更改数据的标志

static bool reload { get; set; }

添加到onload

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

添加了poll方法以检查是否应该重新加载

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();

            }
        }