dcsimg
Results 1 to 13 of 13

Thread: IsModal: function to know whether we are currently modally

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    2,009

    IsModal: function to know whether we are currently modally

    This function will tell whether the application is currently showing a form modally or not.

    The idea is to try to show a form and see if there is an error or not.

    For that, add a form and name it frmModalCheck.

    BorderStyle: 0 - None
    Caption: ""

    Code for the form:

    Code:
    Private Sub Form_Load()
        Me.Move -Screen.TwipsPerPixelX, -Screen.TwipsPerPixelY, Screen.TwipsPerPixelX, Screen.TwipsPerPixelY
        On Error Resume Next
        Me.Show
        If Err.Number = 0 Then
            Unload Me
        End If
    End Sub
    In a bas module:

    Code:
    Public Function IsModal() As Boolean
        On Error Resume Next
        Load frmModalCheck
        IsModal = Err.Number = 401
        If IsModal Then Unload frmModalCheck
        On Error GoTo 0
        Set frmModalCheck = Nothing
    End Function
    Attached Files Attached Files
    Last edited by Eduardo-; Jan 22nd, 2020 at 11:04 AM.

  2. #2
    Hyperactive Member
    Join Date
    Mar 2018
    Posts
    369

    Re: IsModal: function to know whether we are currently modally

    I've always used (GetWindowLong(GWL_STYLE) & WS_DISABLED) to check for modality but like your solution better.

  3. #3

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    2,009

    Re: IsModal: function to know whether we are currently modally

    Quote Originally Posted by wqweto View Post
    Is there a problem with App.NonModalAllowed? Under what conditions does it fail for you?

    cheers,
    </wqw>
    --->
    Attached Files Attached Files

  5. #5
    Frenzied Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    1,720

    Re: IsModal: function to know whether we are currently modally

    Yikes!

    > If you are lucky, you may find the App.NonModalAllowed property which sounds
    > like it will solve all the problems. Unfortunately, it isn't quite this simple...
    >
    > App.NonModalAllowed will ONLY return false when working in an ActiveX/COM/MTS
    > environment, and will always be true when in a standard EXE environment.

    http://hashvb.earlsoft.co.uk/Modal_dialogs

    cheers,
    </wqw>

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    2,009

    Re: IsModal: function to know whether we are currently modally

    Quote Originally Posted by wqweto View Post
    Yikes!

    > If you are lucky, you may find the App.NonModalAllowed property which sounds
    > like it will solve all the problems. Unfortunately, it isn't quite this simple...
    >
    > App.NonModalAllowed will ONLY return false when working in an ActiveX/COM/MTS
    > environment, and will always be true when in a standard EXE environment.

    http://hashvb.earlsoft.co.uk/Modal_dialogs

    cheers,
    </wqw>
    It doesn't work for me in an ocx project either. I'll post a sample later.
    Last edited by Eduardo-; Jan 22nd, 2020 at 10:35 AM.

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    2,009

    Re: IsModal: function to know whether we are currently modally

    Quote Originally Posted by Eduardo- View Post
    It doesn't work for me in an ocx project either. I'll post a sample later.
    --->
    Attached Files Attached Files
    Last edited by Eduardo-; Jan 22nd, 2020 at 12:37 PM.

  8. #8
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    5,984

    Re: IsModal: function to know whether we are currently modally

    It'd sure be nice to do this without needing another form dangling around. I thought about trying to create a top-level window with an API call, but I'm convinced that I could do that regardless of whether I'm modal or not, as that would just completely circumvent anything in VB6.

    If error trapping can figure it out, there's got to be some flag (or counter) in the bowels of our program. I'm a bit surprised that NonModalAllowed doesn't work. That seems like an outright bug.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    2,009

    Re: IsModal: function to know whether we are currently modally

    Quote Originally Posted by Elroy View Post
    It'd sure be nice to do this without needing another form dangling around.
    Yes, I agree, but at least for me it isn't a problem anyway.

    Quote Originally Posted by Elroy View Post
    If error trapping can figure it out, there's got to be some flag (or counter) in the bowels of our program.
    I'm not sure what you mean.
    If you mean to keep the error information on the Err object, here is the code for the bas module:

    Code:
    Private mErr_Description As String
    Private mErr_HelpContext As Long
    Private mErr_HelpFile As String
    Private mErr_Number As Long
    Private mErr_Source As String
    
    Public Function IsModal() As Boolean
        SaveErr
        On Error Resume Next
        Load frmModalCheck
        IsModal = Err.Number = 401
        If IsModal Then Unload frmModalCheck
        On Error GoTo 0
        Set frmModalCheck = Nothing
        RestoreErr
    End Function
    
    Private Sub SaveErr()
        mErr_Description = Err.Description
        mErr_HelpContext = Err.HelpContext
        mErr_HelpFile = Err.HelpFile
        mErr_Number = Err.Number
        mErr_Source = Err.Source
    End Sub
    
    Private Sub RestoreErr()
        ' LastDllError is lost
        Err.Description = mErr_Description
        Err.HelpContext = mErr_HelpContext
        Err.HelpFile = mErr_HelpFile
        Err.Number = mErr_Number
        Err.Source = mErr_Source
    End Sub
    Quote Originally Posted by Elroy View Post
    I'm a bit surprised that NonModalAllowed doesn't work. That seems like an outright bug.
    Yes.
    Last edited by Eduardo-; Jan 22nd, 2020 at 01:16 PM.

  10. #10
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    5,984

    Re: IsModal: function to know whether we are currently modally

    Hmm, I tried using a MessageBox API call with a hook and a timer, and then I tried hiding and/or disabling the messagebox. That didn't work. Basically, all it did was freeze the program because I could no longer click the messagebox to make my program continue.

    So, at least in my mind, that shows that this is a VB6 thing, and not something to do with the Windows OS.

    Quote Originally Posted by Elroy View Post
    If error trapping can figure it out, there's got to be some flag (or counter) in the bowels of our program.
    I'm just trying to say that, VB6 certainly knows when we're trying to show a non-modal form when a modal form is already showing. That's when it throws the error 401. How does it know to do that? In other words, there's got to be something that the VB6 runtime examines to figure out that it should throw that error. At first glance, you'd think it'd be the same thing that NonModalAllowed looks at, but apparently not.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  11. #11
    Frenzied Member
    Join Date
    Jun 2012
    Posts
    1,385

    Re: IsModal: function to know whether we are currently modally


  12. #12

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    2,009

    Re: IsModal: function to know whether we are currently modally

    Quote Originally Posted by Elroy View Post
    If error trapping can figure it out, there's got to be some flag (or counter)...
    Ah, I understood now. Yes, I guess it must be keeping a list of the forms that are opened modally.

  13. #13
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    5,984

    Re: IsModal: function to know whether we are currently modally

    Quote Originally Posted by Krool View Post
    Hi Krool,

    Yeah, I think most of us already knew about that one. However, as they outlined, the following line anywhere in our project trips it up:

    Code:
    
    Me.Enabled = False
    
    Personally, I was looking for a more "pure" solution. But really, it's just an academic exercise for me.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width