Author Topic: SetFocus doesn't work!  (Read 597 times)

0 Members and 1 Guest are viewing this topic.

xys1995

  • Mosquito
  • Posts: 17
SetFocus doesn't work!
« on: June 28, 2022, 10:49:54 PM »
I have a Windows Form ,and when mouse right clik from a datagridview cell,the program ask user to select an entity in autocad drawing,everything is ok except that the cursor is windows cursor but not  the pickbox。User must click in the drawing and the cusor then changes.
I used the SetFocus Function([DllImport("user32.dll")]) ,but it does not work .Can anybody tells me how to show the pickbox icon automatically?

vs2019+AutoCAD2019


Code: [Select]
[DllImport("user32.dll")]
        public static extern System.IntPtr SetFocus(System.IntPtr hwnd);

        private void EntityDataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button != MouseButtons.Right)
                return;

            int rowIdx = e.RowIndex;
            int colIdx = e.ColumnIndex;
            if (rowIdx == -1 || colIdx != 0)
                return;
             
            this.Hide();

            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = HostApplicationServices.WorkingDatabase;
            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;

            //SetFocus doesn't work!
            SetFocus(doc.Window.Handle);

            PromptEntityOptions p = new PromptEntityOptions("\nPlease Select\n");
            PromptEntityResult res = ed.GetEntity(p);
            if (res.Status != PromptStatus.OK)
                return;
            ObjectId id = res.ObjectId;

            string layerName = string.Empty;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                DBObject obj = tr.GetObject(id, OpenMode.ForRead);
                if (obj is Entity)
                {
                    Entity ent = obj as Entity;
                    layerName = ent.Layer;
                }
                tr.Commit();
            }

            this.Show();
            MessageBox.Show(layerName);
        }

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 7485
  • AKA Daniel
Retired

xys1995

  • Mosquito
  • Posts: 17
Re: SetFocus doesn't work!
« Reply #2 on: June 29, 2022, 04:53:52 AM »
Thank you It's Alive!

I tried the code,and it still can not  work.
I find that if I add a BUTTON control and use Button_Click event,it can work properly.
Besides,I tried DataGridView_CellContentClick and DataGridView_CellClick event, it still can work properly .
And DataGridView_CellMouseClick event can't work! Maybe MouseClick event is special.....

ribarm

  • Gator
  • Posts: 2829
  • Marko Ribar, architect
Re: SetFocus doesn't work!
« Reply #3 on: June 29, 2022, 07:01:08 AM »
Daniel,
Can I reply from my personal perspective...
It could be perhaps better if you changed your avatar to something more descriptive, or just a little practically more suitfull to alien's eyes... Not sure, but it reminds me on stupid up/down highflyingbirdism... Even flower can have more meaning if you look from it's perspecitve... Someone would say : from certain point of view... Who would that be ???
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

ronjonp

  • Needs a day job
  • Posts: 7462
Re: SetFocus doesn't work!
« Reply #4 on: June 29, 2022, 11:54:31 AM »
Daniel,
Can I reply from my personal perspective...
It could be perhaps better if you changed your avatar to something more descriptive, or just a little practically more suitfull to alien's eyes... Not sure, but it reminds me on stupid up/down highflyingbirdism... Even flower can have more meaning if you look from it's perspecitve... Someone would say : from certain point of view... Who would that be ???
@Ribarm .. are you feeling OK ? That spam yesterday was very odd.  :?

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ribarm

  • Gator
  • Posts: 2829
  • Marko Ribar, architect
Re: SetFocus doesn't work!
« Reply #5 on: June 29, 2022, 12:28:18 PM »
We all feel exactly the way we feel... I was OK, just little nervous as seeing that there are no real advanicing to fortold or forsaught objective reality thoughtful thinking... Can we say, forgive them for they don't know what is happening or should happen? Well, this all is leading to some kind of from my vision task to say restore freedom to so let's say collective wealth and prosper... But, I am here alone, and we all know that history is changing... Still I am like someone else, just little different... We'll see if translation of language skills would bring us any good... If I may say, I, or someone from different culture beeing, may sound somewhat strange... But, all in all, as far as I can tell, how would someone describe disastrous news that are going if not descriptively like told... How can someone feed with weaponary and idiotism in all or everyone communicational rights if not directly and with justificationaly subjective language... From religious side, why not seek for someone from other planet, or practically different anhthem soundtrack... Best wishes to idiots from AAA...

Regards, sincerely, M.R.

BTW. Why would someone with skills like territory weoponary junkers fuck with destructive hierarchical politicians...
If I may say so, for my eyes, Wembley match between anyone "iole" normal sporting spiritual spectacle happened already... To da li je neko lud ili glup, najverovatnije je isuvise ocigledno i predvidivo bilo kome...
Koj kome, pa najverovatnije, samuraj k svome sensei svetu...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Jeff H

  • Needs a day job
  • Posts: 6117
Re: SetFocus doesn't work!
« Reply #6 on: June 29, 2022, 06:05:29 PM »
I do not what is going on and hope everyone is okay and safe as some parts of the world are from that right now, but have you tried
Code: [Select]
Autodesk.AutoCAD.Internal.Utils.SetFocusToDwgView();

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 7485
  • AKA Daniel
Re: SetFocus doesn't work!
« Reply #7 on: June 30, 2022, 01:42:30 AM »
Daniel,
Can I reply from my personal perspective...
It could be perhaps better if you changed your avatar to something more descriptive, or just a little practically more suitfull to alien's eyes... Not sure, but it reminds me on stupid up/down highflyingbirdism... Even flower can have more meaning if you look from it's perspecitve... Someone would say : from certain point of view... Who would that be ???

LOL! Itís a reference from Young Frankenstein, where heís screaming at his new creation ďitís alive, itís aliveĒ, so itís totally descriptive   :lmao:
I do change it quite often, sometimes looking left, sometimes looking right, nobody notices, I think MP did though. He had once used Marty Feldmanís mug as an avatar


Retired

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 7485
  • AKA Daniel
Re: SetFocus doesn't work!
« Reply #8 on: June 30, 2022, 01:44:32 AM »
I do not what is going on and hope everyone is okay and safe as some parts of the world are from that right now, but have you tried
Code: [Select]
Autodesk.AutoCAD.Internal.Utils.SetFocusToDwgView();

That was in the link I posted, I think the GridView is always a bit finicky, I think its like four controls all fighting for events
Retired