How to switch between a form and revit UI ?

How to switch between a form and revit UI ?

Anonymous
Not applicable
1,416 Views
11 Replies
Message 1 of 12

How to switch between a form and revit UI ?

Anonymous
Not applicable

Hi

 

I've created a windows form but I would like to be able to change view with the UI.

My problem is I can't click outside my Form while my windows form is shown.. Is there a function for that?

 

Thanks for your answers.

0 Likes
Accepted solutions (1)
1,417 Views
11 Replies
Replies (11)
Message 2 of 12

Anonymous
Not applicable
Accepted solution

Dear  sabatierae,

 

 

This will help if you post example code. But As i think you are using

 

Myform.Showdialog().  

 

thus why you are not able to click out side the form.

 

For this you have to use modeless form. 

To find example of modeless form must See SDK example. 

 

Thanks.

0 Likes
Message 3 of 12

Anonymous
Not applicable
Yes I'm using .ShowDialog method.
I'll try with a modeless form and let you know!
Thanks for your quick answer.
0 Likes
Message 4 of 12

Anonymous
Not applicable

Dear sabatiera,

 

Thanks to accept as solution . if you need help you can share with me . i will happy to help you. 

 

Thanks

0 Likes
Message 5 of 12

Anonymous
Not applicable

Hi,

 

I've changed my mind and want to create a button to hide my form using myform.Hide().

 

So my form is hidden and i'd like to show it again when I press let say the "A" key.

 

I've tried with :

 

if (Console.ReadKey(true).Key==ConsoleKey.A)
                                {
                                    myForm.Visible = true;
                                };

But I get an error running it.

 

Where is my mistake(s)?

 

Thanks for your help.

 

0 Likes
Message 6 of 12

Anonymous
Not applicable

 Dear Sabatierae,

 

 

Could you please share error message or your solution to check?.

 

As i know to activate and bring form to front you have to use.

 

Myform.BringToFront();
Myform.Activate();

 

But to help you exaclty need test example of code.

 

thanks

0 Likes
Message 7 of 12

Anonymous
Not applicable

Here is my code : 

 

System.Windows.Forms.Form form=new System.Windows.Forms.Form();
                    Button button = new Button();
                    button.Click += (sender,e)=>
                    {
                        form.Hide();
                        if (Console.ReadKey(true).Key == ConsoleKey.A)
                        {
                            form.BringToFront();
                            form.Activate();
                        }
                    };
                    form.Controls.Add(button);
                    form.ShowDialog();

 

 

And the error:

System.InvalidOperationException: Impossible de lire les touches lorsque l'application ne possède pas de console ou lorsque l'entrée sur la console a été redirigée à partir d'un fichier. Essayez Console.Read.

 

(Google trad translation : Can not read the keys when the application has no console or where entry on the console has been redirected from a file. Try Console.Read)


à System.Console.ReadKey(Boolean intercept)
à TestHideForm.Class1.<>c__DisplayClass1.<Execute>b__0(Object sender, EventArgs e) dans c:\Users\asabatier\Desktop\Test Hide Form\TestHideForm\TestHideForm\Class1.cs:ligne 32
à System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
à System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
à System.Windows.Forms.Control.WndProc(Message& m)
à System.Windows.Forms.ButtonBase.WndProc(Message& m)
à System.Windows.Forms.Button.WndProc(Message& m)
à System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

 

 

Thanks for your help!

0 Likes
Message 8 of 12

Anonymous
Not applicable

You might want to use 'MyForm.Show()' instead of 'MyForm.ShowDialog()'.

0 Likes
Message 9 of 12

Anonymous
Not applicable
Dear,

I am trying but not got this error. If you share me you test solution may be i help you.

This error is happening when form is not in current thread. there may be some other reason also but this is also a cause.


Thanks
0 Likes
Message 10 of 12

stever66
Advisor
Advisor

Are you using an external command, or an external application?  Or are you using the Macro editor?

 

I believe you have to use an external application to do what you want.  Once you hide your form, you will have to exit your code to return control to the user.   Then you will need an event handler to restart your code when the user presses "A".

0 Likes
Message 11 of 12

Anonymous
Not applicable

Hi,

thanks for your answers but still getting the same error...

 

Here is my entire code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.Attributes;
using Autodesk.Revit;
using System.Windows.Forms;
using System.Drawing;

namespace Modelisation
{
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    [Autodesk.Revit.Attributes.Regeneration(RegenerationOption.Manual)]

    public class Creation : IExternalCommand
    {
        public Result Execute(
       ExternalCommandData commandData,
       ref string message,
       ElementSet elements)
        {
            //Get application and document objects
            UIApplication uiApp = commandData.Application;
            Document doc = uiApp.ActiveUIDocument.Document;
            
            
            {
                System.Windows.Forms.Form form = new System.Windows.Forms.Form();
                Button button = new Button();
                button.Click += (sender, e) =>
                {
                    form.Hide();
                    if (Console.ReadKey(true).Key == ConsoleKey.A)
                    {
                        form.BringToFront();
                        form.Activate();
                    }
                };
                form.Controls.Add(button);
                form.Show();

                return Result.Succeeded;
            }

        }
    }
}

 

Thank a lot for your advices.

 

Sabatierae

0 Likes
Message 12 of 12

Anonymous
Not applicable

Dear Sabatierae,

 

 

As i know you want a window form that can be active and comes on front as per your needs. But  as i see your code ,accoding to this code this purpose will not serve.

 

Because when an external command return result. Current thread which is running was disposed. Since you are using form.show() thus why form is appear but it is in gen 3 garbage collector. so when ever you click on form button the whole application will closed.

 

To over come this you have to use Thread Interlocked mechanism. As i said before you must see SDK example "ModelessForm_ExternalEvent"
 and "ModelessDialog" . then if you still not get it done . Let me know i will create a easy sample for you.

In these example must focus on request.cs and requesthandler.cs

 

 

Thanks,

0 Likes