0

I want to load a new layout (just like we do in Android programming as layout1.loadLayout()) in C# when a button is pressed?

How can this action take place when a method is called?

If I am not clear in my question, I am looking to load a new set of controls and hiding the current controls temporarily on a window form when a button is clicked (while they should retain their current properties, so that if I go back they should be in the same way where I left them).

How can I jump between different layouts while not completely deleting and creating them again and again?

Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
HelmBurger
  • 1,168
  • 5
  • 15
  • 35

2 Answers2

0

if this is for a desktop app, look into user controls.

info here

Mike Corcoran
  • 14,072
  • 4
  • 37
  • 49
0

You could create panels to group controls and toggle it's visibility. Look for panel control in components, then add controls to it, toogle it's visibility.

  • Be careful with this approach if you're using DataBinding, as binding will only be fired when controls are actually displayed. So for example, if you have controls on a panel that is hidden when form loads, data binding will not occur until the panel is actually made visible. – Luc Morin Oct 19 '12 at 14:51
  • That's what I do when I do programming for android apps. But I want to know whether it is the right way of doing what I want or there is a much better, more efficient way too, which I don't know about? – HelmBurger Oct 19 '12 at 16:13
  • I think this is easiest way when working with win forms. If you are working with WPF then check http://stackoverflow.com/questions/910814/loading-xaml-at-runtime –  Oct 19 '12 at 16:16
  • I did this: { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { panel1.Visible = true; panel2.Visible = false; } private void button1_Click(object sender, EventArgs e) { panel1.Visible = false; panel2.Visible = true; } } } But when am clicking on button1, the panels r not being displayd as desird. Instead am seeing the same panel 1. What is the problem??? – HelmBurger Oct 19 '12 at 19:28
  • @user1759099 I think you have added panel2 inside panel1 (in design view). Therefore when you call panel1.visible = false; It hides panel1, which also hides panel2 (similar to any control added to panel1). Try moving panel2 outside panel1. To check if you have added panel2 inside panel1, try moving panel1 in design view. It that moves panel2, then you have added panel2 inside panel1. –  Oct 20 '12 at 09:51
  • I think you are right. But when I am shorten the size of panel 1 to place the panel 2 somewhere else on the form, it cuts the controls inside it. How can i keep both of the panels fully stretched to the width of form without being inside each other? – HelmBurger Oct 22 '12 at 00:24
  • Got my job done using this: http://stackoverflow.com/questions/7253702/how-does-one-make-a-child-panel-fill-the-remaining-space-in-a-parent-panel – HelmBurger Oct 22 '12 at 03:18