Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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..

Posted

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.

Anybody looking for a graduate programmer (Midlands, England)?
Posted

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.

Posted
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.
Anybody looking for a graduate programmer (Midlands, England)?
Posted

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.

Anybody looking for a graduate programmer (Midlands, England)?
Posted (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 by PlausiblyDamp
Anybody looking for a graduate programmer (Midlands, England)?
Posted

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);

Anybody looking for a graduate programmer (Midlands, England)?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...