.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to restore a form in C#

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
ezcad.co.uk
1699 Views, 6 Replies

How to restore a form in C#

Hi All,

Visual Lisper trying to migrate to .net!

Can anyone please provide an example of how to restore a form?

I have an "about" project which contains 3 forms.

Form1 is displayed by default when the command is called...it contains two buttons which both close form1 & open form2 or form3. What I would like to happen is that when form2 or form3 is closed, form1 will be restored.

 

This is the code which defines the command & shows form1:

<code begins>

[CommandMethod("EzAbout")]

public void EzAbout()

{

Form1tvf1;

tvf1 = newForm1();

Autodesk.AutoCAD.ApplicationServices.Application.ShowModelessDialog(null, tvf1, false);

}

<code ends>

 

This is the code from the button on form1:

<code begins>

private void button1_Click_1(object sender, EventArgs e)

{

this.Close();

Form2 tvf2;

tvf2 = new Form2();

Autodesk.AutoCAD.ApplicationServices.Application.ShowModelessDialog(null, tvf2, false);

}

<code ends>

 

This is the code behind the close button on form2...trying to restore form1 (it closes form2 but does not restore form1)

<code begins>

private void closeButton_Click(object sender, EventArgs e)

{

this.Close();

Form1 tvf1;

tvf1 = new Form1();

Autodesk.AutoCAD.ApplicationServices.Application.ShowModelessDialog(null, tvf1, false);

}

<code ends>

 

Any guidance would be greatly appreciated.

Thanks in advance.

Steve

6 REPLIES 6
Message 2 of 7
khoa.ho
in reply to: ezcad.co.uk

Hi Steve,

You can try before closing your Form2 (or Form3), you should add an event to catch this form visibility, and then this triggered event will open Form1 after Form2 closed.

// Form2 close method
private void closeButton_Click(object sender, EventArgs e)
{
    this.VisibleChanged += OnVisibleChanged;
    this.Close();
}

private void OnVisibleChanged(object sender, EventArgs eventArgs)
{
    Form1 tvf1 = new Form1();
    Autodesk.AutoCAD.ApplicationServices.Application.ShowModelessDialog(null, tvf1, false);
}

 

-Khoa

Message 3 of 7
vinkd
in reply to: ezcad.co.uk

You could store the form instances in a field, like this:

 

Form1 tvf1 = null;
private void closeButton_Click(object sender, EventArgs e) { this.Close(); //Personally I would use this.Hide()
//If tvf1 is not instantiated yet, do so if (tvf1 == null) {
tvf1 = new Form1(); }
//And in any case, show tvf1 Autodesk.AutoCAD.ApplicationServices.Application.ShowModelessDialog(null, tvf1, false); }

 

You might need to check if tvf1 is already visible too, I have not tested the above code and it's been a while since I've used subscreens. Hope it helped 🙂

Message 4 of 7
ezcad.co.uk
in reply to: ezcad.co.uk

Hi Guys,

Thanks for the suggestions but unfortunately i have not had any success with either.

Steve

Message 5 of 7
khoa.ho
in reply to: ezcad.co.uk

I re-read your topic and wrote a sample project and saw it’s quite simple. See the following code:

 

AutoCAD command class:

using Autodesk.AutoCAD.Runtime;
namespace EzAbout
{
    public class Commands
    {
        [CommandMethod("EzAbout")]
        public static void EzAbout()
        {
            Form1 tvf1 = new Form1();
            Autodesk.AutoCAD.ApplicationServices.Application.ShowModelessDialog(null, tvf1, false);
        }
    }
}

 

Form1:

using System;
using System.Windows.Forms;
namespace EzAbout
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 tvf1 = new Form2();
            Autodesk.AutoCAD.ApplicationServices.Application.ShowModelessDialog(null, tvf1, false);
            this.Close();
        }
    }
}

 

Form2:

using System.Windows.Forms;
namespace EzAbout
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            Autodesk.AutoCAD.ApplicationServices.Application.ShowModelessDialog(new Form1());
        }
    }
}

 
This sample project can also be downloaded in the attachment. It’s tested for VS 2010, .NET 3.0, and A2009.

-Khoa

 

Message 6 of 7
ezcad.co.uk
in reply to: khoa.ho

Khoa,

Thank you so much.

Steve

Message 7 of 7
ezcad.co.uk
in reply to: ezcad.co.uk

Hi again,

I know I have previouslsy tagged this post as "resolved" but in testing it has thrown up another issue.

After implementing the code as described by Khoa, when closing (the final display of) Form1 after Form2 & Form3 have been opened & closed, AutoCAD looses focus & the screen switches to the last application you were in, hiding AutoCAD.

So, this is what happens:

1. Start command - Form1 appears.

2. Click on button1 - Form1 closes & Form2 appears.

3. Click on Form2 (default) close button. Form2 closes & Form1 is restored.

4. Click on button2 - Form1 closes, (a brief flash of the screen) & Form3 appears.

5. Click on Form3 (default) close button. Form3 closes & Form1 is restored.

6. Click on Form1 (default) close button. Form1 closes & AutoCAD shifts focus to the previously view application.

 

The sample provided by Khoa also behaves in the same manner.

 

BTW I am testing on AuotCAD 2010, VSE2010 & Win XP Pro.

 

Steve.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost