Button C#

rmokkenstorm

Freshman
Joined
Feb 20, 2006
Messages
31
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
Code:
private void btnButton1_Click(object sender, System.EventArgs e)
	{
	Agenda frm = new Agenda();
             frm.ShowDialog();
             }
But now he opens the form above the otherone..
 
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.
C#:
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.
 
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.
 
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.
 
Method 1:
C#:
// 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)...
C#:
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;
C#:
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.
 
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?
 
It depends which form you pass into the constructor when you create the object.
C#:
// 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);
 
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
 
Back
Top