Announcements

Starting in December, we will archive content from the community that is 10 years and older. This FAQ provides more information.

Pressing Enter-key when custom made form is active opens the ilogic code editior

JhoelForshav
Mentor
Mentor

Pressing Enter-key when custom made form is active opens the ilogic code editior

JhoelForshav
Mentor
Mentor

Hi,

I sometimes build custom forms to run from ilogic. One example is the form in the picture below which i use to create and manipulate design view representations in a spiral staircase configurator.

designviewreps.PNG

I've recently discovered a problem with this and am now wondering if anybody here can explain this/has a solution for it.

When the form is active and I press the ENTER-key, the ilogic code editor will open. No error message or anything like that and I can just close the code editor to continue using the form. This means however that i cant use KeyDown events for enter keys and also other users at my company will be confused if they happen to press enter.

 

It will open the code for the most recently clicked/marked rule in the ilogic browser btw, which is weird because I can't open rules that way otherwise (By clicking them once in the browser and then hit ENTER)...

 

So, does anybody have any experience with this problem and maybe even have a solution for it?

 

/Jhoel

Reply
Accepted solutions (1)
1,926 Views
18 Replies
Replies (18)

JhoelForshav
Mentor
Mentor

After some more testing i have noticed that what actually happens when i press ENTER is that inventor once again executes the last thing i did. For example if the last thing was "Zoom All" that is what happens. Or as described above if the last thing i did before running the code was to open the ilogic code editor for a selected rule, that is what's done.

 

I have a feeling that somewhere there is an object holding transactions/commands or something that i need to clear out before showing my form....

Does anybody have an idea of what it could be? I'm thinking something in the commandmanager or transactionmanager maybe...

0 Likes

MjDeck
Autodesk
Autodesk

Maybe this is what's happening:
The form could be visible on screen, but it doesn't have the keyboard and mouse input focus. The form will only have focus if you click on it. Once you click outside of it, the main Inventor window will have the input focus. It will handle the Enter key by relaunching the last command.


Mike Deck
Software Developer
Autodesk, Inc.

0 Likes

JhoelForshav
Mentor
Mentor

The form actually has focus. for example i can capture keyboardevents etc in the form. I set the form to topmost but i dont open it with ShowDialog because i want to be able to interact with inventor with the form open. The problem is that inventor also has focus somehow. My sub that handles enter keydown in the forms numericupdown executes, but inventor captures the keydown event aswell and therefore opens the code editor/fits the model to view.

The problem seems to exist only for those two (fit view/open code editor), so the last one i've done of those (no matter what i've done after that) executes.

It's so weird because, as i said before, i cant open the code editor by selecting a rule and pressing enter if the form is not open.

0 Likes

MjDeck
Autodesk
Autodesk

I think you need extra code to handle the Enter key, and prevent Inventor from grabbing it.

Is your form written in .NET? If so, are you using VB.NET or C#? Winforms or WPF?


Mike Deck
Software Developer
Autodesk, Inc.

0 Likes

MjDeck
Autodesk
Autodesk

In either WinForms or WPF, the Enter key will work better if you set the owner of the non-modal dialog to be the main Inventor window.
To do that in C#, add the attached HwndWrapper.cs to your project.
In WinForms, use code like this:

            var dlg = new Form1();
            var wrapper = new WinformsUtil.HwndWrapper(ThisApplication.MainFrameHWND);
            dlg.Show(wrapper);

In WPF, you need to add a reference to System.Windows.Forms in order to use the HwndWrapper. Then code like this should work:

using System.Windows.Interop;
using WinformsUtil;
...
            var dlg = new Form1();

            WindowInteropHelper inspectorHelper = new WindowInteropHelper(dlg);
            HwndWrapper wrapper = new HwndWrapper(ThisApplication.MainFrameHWND);
            inspectorHelper.Owner = wrapper.Handle;

            dlg.Show();

  


Mike Deck
Software Developer
Autodesk, Inc.

0 Likes

JhoelForshav
Mentor
Mentor

Hi @MjDeck 

Thank you! This sounds like it could be just the thing i need. I'll test it when i'm back at the office on tuesday šŸ™‚

I've copied the form from a Windows Forms App VB project in visual studio... so i guess its WinForms?

 

I had time to do a little bit of testing today and it seems like i can't use WinformsUtil... So maybe not Winforms after all? I've attached my iLogic rule so ypu can take a look at it if you want to šŸ™‚

 

 

