.NET

Reply
Active Contributor
ezcad.co.uk
Posts: 26
Registered: ‎07-15-2008
Message 1 of 7 (643 Views)
Accepted Solution

How to restore a form in C#

643 Views, 6 Replies
11-07-2012 12:23 PM

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

Mentor
khoa.ho
Posts: 209
Registered: ‎09-15-2011
Message 2 of 7 (632 Views)

Re: How to restore a form in C#

11-07-2012 12:56 PM 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

Active Member
vinkd
Posts: 7
Registered: ‎09-22-2012
Message 3 of 7 (628 Views)

Re: How to restore a form in C#

11-07-2012 12:59 PM 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 :smileyhappy:

Active Contributor
ezcad.co.uk
Posts: 26
Registered: ‎07-15-2008
Message 4 of 7 (609 Views)

Re: How to restore a form in C#

11-07-2012 01:39 PM in reply to: ezcad.co.uk

Hi Guys,

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

Steve

Mentor
khoa.ho
Posts: 209
Registered: ‎09-15-2011
Message 5 of 7 (596 Views)

Re: How to restore a form in C#

11-07-2012 03:52 PM 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

 

Active Contributor
ezcad.co.uk
Posts: 26
Registered: ‎07-15-2008
Message 6 of 7 (562 Views)

Re: How to restore a form in C#

11-08-2012 01:20 PM in reply to: khoa.ho

Khoa,

Thank you so much.

Steve

Active Contributor
ezcad.co.uk
Posts: 26
Registered: ‎07-15-2008
Message 7 of 7 (542 Views)

Re: How to restore a form in C#

11-08-2012 11:37 PM 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.

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.