Author Topic: Modeless Dialog Box - StartPosition help  (Read 2908 times)

0 Members and 1 Guest are viewing this topic.

mr_nick

  • Guest
Modeless Dialog Box - StartPosition help
« on: August 18, 2014, 09:37:51 AM »
Hi, I am in the process of learning C# and am trying to port some of my old vlisp routines as part of the learning process. In one particular routine I want to display a modeless dialog while a process is running so that the user doesn't think that their machine has hung while background processes are running. I have got the dialog showing up OK but I can't get it to centre on the parent window - one first run it appears top left and then on subsequent runs it moves slightly down and right each time until it reaches a particular point and then it reverts back to top left and the cycle starts again. I have got the StartPosition set to 'CenterParent' but this is being completely ignored.

Can anybody tell me what I need to do to get my modeless dialog to be correctly centred on the parent window?

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Modeless Dialog Box - StartPosition help
« Reply #1 on: August 18, 2014, 02:56:48 PM »
I am not where I can test these, but here are some suggestions that may work:

1) Try setting the parent form before displaying the form;
Code: [Select]
MyForm.Parent = this;
//or
MyForm.Show(this);

OR

2) Try centering the form manually using the parent form location;
Code: [Select]
MyForm.StartPosition = FormStartPosition.Manual;
MyForm.Location = new Point(this.Location.X + (this.Width - f2.Width) / 2, this.Location.Y + (this.Height - f2.Height) / 2);
MyForm.Show(this);

Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

n.yuan

  • Bull Frog
  • Posts: 348
Re: Modeless Dialog Box - StartPosition help
« Reply #2 on: August 18, 2014, 04:26:06 PM »
Assume you are using System.Windows.Forms.Form as your modeless dialog box. If you show the modeless dialog box correctly (i.e. using Application.ShowModelessDialog(), not Form.Show(), as Autodesk's development document recommended), then setting form's "StartPosition" will have no effect on where the form is displayed: AutoCAD remembers where the last time the form was positioned. Also, in most cases, the modeless form usually is a static/Shared instance during an AutoCAD session (i.e. only a single instance of the form is created, and its visibility is set to true or false, based on needs. If each time when user close the modeless form and you dispose it and create new instance, AutoCAD open the form at the last position with a small offset.

To force a preferable StartPosition while still using Application.ShowModeless() to open a form, you can handle Form.Shown event and write code to position the form. You can use System.Windows.Forms.Screen.Primary/AllScreens to determine the primary screen, or the desired screen you want to form to be, you can also user Application.MainWindo to find AutoCAD window's size/center for your form.

Just remember, Form.Shown only fires once when the form is first shown. If you only have a singleton modeless form and toggle it on and off, but wish it to be a certain location when it is turned on regardless where it was turned off, then you need to handle Form.VisibleChanged event to manipulate its position.

mr_nick

  • Guest
Re: Modeless Dialog Box - StartPosition help
« Reply #3 on: August 19, 2014, 09:03:50 AM »
Thanks for the responses.

Keith - I had tried a similar but slightly different approach as your (2) which didn't produce the desired results so I tried using your syntax but it produced the exact same result in that the modeless dialog was consistently forced into the absolute top left of the screen. I'm assuming this is down to not getting the correct window size of the autodesk application so the centering coords are skewed. This is something I need to dabble with to confirm this assumption.

n.yuan - As I said in my opening post, I'm very much still in the learning process in terms of C# so I need to have a read up on your suggestions to see how I go about implementing them as currently I'm swimming in a pool of new terminolgy and seemingly quite out of my depth  :-(


Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Modeless Dialog Box - StartPosition help
« Reply #4 on: August 19, 2014, 05:46:36 PM »
If you are not getting the correct parent, you cannot get the correct location.

Try getting the handle of the parent and see what you end up with. If you get 0 you either have the desktop or no window.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

CADbloke

  • Bull Frog
  • Posts: 342
  • Crash Test Dummy
Re: Modeless Dialog Box - StartPosition help
« Reply #5 on: August 19, 2014, 06:04:11 PM »
currently I'm swimming in a pool of new terminology and seemingly quite out of my depth  :-(

Welcome to the club! It's a big pool and none have swum its entire length, breadth and depth, although there are many in here who have done a lot of laps and ...I just ran out of analogy but you get the idea. I hope you have seen the Getting Started sticky thread, there's a lot of info and links to good resources in there. You have a big head start on most people new to .NET - you have already done battle with the AutoCAD API.

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: Modeless Dialog Box - StartPosition help
« Reply #6 on: August 19, 2014, 06:50:44 PM »
currently I'm swimming in a pool of new terminology and seemingly quite out of my depth  :-(

Welcome to the club! It's a big pool and none have swum its entire length, breadth and depth, although there are many in here who have done a lot of laps and ...I just ran out of analogy but you get the idea. I hope you have seen the Getting Started sticky thread, there's a lot of info and links to good resources in there. You have a big head start on most people new to .NET - you have already done battle with the AutoCAD API.

My favorite analogy: http://www.theswamp.org/index.php?topic=39134.msg443523#msg443523   :lol:
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}