0 Likes

MjDeck
Autodesk
Autodesk
Accepted solution

@JhoelForshav , you are using WinForms. But since everything is in one iLogic rule you don't need a namespace. It should work if you copy the attached text file to the bottom of your rule. That will define the HwndWrapper class. Then delete the Form.Show line and replace it with these lines:

Dim wrapper = New HwndWrapper(ThisApplication.MainFrameHWND)
Form.Show(wrapper)

Mike Deck
Software Developer
Autodesk, Inc.

JhoelForshav
Mentor
Mentor

Thank you! This is just what i needed! Smiley Happy

0 Likes

jjstr8
Collaborator
Collaborator

Mike,

I'm not sure the wrapper class is needed.  I'm using the following in a WPF addin (just the pertinent code shown).

using System.Windows.Interop;
...
    internal class MyAddinCmd
    {
        private Inventor.Application application;
        private MyAddinCommand myAddinCommand;
...
        private void ButtonDefinition_OnExecute(NameValueMap context)
        {
            if (myAddinCommand == null || myAddinCommand.WindowClosed)
            {
                myAddinCommand = new MyAddinCommand(application);
                WindowInteropHelper helper = new WindowInteropHelper(myAddinCommand);
                helper.Owner = (IntPtr)application.MainFrameHWND;
                myAddinCommand.Show();
            }
            else
            {
                myAddinCommand.WindowState = WindowState.Normal;
                _ = myAddinCommand.Activate();
            }
        }
     }
0 Likes

MjDeck
Autodesk
Autodesk

@jjstr8 , you're right. My code has an extra step that is not needed. You can assign MainFrameHWND directly to helper.Owner.


Mike Deck
Software Developer
Autodesk, Inc.

0 Likes

Maxim-CADman77
Advisor
Advisor

@MjDeck 

Sorry for nub question, but:
Can WinformsUtil be used within iLogic?
Should I download some WinformsUtil.DLL?

0 Likes

MjDeck
Autodesk
Autodesk

Hi Maxim - if it is a .NET Framework DLL, then it might be possible to use it in iLogic. I'm not sure which utility you're referring to. Can you post a link?


Mike Deck
Software Developer
Autodesk, Inc.

0 Likes

Maxim-CADman77
Advisor
Advisor

@MjDeck 

Earlier here you've wrote:

"In either WinForms or WPF, the Enter key will work better if you set the owner of the non-modal dialog to be the main Inventor window
To do that in C#, add the attached HwndWrapper.cs to your project."

I'd like to set Inventor window as owner of the from I've drawn with iLogic (VBnet) code....

0 Likes

MjDeck
Autodesk
Autodesk

@Maxim-CADman77 - sorry, my mistake. Looking at my previous post I see that WinformsUtil is a namespace name that I used. It's not a separate DLL. But I forgot to attach the C# file HwndWrapper.cs!

In any case, you're using VB, so you don't need the C# code. The VB code for HwndWrapper is attached to Message 8 above. It's only required if you're using WinForms (and not WPF).


Mike Deck
Software Developer
Autodesk, Inc.

0 Likes

Maxim-CADman77
Advisor
Advisor

Sorry (I did not notice the attachment).
At least now I get no errors in that line,
But I'd like to know whether I can align such form to parent window using  .CenterToParent property? Or it is not available in iLogic?

0 Likes

MjDeck
Autodesk
Autodesk

CenterParent might not work. Can you use CenterScreen instead? You can set the property after creating the form object, and before showing it. For instance, here is a statement that can be added to the rule from @JhoelForshav in Message 7 above:

	Dim Form As New Views
	Form.StartPosition = FormStartPosition.CenterScreen

Mike Deck
Software Developer
Autodesk, Inc.

0 Likes

Maxim-CADman77
Advisor
Advisor

I used to use CenterParent when possible.
I believe CenterScreen can send the form to the wrong screen on multi-display configurations.

0 Likes

MjDeck
Autodesk
Autodesk

Here's code to manually center a form within the Inventor window:

	Form.StartPosition = FormStartPosition.Manual
	Dim form_pt_x = ThisApplication.Left + ThisApplication.Width / 2 - Form.Width / 2
	Dim form_pt_y = ThisApplication.Top + ThisApplication.Height / 2 - Form.Height / 2
	Form.Location = New System.Drawing.Point(form_pt_x, form_pt_y)

Mike Deck
Software Developer
Autodesk, Inc.