Results 1 to 4 of 4

Thread: [RESOLVED] Modal and Modeless dialogs

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    Resolved [RESOLVED] Modal and Modeless dialogs

    A painful set of questions but I’m hoping someone can answer them.
    This is in AutoCAD so maybe it behaves differently.

    I’m trying to understand if modal and modeless created forms are duplicated.
    And why the creation of each results in different events being triggered.

    I define the form when starting the project.
    Public FormModal As New frmModal

    I show it using
    Application.ShowModalDialog(Application.MainWindow.Handle, FormModal, False)

    Each time it’s called frmTest_Load and frmTest_Shown are called.

    Q1. Does this create a new form each time it’s called?
    Q2. Why is frmTest_Load triggered each time if the form is already loaded?
    Q3 I can programmatically hide it but if I manually close it does it close instead of hide?

    With modeless forms
    Application.ShowModelessDialog(FormModeless)
    FormModeless.Hide()
    frmTest_Load and frmTest_Shown are called for the first load but neither are called the second time.

    Q4 Why are frmTest_Load or frmTest_Shown not triggered on the second loading
    Last edited by sgrya1; Oct 9th, 2021 at 01:48 AM.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: Modal and Modeless dialogs

    AutoCAD may be different, and I have no experience with it, but this is the way it would likely work:

    Every time you call New AnyForm, you are creating a new instance. For example, when you wrote this:

    Public FormModal As New frmModal

    you created a new instance of frmModal. When you show that new instance, you should get the load event, but only once. Unfortunately, you then said you show it using:

    Application.ShowModalDialog(Application.MainWindow.Handle, FormTest, False)

    This is a bit unfortunate, because what is FormTest? Is it one of the ones you mentioned before (FormModal or FormModeless), but that doesn't seem right, since you are showing a modal dialog, so it seems like only one of those would make sense.

    After that, the organization of the code matters. If the code you showed is in a button, for example, then you could be running this line:

    Public FormModal As New frmModal

    every time you click the button, so each time you are just creating a new form. If you already had one, that won't matter, because you're creating a new one.
    My usual boring signature: Nothing

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Modal and Modeless dialogs

    Quote Originally Posted by sgrya1 View Post
    A painful set of questions but I’m hoping someone can answer them.
    This is in AutoCAD so maybe it behaves differently.

    I’m trying to understand if modal and modeless created forms are duplicated.
    And why the creation of each results in different events being triggered.

    I define the form when starting the project.
    Public FormModal As New frmModal
    Public FormModeless As New frmModal
    I show it using
    Application.ShowModalDialog(Application.MainWindow.Handle, FormTest, False)

    Each time it’s called frmTest_Load and frmTest_Shown are called.

    Q1. Does this create a new form each time it’s called?
    Q2. Why is frmTest_Load triggered each time if the form is already loaded?
    Q3 I can programmatically hide it but if I manually close it does it close instead of hide?
    1. Yes. Every time the New keyword is used, a new object is created.
    2. The form isn't already loaded. The Load event is raised on the first and only the first occasion that a form is displayed. When you call ShowModalDialog, the form will be displayed. The Load event is raised after you call that method but before the form is actually displayed on-screen.
    3. To "close" something means different things in different contexts. Hiding a modal dialogue simply hides it from view. Closing a modal dialogue will hide the form from view and also return a DialogResult value via the ShowDialog method. Not sure whether your Application.ShowModalDialog method returns something but I suspect that it would. At the very least, it should be able to indicate whether the user clicked an OK or a Cancel button. In .NET, closing a form displayed as a modal dialogue does not dispose it, which means that it can be displayed again if you want to or, otherwise, you need to dispose it yourself. That's why modal dialogue forms should generally be created with a Using statement, to dispose them when you're done, e.g.
    vb.net Code:
    1. Using dialogue As New SomeForm
    2.     If dialogue.ShowDialog() = DialogResult.OK Then
    3.         'The user clicked the OK button.
    4.     End If
    5. End Using
    Quote Originally Posted by sgrya1 View Post
    With modeless forms
    Application.ShowModelessDialog(FormModeless)
    FormModeless.Hide()
    frmTest_Load and frmTest_Shown are called for the first load but neither are called the second time.

    Q4 Why are frmTest_Load or frmTest_Shown not triggered on the second loading
    4. The Load and Shown events are raised immediately before and immediately after a form is displayed for the first time and only the first time respectively. If you display a form, then hide it, then show it again, there will be no Load and Shown events the second time.
    Quote Originally Posted by sgrya1 View Post
    Changing the code to
    Dim FormModeless As New frmModless
    Application.ShowModelessDialog(FormModeless)
    FormModeless.Close()

    frmTest_Load is triggered frmTest_Shown is not

    Q5 Why is frmTest_Shown not triggered
    5. Unless you're mistaken, I would imagine it is because you are closing the form immediately after showing it and, as a result, the form never actually gets displayed properly. When you display a modal dialogue, execution blocks at that point until the dialogue is closed. When you show a modeless dialogue, execution continues without blocking. Showing a form and then immediately closing it makes no sense so how the form behaves in that scenario is a moot point. That said, I suspect that Shown actually gets raised at the same point that Activated does but only the first time. By closing the form immediately, you are probably preventing it ever getting focus and thus ever raising an Activated or Shown event. That's just a theory but it makes sense. What doesn't make sense is closing a form immediately after showing it, so don't do that and then your question becomes irrelevant.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    Re: Modal and Modeless dialogs

    Wow. Thank you both.

    That really does explain everything. I have no more questions.

    And sorry. "FormTest" was a typo and should have been "FormModal".

Posting Permissions

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



Click Here to Expand Forum to Full Width