Windows Develop Bookmark and Share   
 index > Windows Forms General > c# noob: gui with multiple "frames"
 

c# noob: gui with multiple "frames"

hello, I have been playing around with C#and created a gui program where there the program transitions to and from multiple frames whereeach frame has its own objects and functions and ofcoursethere must be away to transfer data from one frame to the next. For example on frame A you get a text input and click "Next" which bringsup frame B where the text you inputted is now added to alabel object.
My method of doing this was having a main controller derived from object Form that would contain Panel objects as member variables. My frames were classes derived from the Panel object. The Form controlled the panel transitions by implementing the transition button clickevents. On a transition it would remove the current Panel from the control and add the next one in while copying over the data:
        void npane_next_Click(object sender, EventArgs e)
        {
            PanelA.data = PanelB.data; //copy over data
            this.Controls.RemoveAt(0); //remove current frame
            PanelB.refresh();   //refresh the next frames's objects with the new data
            this.Controls.Add(PanelB);  //add the next frame
        }
I get a good feeling that there is a more elegant, widely used method to doing things like this. Any help is appreciated.
  • Moved byOmegaManMVPTuesday, April 28, 2009 4:38 PM (From:Visual C# General)
  •  
nehemiahwillis  Wednesday, April 22, 2009 10:54 PM
I would probably go for UserControls. Create one UserControl per frame and then put your user controls in your form. You don't even have to add and remove your controls, just set the Visible property to true / false as needed.
Michal Burger  Thursday, April 23, 2009 5:04 PM
Actually you should have a class that stores the variables as instance variables and pass around a reference to an instance of this class:

class MyData
{
public string UserName = null;
}

...

MyData data = new MyData();
frame1.Data = data;
frame2.Data = data;
frame3.Data = data;

(Use properties if you like them better than public fields.) This way you send a reference to the same MyData class to all of your frames so they share the same instance and the same variables. (I.e. if one of your frames changes the UserName, all the other frames instantly see this.) I like nested classes better than unnecessary new classes though. So what would I really do is make the frames nested in your form class and then pass a reference to the form instance to your frames when you create them. they should be able to see all (even the private) variables in your form class. (I don't know if this is a good design but I like it this way :P.)
Michal Burger  Saturday, April 25, 2009 2:02 PM
Are you refer to Multiple Form?
kaymaf
I hope this helps, if that is what you want, just mark it as answer so that we can move on
kaymaf  Wednesday, April 22, 2009 11:26 PM
You can do it.A better way is to iterate thru all Controls, IMHO:

Panel tempPanel = new Panel ( );
foreach (System.Windows.Forms.Control crl inthis.Controls )
{
if ( crl.Name == "myPanel" )
{
tempPanel = crl;
break;
}
}

tempPanle.Dispose ( );

You cannot killa control in the iteration loop but you can save the reference and do it after the break;
Then you ad dyour next panel.
AlexB
AlexBB - Vista Ult64 SqlSer64 WinSer64  Thursday, April 23, 2009 1:02 AM
Are you refer to Multiple Form?
kaymaf
I hope this helps, if that is what you want, just mark it as answer so that we can move on

I dont know the exact term I was just trying to find out the popular way to do what I am attempting.
nehemiahwillis  Thursday, April 23, 2009 4:41 PM
Sounds like a Wizard to me. There is a Wizard control for asp.net, but I don't think there is one for WinForms (I don't know why, I would find it useful). You might be able to use a TabControl, but hide the tabs to simulate a Wizard experience. I've never tried that, but it might work.


If this answers your question, please mark the question as answered.
BigTuna99  Thursday, April 23, 2009 4:52 PM
I would probably go for UserControls. Create one UserControl per frame and then put your user controls in your form. You don't even have to add and remove your controls, just set the Visible property to true / false as needed.
Michal Burger  Thursday, April 23, 2009 5:04 PM
ok thanks, setting visibility seems like a good idea.
What about data sharing between the Panels? right now each frame object has their own member variables that I manually copy over each time there's a panel transition. This seems pretty weak and I know atleast one way that seems better: have a class that stores each "global variable" as a static member variable.
Are there any better ways to do this?
Thanks
nehemiahwillis  Friday, April 24, 2009 3:46 PM
Actually you should have a class that stores the variables as instance variables and pass around a reference to an instance of this class:

class MyData
{
public string UserName = null;
}

...

MyData data = new MyData();
frame1.Data = data;
frame2.Data = data;
frame3.Data = data;

(Use properties if you like them better than public fields.) This way you send a reference to the same MyData class to all of your frames so they share the same instance and the same variables. (I.e. if one of your frames changes the UserName, all the other frames instantly see this.) I like nested classes better than unnecessary new classes though. So what would I really do is make the frames nested in your form class and then pass a reference to the form instance to your frames when you create them. they should be able to see all (even the private) variables in your form class. (I don't know if this is a good design but I like it this way :P.)
Michal Burger  Saturday, April 25, 2009 2:02 PM

ok that makes sense thanks

nehemiahwillis  Tuesday, April 28, 2009 4:18 PM
Check this thread.

Hans Passant.
nobugz  Wednesday, April 29, 2009 3:58 AM

You can use google to search for other answers

Custom Search

More Threads

• Visa's CISP and storing CVV2 numbers...
• Sending data to a second dialog??
• Remove drop down from DateTimePicker
• Run-time property creation and display
• Is possible to use new resource with a treeview instead of imagelist .need Workaround.
• Zooming an image in a PictureBox (Graphics.DrawImage = slow)
• Web Browser Scroll Bar
• Slide panel(form) like ToolBox or SolutionExplorer
• Problems with TreeView not centering Text vertically on nodes
• accessing data within a dataset