.NET

Reply
Valued Contributor
Paulio
Posts: 82
Registered: ‎06-26-2008
Message 1 of 11 (1,298 Views)
Accepted Solution

SetFocusToDwgView

1298 Views, 10 Replies
09-07-2010 04:23 AM

Has anyone managed to get the above function to work?

 

Tony mentioned it in a post I found from October 2009 where the OP was having exactly the problem I have: Getting AutoCAD to be active after clicking a button in a modeless form.

 

The button on my form runs a function that just asks the user to select some blocks (just using editor.GetSelection ...) but you have to first click in AutoCAD to activate it, then you can select the blocks.

 

I'm using 2010 and have tried the above function of the Internal.Utils but it doesn't seem to do anything. I also tried the method that ChiefBrainCloud suggested in the previously mentioned post (SetFocus function from importing user32.dll) but that just crashes AutoCAD.

 

I'm clearly not the first to encounter this problem but I can't seem to find the solution. It should be simple shirley?

 

Thanks in advance

 

Paul

Thanks HomeBoy,

 

It seems to work if I disable the form as well as making it invisible.

 

P

*Expert Elite*
norman.yuan
Posts: 998
Registered: ‎04-27-2009
Message 2 of 11 (1,280 Views)

Re: SetFocusToDwgView

09-07-2010 07:16 AM in reply to: Paulio

It might not the ideal solution, but I do it by hiding/showing the form before and after user interaction with drawing editor.

 

This the smaple code:

 

To show form in a command method:

 

public class MyCommand
    {
        [CommandMethod("FormTest", CommandFlags.Session)]
        public static void RunThisMethod()
        {
            Document dwg = Autodesk.AutoCAD.ApplicationServices.
                      Application.DocumentManager.MdiActiveDocument;

            MyForm frm = new MyForm();
            Application.ShowModelessDialog(frm);

            dwg.Editor.WriteMessage("\nMyCommand executed.");
        }
    }

 The form has a label to show picked point and a button for user to start picking in drawing editor. The button's Click event handler is like this:

 

private void button1_Click(object sender, EventArgs e)
        {
            Document dwg = Autodesk.AutoCAD.ApplicationServices.
                Application.DocumentManager.MdiActiveDocument;
            Editor ed = dwg.Editor;

            string msg = "";

            this.Visible = false;

            PromptPointOptions opt = new 
                PromptPointOptions("\nPick point:");

            PromptPointResult res = ed.GetPoint(opt);

            if (res.Status == PromptStatus.OK)
            {
                msg = String.Format("X={0}\nY={1}\nZ={2}", 
                    res.Value.X, res.Value.Y, res.Value.X);
            }

            if (msg.Length > 0)
            {
                lblMsg.Text = msg;
            }

            this.Visible = true;
        }

 

If you run this code, you can see, right after the line

 

this.Visible=true;

 

The drawing editor gets focus and the user can start picking immediately.

 

As you may have alreay know, with modal form, one can use Ediotr.StartUserInterraction() to hide/show the form automatically (since Acad2008? When I started with Acad2006, I still have to write code to hide form before allow user to interact with drawing editor). However, with modeless form, the Ediotr.StartUserInteraction() does not work. Fortunately, my code habit from Acad2006 simply led me to use form.Visible=true/false on modeless form without issue.

 

In the era when most users still use single screen, hiding the form here is rather ideal approach anyway, while with more screens being used, user may pull the modelss form to a different screen, thus hiding the form may lead user wonderring why this unnecessary hiding.

 

So, the better solution would be for Autodesk to extend the Editor.StartUserInteraction() method to apply to both modal and modeless form.

Valued Contributor
Paulio
Posts: 82
Registered: ‎06-26-2008
Message 3 of 11 (1,271 Views)

Re: SetFocusToDwgView

09-07-2010 08:35 AM in reply to: norman.yuan

Thanks for the reply Norman but I'm already setting the form's visibility to False when the button is clicked.

I've tried doing myForm.Show as well as Application.ShowModelessDialog(myForm) just to see if there's any difference when it's shown by AutoCAD or not but no luck. I just get an arrow (default) cursor hovering over AutoCAD until I click the drawing.

