-
Question
Can someone help me on how to link different forms/buttons together. Say I have 2 forms, one form will act as the main application and the other one will appear when clicked on. EX: I have an application that has <BACK> <NEXT> buttons, clicking next will transfer me to the next form, back will transfer me back.
Can someone give me the source code so I can add a Back and Next. I am curently reading the Learn C# in 24 Hrs so I odn't kno much. All I kno is how to make a button, and when I dbl click on it, it will go to the coding page and I type in the code in the provided space.
-
Re: Question
Code:
SecondForm f=new SecondForm();
f.Show();// this will display your new form
-
Re: Question
If you want to be able to go back to a previous form then you're going to need a reference to that form. Let's say that you have a number of people and a package. Each person knows the next person in the sequence so when told they can pass that package on to them. What happens if they are told to pass it back? If they don't know who gave it to them then they can't. The form situation is similar. Each time a form creates and displays the next form it must pass a reference to itself to that form so that it can later pass control back if required. This would normally be done by rewriting the constructor to take an parameter that refers to the previous form, which you can then go back to if required:
Code:
public class Form1 {
private Form previousForm;
public Form1(Form previousForm) {
// This call is required by the Windows Form Designer.
InitializeComponent();
// Add any initialization after the InitializeComponent() call.
this.previousForm = previousForm;
}
private void GoBack() {
this.Hide();
this.previousForm.Show();
}
}
What you're creating is basically a wizard and just having a series of forms that each have a reference to the next and previous form is definitely not the best way to go about creating one, but it's a place to start for a very basic case.
-
Re: Question
Ok thx. Where would I put this code though? Would I dbl click on the "next" or "back" button and put the code in the space? I am still confused on that part.
-
Re: Question
GoBack needs to be executed when you click the Back button, so you need to create a Click event handler for the Back button and either call the GoBack method from there or else move the code from the GoBack method to the event handler.
An event handler is a method that handles an event, i.e. is executed whenever a specific event is raised and receives data about that event. When you double-click a control in the designer you are telling the IDE to create a handler for the control's default event. The default event for the Button class is Click.