动态交换或切换控件的可见性

问题描述 投票:0回答:1

我有一个TreeView形式,该形式停靠到组框中。要解决的问题是,有一个在Task上运行的操作,该操作从服务器应用程序加载数据。运行该程序时,TreeView的位置应显示进度指示器。也就是说,应显示它而不是TreeView,并完全取代它。以下是此代码的样子:

private async void preload_data(object sender, System.EventArgs args)
{
    try
    {
        // the treeView should be disabled/invisible at this point
        // make the CircularProgressBar enabled/visible

        // get the information from the server
        await Configuration.network.fetch_stuff();
    }
    catch (Exception ex)
    {
        // something bad happened
    }
    finally
    {
        // whatever happened, the treeView should be back 
    }
}

CircularProgressBar(第三方控件)应如上面的代码所示,并应替换TreeView。它应该填充与TreeView完全相同的空间,该空间已对接填充。下面是此屏幕截图:enter image description here

此表单及其所有控件都是在设计器中设计的,我不想在那儿做,我想以编程方式来做。最好的方法是什么?我看过Controls.Remove()Controls.Add()的示例,但尚不清楚是否适合此目的。

c# winforms controls
1个回答
0
投票

使用控件的Visible属性解决。

© www.soinside.com 2019 - 2024. All rights reserved.