views:

223

answers:

4

In Flex there is the ViewStack component. Does C# have a similar control?
If so, which? If not, how do you create similar behavior?

A: 

I don't think it exists natively. You will probably have to play with the Visible property.

DrDro
+1  A: 

No, there is no standard control that provides the same behaviour.

However to get similar behaviour I would simply create a new UserControl for each item in the view stack and add these to a parent form at the same location and with the same width/height.

Using a helper method it is then simple to hide all user controls, and then display the specific user control based on an input parameter.

The major benefit of UserControls is that you can use the designer to visually create each of the separate stack items. A possible drawback is that if you have many items in the stack, or each stack item is complex, memory usage may become quite large.

Ash
And is this (in some way) how, for example, the options window in Visual Studio is created? That's what raised my question.
MysticEarth
Don't know for certain, but it would be likely use a similar approach. An even lighter weight approach could be to use Panel controls instead of UserControls, but this would likely require each panel to be dynamically populated with child controls at runtime.
Ash
+1  A: 

Yes, the TabControl component works this way. All you have to do is hide the tabs. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form. The tabs are still visible at design time, makes it easy to edit the pages. But hidden at runtime. Use the SelectedTab or SelectedIndex property to select the view.

using System;
using System.Windows.Forms;

class ViewStack : TabControl {
  protected override void WndProc(ref Message m) {
    // Hide tabs by trapping the TCM_ADJUSTRECT message
    if (m.Msg == 0x1328 && !DesignMode) m.Result = (IntPtr)1;
    else base.WndProc(ref m);
  }
}
Hans Passant
While this works, I've had z ordering issues when dealing with the tab control. To hide/show pages, it simply changes the z order of the visible page to bring it to the top. I have had some child controls being visible even when their parent tab is not active.
Ash
@ash - that's very unusual, maybe you ought to start a thread about it.
Hans Passant
A: 

Thanks Hans Passant: your solution fit right in - ViewStack even showed up in Visual Studio so I can drag and drop. Thank you.

Chetan