rmokkenstorm Posted March 9, 2006 Posted March 9, 2006 I feel really stupid by asking this. On my form I have a button. When the user clicks the button he should open a form and close the otherone. just like a hyperlink in HTML with the same target. my code is private void btnButton1_Click(object sender, System.EventArgs e) { Agenda frm = new Agenda(); frm.ShowDialog(); } But now he opens the form above the otherone.. Quote
Cags Posted March 9, 2006 Posted March 9, 2006 This is not actually as easy to achieve as it sounds. The ShowDialog method you are using opens the second form as modal, so that the original form cannot be touched untill the second is closed. The problem is if you close the initial form the application will terminate as this is where the main message loop is being ran (as was recently reminded to me in another thread). I suggest you fake this effect by doing this following. private void btnButton1_Click(object sender, System.EventArgs e) { Agenda frm = new Agenda(); // optional to position correctly frm.Location = this.Location; frm.Size = this.Size; frm.Show(); // since closing will end app, simply hide main form this.Hide(); } NB. the closing is only an issue if it is your applications main form, if it isn't then you can close the form safely. Quote Anybody looking for a graduate programmer (Midlands, England)?
rmokkenstorm Posted March 9, 2006 Author Posted March 9, 2006 It works:D Thanks Can you help me with one little thing next and that is a back button or some kind.. So i can return to the previos Form? If I put a close button on the second form. He doesn't exit the program. But only the form form 1 (Agenda) is still hidden now. Quote
Cags Posted March 9, 2006 Posted March 9, 2006 In order to travel backwards you will need the second form to have some kind of reference to the first. This can be done in several ways. One way would be to add a public property to the second called something like PreviousForm, then upon creating this second form you set the property to the current one. Another way would be to pass the first form to the second as a parameter into the constructor and then store a local reference. If you need some help with the coding just say. Quote Anybody looking for a graduate programmer (Midlands, England)?
rmokkenstorm Posted March 9, 2006 Author Posted March 9, 2006 If You could be so kind and if its not to much trouble. Then Please Quote
Cags Posted March 9, 2006 Posted March 9, 2006 Method 1: // to be added to Agenda Form private Form _previous; public Form PreviousForm { get { return _previous; } set { _previous = value; } } Then when creating agenda form i.e. during the method we were previously talking about (button1_click)... Agenda frm = new Agenda(); frm.PreviousForm = this; Now this is done you have a link to the previous form, so when you close the second form you can simply do.. PreviousForm.Show(); Method 2: Amend the contructor of the second form by adding another parameter; private Form _previousForm; public Agenda(Form frmTemp) { _previousForm = frmTemp; } you can then how this form by calling _previousForm.Show() from anywhere in your application. Think thats right sorry its a little rushed. Quote Anybody looking for a graduate programmer (Midlands, England)?
rmokkenstorm Posted March 9, 2006 Author Posted March 9, 2006 I can't make it work.. should i put something by InitializeComponent(); ? Quote
Cags Posted March 9, 2006 Posted March 9, 2006 (edited) I don't actually know which of the two methods would be most successful, but attached is an example of one of them...PreviousFormExample.zip Edited March 9, 2006 by PlausiblyDamp Quote Anybody looking for a graduate programmer (Midlands, England)?
rmokkenstorm Posted March 9, 2006 Author Posted March 9, 2006 Just one more question my forms go's about.. 4 or 5 forms deep.. If i'm at the 5th form and press back.. will he go back to the 4th form of the first? Quote
Cags Posted March 9, 2006 Posted March 9, 2006 It depends which form you pass into the constructor when you create the object. // this will make any new form move back to teh previous Form tmpForm = new Form(this) // however if you wish all forms to go back to one you could just pass the first form down the heirarchy... Form tmpForm = new Form(_previousForm); Quote Anybody looking for a graduate programmer (Midlands, England)?
rmokkenstorm Posted March 9, 2006 Author Posted March 9, 2006 oke thanks and thanks for the fast reply's and thanks for the solution and thanks for the sample well i can go on like this forever anyway...thanks Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.