Form.ShowDialog() messing with location and size
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Form.ShowDialog() messing with location and size

  1. #1
    Join Date
    Nov 2011
    Posts
    47

    Form.ShowDialog() messing with location and size

    Why is it that when I call ShowDialog() on my form, it resizes the form and relocates it to a seemingly random size and location? I don't seem to have any control over it.

    I can gain some control over the size of the form if I attempt to reset its location as follows:

    DataSourceForm dataSourceForm = new DataSourceForm();
    dataSourceForm.ShowDialog();
    dataSourceForm.Location = new System.Drawing.Point(
    this.Location.X + DataSourceForm.OFFSET_X, this.Location.Y + DataSourceForm.OFFSET_Y);

    where "this" refers to a parent dialog box (so I'm positioning my form relative to the parent).

    But this, ironically, restores the size of my form as seen in the Visual Studios designer but does not set its location. It would be nice if ShowDialog didn't take control of the size or the location so that I can set both in either the designer or in the constructor without worry that they will change later on.

    How can I do this?

  2. #2
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,451

    Re: Form.ShowDialog() messing with location and size

    Showdialog() does not do anything to the size and location. What it does do is STOP the calling code and waits until the dialog form is closed before continuing code execution.

    Try this.

    DataSourceForm dataSourceForm = new DataSourceForm();
    dataSourceForm.Location = new System.Drawing.Point(
    this.Location.X + DataSourceForm.OFFSET_X, this.Location.Y + DataSourceForm.OFFSET_Y);
    dataSourceForm.ShowDialog();

  3. #3
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Form.ShowDialog() messing with location and size

    That won't work sotoasty. In order to manually specify a start location you must first set StartPosition to Manual and then set the Location property.

    It is not messing with your size however. It is simply using the default size for your form.

    However, as sotoasty said, your code is wrong. The call to dataSourceForm.ShowDialog(); is blocking, so the next N lines of code will not execute until the child form is closed.
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

  4. #4
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,451

    Re: Form.ShowDialog() messing with location and size

    BigEd781,
    Of course you are absolutely correct about the StartPosition Property. I know it is bad form, but I assumed that if someone was wanting to set the location, that they would have set that to Manual when designing the form. I have only been here for like 10 years, so I should know better by now.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center