Reputation: 11469
I have program with asp.net c# but now i need to create an application in windows forms, I have tried to see some tutorials but they all show only one form. How is this concept done in windows forms?
In asp.net you create a page and fill it with controls like text boxes, drop downs etc.. and maybe a button with "Go Next", then you would have another page with different controls and with its own functionality too. So when you click next you would do something like Response.Redirect("SomePage.aspx");
for example..
How is this done in windows forms?, can I have several forms and navigate from one to the other? and if so how do you persist data when navigating from one to another?.
Please excuse if the question is too basic but I am new to windows forms.
Any help would be much appreciated
Regards
Upvotes: 0
Views: 1328
Reputation: 54562
You could also make UserControls and fill them with your controls. Then you can add or remove them from your form or container. That is how I switch Edit Pages in my application.
A simple Example using 2 UserControls
and a Panel
as a container:
public partial class Form1 : Form
{
UserControl1 usr1 = new UserControl1();
UserControl2 usr2 = new UserControl2();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
panel1.Controls.Clear();
panel1.Controls.Add(usr1 );
}
private void button2_Click(object sender, EventArgs e)
{
panel1.Controls.Clear();
panel1.Controls.Add(usr2 );
}
}
Upvotes: 1
Reputation: 67135
Basically, you have one parent form that can spin off any number of children forms. So, in the case of persisting data in memory (otherwise you just use a database or file or ...), you can pass it back and forth between forms or you can store it all in the parent form and access the data in that parent form from the children. I cannot seem to find a good visual representation of this, but I have tried somewhat below:
Parent
|
|-->Child
|
|->GrandChild
|
|->Child2
So, a parent will create a new form and display it using Show or ShowModal, where Modal means that nothing else in the application can have focus while that form is showing. And, the parent is the root of it all, so the application remains open until the parent form is closed.
Second Possibility
You can create different controls that you swap in and out of the main form's panel. This is probably closer to what you would expect in the web. So, basically, you start with a default or empty screen. If it is a default screen, then it would just be a control that sits in the main form's panel. When a button is clicked, you either remove the old control and create the new one to be put in its place, or you create the new control and put it on top of the old control so that the old control is "gone" (until you "close"/destroy that control).
Please let me know if that does not make sense, and I will try to explain the concept a little more thoroughly
Upvotes: 1