Reputation: 472
If I were to navigate between 2 forms (or more), it would be like this:
Main Menu to second form:
Form2 form2 = new Form2(this) // Instantiate form and pass form data to next form
Hide();
Second form:
Form1 form;
public Form2(Form1 form1)
{
this.form1 = form1; //Take in previous form's data
}
Navigating forward will Hide()
the current form while navigating backwards will Close()
the current form.
But what if I am in the 3rd, 4th... nth form and I wish to go back to the main menu? Is there some kind of way to close all of my hidden forms?
Or is there a proper method to navigate between forms?
Upvotes: 1
Views: 1733
Reputation: 472
While Sebi and Mihail has given great ways for a proper navigation, I have did some practice on my own and found several solutions.
As what Sebi mentioned, a TabControl
with one main form is the better way to go in many cases as it is more fluent, does not have forms popping up all the time.
Another way is to only preserve the instance of your parent(main) form:
Navigating from a child form to another will pass the parent form and values of your data (if required), before closing the form (Once closed, all resources created is disposed).
However in some cases where you need to maintain the values for a previous child form (So that all user input does not need to be re-entered again), You have to also pass the child form and hide it instead of closing it.
One thing that confused me when I opened the question was that I had initially thought that navigating forward in forms should always use Hide();
, and not Close();
.
To close all forms except the main form (Might be useful for those who have a similar design concept as mine):
List<string> currentform = new List<string>();
foreach (Form form in Application.OpenForms)
{
if (form.Name != "Form1")
{
currentform.Add(form.Name);
}
}
foreach (string formName in currentform)
{
Application.OpenForms[formName].Close();
}
Upvotes: 0
Reputation: 3979
In my opinion you should use Interfaces to change data between forms. This let's you stay more independent and your Form2 just get the data it needs. For example:
public interface IForm1 //You should find better naming
{
void Edit(); //Method for edit some data
List<T> DataList {get;} //List with some relevant data
}
public Form Form1 : IForm1
{
public void Edit(){ //Your edit logic}
public List<T> DataList {get{return myGrid.DataSource as List<T>;}}
}
public Form Form2
{
private IForm1 formData;
public Form2(IForm1 formData)
{
formData = formData;
}
}
Further i would think about your idea to have so much forms. I think one MainForm with a TabControl as first element is better way to go in many cases. You can create a UserControl for each TabPage and just switch the TabPage instead of poping up Forms all the time.
UPDATE
This Picture maybe clarify what i mean. The TabPageHeader are all invisible (in image i make it visible for clarification). If Login succeed you just switch the TabPage.
TabControl.SelectedTabPage = tabPageMainScreen;
So it feels more fluent to the user and you don't have the problem you describe. But i would recommend to separate the Form by different UserControls to keep it simple.
UPDATE 2
On Winforms you can hide the TabHeader as suggested in this post. Example:
tabControl.ItemSize = new Size(0, 1);
tabControl.SizeMode = TabSizeMode.Fixed;
It's a bit ugly that the default TabControl don't has regular way but it works fine.
Upvotes: 1
Reputation: 4138
I would create a view model from the data in the first form and then supply it in a method on the second form.
public class Form1ViewModel {
public int Age {get; set;}
public string Name {get; set;}
}
//method on Form1
public void NavigateToForm2()
{
var vm = new Form1ViewModel{ Age = 32, Name = "Test name"};
var form2 = new Form2();
form2.SetViewModel(vm);
form2.Show();
}
Also to close the forms take a look at this link.
I would recommend taking a look into MVVM pattern because that handles the navigation, instantiation of the forms (views). A library to start with is mvvmfx. There are also other libraries for MVVM.
Upvotes: 0