It's not like it's a showstopper or anything; it's just really annoying when it should be so simple to fix!

*Expert Elite*
norman.yuan
Posts: 998
Registered: ‎04-27-2009
Message 4 of 11 (1,269 Views)

Re: SetFocusToDwgView

09-07-2010 08:46 AM in reply to: Paulio

here is a video clip showing the running of the exactly code I posted:

 

http://screencast.com/t/Y2RiYTBhNjA

 

From the video you can see the form is modeless form. When I clicked "Pick" button, the form is hidden and the mouse cursor in Editor becomes "cross" immediately for user to pick.

 

BTW, the running of the code showing in the video is on AutoCAD 2009 (Map), if that makes difference of the result between yours and mine.

Valued Contributor
Paulio
Posts: 82
Registered: ‎06-26-2008
Message 5 of 11 (1,266 Views)

Re: SetFocusToDwgView

09-07-2010 08:59 AM in reply to: norman.yuan

I didn't mean to imply that what you suggested was wrong and I'm sure it does work for you. Unfortunately, it doesn't work for me. There's obviously something else going on with my app but I can't see anything that would cause it. I'm not doing anythng particularly clever or complicated.

 

The button is actually a button in a button column of a datagridview but that shouldn't make any difference should it?

 

Perhaps it's because I've got a public variable which I set to the form when I first show it (I'm not keen on public variables but I populate the form with stuff from spreadsheets and the user doesn't want to have to load that data every time they want to see the form, so having a public variable means they only have to load once).

 

As I said, it's not that big a deal so I probably shouldn't spend too much more time on it. I just thought someone might say "I had that, all you need to do is..."

 

Valued Contributor
danny.boy.1
Posts: 84
Registered: ‎05-12-2009
Message 6 of 11 (1,245 Views)

Re: SetFocusToDwgView

09-07-2010 02:09 PM in reply to: Paulio

This function set focus to view:

 

Autodesk.AutoCAD.Internal.Utils.SetFocusToDwgView()

 

Works good for me.

Valued Contributor
Paulio
Posts: 82
Registered: ‎06-26-2008
Message 7 of 11 (1,225 Views)

Re: SetFocusToDwgView

09-08-2010 01:01 AM in reply to: Paulio

Update.

 

It seems that the button column in the datagridview is the culprit. If I just put a button on the form and call the same function that gets called in the DataGridVIew's Cell_Click event, then it works as expected (and as Norman described).

 

So the question now is, why should it make a difference?

 

I'm starting to get a tiny bit peeved with this now so if any of you clever blokes (or girls) can tell me why the difference, I'll be extremely grateful.

 

:smileymad:

Active Contributor
HomeBoyLV
Posts: 47
Registered: ‎06-04-2004
Message 8 of 11 (1,151 Views)

Re: SetFocusToDwgView

09-11-2010 02:33 PM in reply to: Paulio

Looking about on the web reveals a few potential problems working from a DataGridView. Try setting the focus to another control before calling SetFocusToDwgView. Also make sure that the DataGridView is not in edit mode. This seems work in similar cases.

 

HomeBoy Out

Valued Contributor
Paulio
Posts: 82
Registered: ‎06-26-2008
Message 9 of 11 (1,118 Views)

Re: SetFocusToDwgView

09-13-2010 12:01 AM in reply to: HomeBoyLV

Thanks HomeBoy,

 

It seems to work if I disable the form as well as making it invisible.

 

P

Distinguished Contributor
mohnston
Posts: 184
Registered: ‎09-22-2006
Message 10 of 11 (867 Views)

Re: SetFocusToDwgView

11-18-2010 09:08 AM in reply to: Paulio

Paulio

Thanks for that last post. I've been fighting this for a long time.

I never thought of Enable/Disable. Brilliant.

Also solves the issue I had with getting focus to the editor from a click on a grid in a palette.


Although, there really should be no need to do this. It's frustrating when things don't work as they should.

CAD Programming Solutions
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.