Change content in a windows form

前端 未结 2 1764
北海茫月
北海茫月 2021-01-02 04:27

I\'m making an application in C# using windows forms, I want to completely swap out all the content in a windows form and replace it with something else. Is there any conven

相关标签:
2条回答
  • 2021-01-02 05:02

    Use one Panel for each unique content set you want to switch. Hide all of the panels, except the initial one. Create a variable activePanel. Set activePanel to current shown panel (i.e. initial one).

    When you need to switch, do the following:

    activePanel.Visible = false;
    activePanel = <Panel you want to open now>; //I mean the Control, not an ID or something.
    activePanel.Visible = true;
    

    Another approach is to dynamically remove and add Controls to the form, but this way you'll have to write a lot more code, hovewer, your application memory footprint should be lesser.

    0 讨论(0)
  • 2021-01-02 05:16

    This works for me: Adding all the controls like:

    form.Controls.AddRange(Pages.ToArray());
    

    Then activating the needed one with

    form.Controls[i].BringToFront();
    
    0 讨论(0)
提交回复
热议问题