.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

SetFocusToDwgView

10 REPLIES 10
SOLVED
Reply
Message 1 of 11
Paulio
2427 Views, 10 Replies

SetFocusToDwgView

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

10 REPLIES 10
Message 2 of 11
norman.yuan
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.

Message 3 of 11
Paulio
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!

Message 4 of 11
norman.yuan
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.

Message 5 of 11
Paulio
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..."

 

Message 6 of 11
danny.boy.1
in reply to: Paulio

This function set focus to view:

 

Autodesk.AutoCAD.Internal.Utils.SetFocusToDwgView()

 

Works good for me.

Message 7 of 11
Paulio
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.

 

Smiley Mad

Message 8 of 11
HomeBoyLV
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

Message 9 of 11
Paulio
in reply to: HomeBoyLV

Thanks HomeBoy,

 

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

 

P

Message 10 of 11
mohnston
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
Message 11 of 11
HomeBoyLV
in reply to: mohnston

The way I understand it, and with a little speculation, I don't see it as a "Problem". My best guess is that SetFocusToDwgView is sending a message to AutoCAD, that is being consumed by the DataGridView. I would guess that when the DataGridView gets focus (and/or enters Edit mode), it spins up it own message pump, and that pump is consuming the message. I would just make sure that any DataGridView is not in edit mode before calling the SetFocusToDwgView. Setting the focus to another control (which would exit edit mode) or explicitly calling the .EndEdit should fix the problem.

 

HomeBoy Out

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost