Author Topic: Set focus to model space  (Read 4731 times)

0 Members and 2 Guests are viewing this topic.

murrdpirate

  • Guest
Set focus to model space
« on: June 30, 2010, 12:17:42 PM »
I have a button on my palette that tells the editor to prompt for an entity.  In order to actually pick an entity, I have to click on model space twice.  I cannot seem to figure out a way to automatically set the focus to the model space of the current drawing.

What's odd is that I can't tell what actually has focus.  The AutoCAD application appears to be the top window, but nothing inside it seems to have focus, not even my palette.  And even when I'm clicking buttons on my palette, as long as I don't click a button that invokes a PromptEntityResult, model space is focused and I can readily select entities.  Any ideas?

Glenn R

  • Guest
Re: Set focus to model space
« Reply #1 on: June 30, 2010, 02:02:36 PM »
You don't mention some critical things, nor post a code sample for people to try out.

What version of autocad, .net and visual studio, operating system etc.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Set focus to model space
« Reply #2 on: June 30, 2010, 02:07:29 PM »
Like Glenn said without the code is tough,
you can maybe try to set the KeepFocus property to false

Draftek

  • Guest
Re: Set focus to model space
« Reply #3 on: June 30, 2010, 02:07:35 PM »
Assuming some things (as Glenn R mentioned)

Try this:
Code: [Select]
// definition
[DllImport("user32.dll")]
static extern IntPtr SetFocus(IntPtr hWnd);
...
...
// use before  invoking the editor
SetFocus(Autodesk.AutoCAD.ApplicationServices.Application.MainWindow.Handle);
Even though the palette is modeless, it still has focus and your code is asking for a selection it cannot complete.

fixo

  • Guest
Re: Set focus to model space
« Reply #4 on: June 30, 2010, 02:19:57 PM »
I have a button on my palette that tells the editor to prompt for an entity.  In order to actually pick an entity, I have to click on model space twice.  I cannot seem to figure out a way to automatically set the focus to the model space of the current drawing.

What's odd is that I can't tell what actually has focus.  The AutoCAD application appears to be the top window, but nothing inside it seems to have focus, not even my palette.  And even when I'm clicking buttons on my palette, as long as I don't click a button that invokes a PromptEntityResult, model space is focused and I can readily select entities.  Any ideas?

Try this code to switch to desired tab
Code: [Select]
        static public void SwitchLayouts()
        {
             Document doc =
                 Application.DocumentManager.MdiActiveDocument;
            Database db =
                doc.Database;
            Editor ed =
                doc.Editor;
            Transaction tr =
              db.TransactionManager.StartTransaction();
            using (tr)
            {
                LayoutManager ltman =
                    LayoutManager.Current;
                ed.WriteMessage("\nYou are in {0}", ltman.CurrentLayout);
                string layout = "Model"; // <-- layout name you need
                ltman.CurrentLayout = layout;
                ed.WriteMessage("\nNow you've switched to {0}", ltman.CurrentLayout);
                tr.Commit();
            }
        }

~'J'~

murrdpirate

  • Guest
Re: Set focus to model space
« Reply #5 on: June 30, 2010, 08:44:29 PM »
Assuming some things (as Glenn R mentioned)

Try this:
Code: [Select]
// definition
[DllImport("user32.dll")]
static extern IntPtr SetFocus(IntPtr hWnd);
...
...
// use before  invoking the editor
SetFocus(Autodesk.AutoCAD.ApplicationServices.Application.MainWindow.Handle);
Even though the palette is modeless, it still has focus and your code is asking for a selection it cannot complete.

This worked.  Thank you very much, Draftek. 

I tried setting KeepFocus to False and using the SwitchLayouts routine just to see if those also worked, but they don't appear to be applicable to my problem.  I think KeepFocus = False would work if it was my palette that was taking focus, but I'm pretty sure it wasn't the palette...although I still have no idea what actually was taking focus.  I guess SwitchLayouts didn't do it because model space was always the selected layout, I just wasn't "in" it for some reason.  Although these didn't solve my particular problem, I wasn't aware of these things and I'm sure I will use them in the future, so thanks.

BTW, Glenn, I just figured that this was more of a common issue, like using transactions or something, and didn't really require a lot of specifics.  Mighta just gotten lucky though and it's not like it hurts to throw some of that info in there, so I'll try to remember in the future.  Maybe I can just make it my signature!

Thanks again everyone.  This forum has been a life saver for me.