-
Nov 25th, 2022, 07:03 PM
#1
Thread Starter
Hyperactive Member
Form show vbmodal and the rest of the sub
Hi friends,
Building a "import" feature and I show a form with a "Waiting" message duroing import process.
Actually, I do like this :
Code:
Private Sub Import_click()
[...]some code
Form2.Show vbModeless, Me
Call SetTopMostWindow(Form2.hWnd, OnTop)
Call ImportProcess(parameters)
End sub
But Will import processing, the user can click everywhere else in the software.
If a I change for :
Code:
Private Sub Import_click()
[...]some code
Form2.Show vbModal
Call ImportProcess(parameters)
End sub
User can not click everywhere else anymore, but Call ImportProcess(parameters) is called only if I close Form2. So, not good.
Is there a way to keep Form2 like vbModal, without pausing the sub ?
Thanks
-
Nov 25th, 2022, 07:59 PM
#2
Re: Form show vbmodal and the rest of the sub
That's how modal windows work.
-
Nov 26th, 2022, 04:40 AM
#3
Addicted Member
Re: Form show vbmodal and the rest of the sub
If don't need to show progress this would be a simply solution:
In the modal form put a timer control, and this code:
Code:
Option Explicit
Public Parameters As Variant
Public ParentObject As Object
Public Process As String
Private Sub Form_Load()
Timer1.Enabled = True
Timer1.Interval = 10
End Sub
Private Sub Timer1_Timer()
'Start Proccess
CallByName ParentObject, Process, VbMethod, Parameters
'the process is terminated, unload modal form
Unload Me
End Sub
And in the main form:
Code:
Private Sub Import_click()
Dim Parameters As Variant
'[...]some code
Dim f As Form2
Set f = New Form2
'Pass the parameters to the other fom
f.Parameters = Parameters
Set f.ParentObject = Me
f.Process = "ImportProcess"
f.Show vbModal, Me
'not needed the other form call the process
'Call ImportProcess(Parameters)
End Sub
ImportProcess must be a public method in the main form and admits only a parameter, but you can adapt it at your needs
-
Nov 26th, 2022, 07:20 AM
#4
Re: Form show vbmodal and the rest of the sub
Try these two sample forms to implement modal progress indication with cancellation
Code:
'--- Form1
Option Explicit
Private WithEvents m_oProgress As Form2
Private Sub Form_Click()
Set m_oProgress = New Form2
m_oProgress.SetProgress "Starting..."
m_oProgress.Show vbModal, Me
End Sub
Private Sub m_oProgress_Start()
pvImport
Unload m_oProgress
Set m_oProgress = Nothing
End Sub
Private Sub pvImport()
Dim cFolders As Collection
Dim lIdx As Long
Dim sFolder As String
Dim sFile As String
Set cFolders = New Collection
cFolders.Add "C:"
Do While lIdx < cFolders.Count
sFolder = cFolders.Item(lIdx + 1)
If Not m_oProgress.SetProgress("Processing " & sFolder & "...") Then
Exit Sub
End If
sFile = vbNullString
On Error Resume Next
sFile = Dir(sFolder & "\*.*", vbDirectory)
On Error GoTo 0
Do While Len(sFile) <> 0
If sFile <> "." And sFile <> ".." Then
sFile = sFolder & "\" & sFile
If (GetAttr(sFile) And vbDirectory) <> 0 Then
cFolders.Add sFile
End If
End If
sFile = Dir
Loop
lIdx = lIdx + 1
Loop
End Sub
Code:
'--- Form2
Option Explicit
Event Start()
Private m_bIsActive As Boolean
Private m_bIsCanceled As Boolean
Public Function SetProgress(sText As String) As Boolean
Label1.Caption = sText
DoEvents
If Not m_bIsCanceled Then
SetProgress = True
End If
End Function
Private Sub Form_Activate()
If Not m_bIsActive Then
m_bIsActive = True
RaiseEvent Start
End If
End Sub
Private Sub Command1_Click()
m_bIsCanceled = True
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If UnloadMode <> vbFormCode Then
m_bIsCanceled = True
Cancel = 1
End If
End Sub
You'll need to put a Command1 and a Label1 on Form2 for indication and cancellation to happen.
Click on Form1 to start "import" process.
This is a sample of the only case when using DoEvents is justified i.e. spinning msg pump to redraw the progress label and accept clicks on cancellation button incl. click on [X] button to unload the form i.e. in order to dismiss the operation altogether it first has to be canceled.
cheers,
</wqw>
Last edited by wqweto; Nov 26th, 2022 at 03:08 PM.
-
Nov 26th, 2022, 12:36 PM
#5
Thread Starter
Hyperactive Member
Re: Form show vbmodal and the rest of the sub
Thanks friends
Have to debug first another point, and I'll try !
-
Nov 27th, 2022, 09:42 PM
#6
Thread Starter
Hyperactive Member
Re: Form show vbmodal and the rest of the sub
Hi,
Tried both codes, running in standalone
As well as progress could be useful, I choose wqweto code
But in a try it in the existing project, with adding the 2 forms, I get a Compil error: Object does not source automation events (I even not understanding what the error means lol) on this line:
Code:
Private WithEvents m_oProgress As Form2
-
Nov 27th, 2022, 10:47 PM
#7
Re: Form show vbmodal and the rest of the sub
-
Nov 28th, 2022, 03:54 AM
#8
Re: Form show vbmodal and the rest of the sub
Originally Posted by wqweto
This is a sample of the only case when using DoEvents is justified i.e. spinning msg pump to redraw the progress label and accept clicks on cancellation button incl. click on [X] button to unload the form i.e. in order to dismiss the operation altogether it first has to be canceled.
@Eduardo: This is a case impl. all safe conditions to call DoEvents i.e. when there is a modal form in this case with a single Cancel button the end-user must interact with.
In this particular case there is no possibility of reentrancy because the top modal form has disable every other (modal or non-modal) form so these cannot be interacted with.
Originally Posted by Couin
But in a try it in the existing project, with adding the 2 forms, I get a Compil error: Object does not source automation events (I even not understanding what the error means lol) on this line:
Code:
Private WithEvents m_oProgress As Form2
There is Event Start() declaration in Form2 in the sample above.
cheers,
</wqw>
-
Nov 28th, 2022, 08:14 AM
#9
Re: Form show vbmodal and the rest of the sub
-
Nov 28th, 2022, 09:27 AM
#10
Re: Form show vbmodal and the rest of the sub
Originally Posted by Eduardo-
You can laugh all that you want but my stand on DoEvents usage remains unchanged -- it is very rarely needed, for newbies almost never.
This is like most beginners try to fix their SQL queries by slapping a HAVING clause which is a case of being beyond clueless. DoEvents is in the same category of cluelessness.
Do you have some other usage for DoEvents in your user-controls? I'll be most interested what you stance is on the subject which you so eagerly try to bring on here.
cheers,
</wqw>
-
Nov 28th, 2022, 10:11 AM
#11
Re: Form show vbmodal and the rest of the sub
Originally Posted by wqweto
Do you have some other usage for DoEvents
Do I need to?
And of course I do.
Originally Posted by wqweto
I'll be most interested what you stance is on the subject which you so eagerly try to bring on here.
cheers,
</wqw>
You already know my stance, or (re)read my old posts.
The point? The point is that you criticized me for using DoEvents (without even taking into account -or knowing at all- how or where I used it).
Joining and also provoking a horde of other ones also criticizing me, based on your reputation (general reputation, what I don't question of course).
At least be coherent and do not use it yourself.
Or anybody that uses DoEvents now is obligated to explain to others why it is well used is his particular case?
Forget it, never mind.
-
Nov 28th, 2022, 10:14 AM
#12
Re: Form show vbmodal and the rest of the sub
Originally Posted by Eduardo-
Do I need to?
And of course I do.
You already know my stance, or (re)read my old posts.
The point? The point is that you criticized me for using DoEvents (without even taking into account -or knowing at all- how or where I used it).
Joining and also provoking a horde of other ones also criticizing me, based on your reputation (general reputation, what I don't question of course).
At least be coherent and do not use it yourself.
Or anybody that uses DoEvents now is obligated to explain to others why it is well used is his particular case?
Forget it, never mind.
I don't remember attacking you personally but my point against DoEvents is in principle -- there are very few cases where one *can* use it.
It's not if one wishes to use it but whether one *can* use it, whether the code becoming reentrant is possible and reentrancy handled at all.
Let me apologize to you if you felt criticized by me -- it was always about the code. It's all about the code, I have nothing against you personally, honestly.
cheers,
</wqw>
Last edited by wqweto; Nov 28th, 2022 at 10:22 AM.
-
Nov 28th, 2022, 10:41 AM
#13
Re: Form show vbmodal and the rest of the sub
Originally Posted by wqweto
I don't remember attacking you personally but my point against DoEvents is in principle -- there are very few cases where one *can* use it.
It's not if one wishes to use it but whether one *can* use it, whether the code becoming reentrant is possible and reentrancy handled at all.
Let me apologize to you if you felt criticized by me -- it was always about the code. It's all about the code, I have nothing against you personally, honestly.
cheers,
</wqw>
I was "surrounded" by several people in that thread because I was using DoEvents, and it served as an excuse for others (better let's not go into details).
I know it is not personal, of course, never thought that.
I guess I somewhat share your view... but not literally as expressed above (in short: in most cases you "can", if you take care of the different situations that can happen -although sometimes are hard to foresee some-)
Anyway IDK if this conversation makes much sense, since I'm quite sure I won't be able to make you change your mind a millimeter on this issue, or on using OERN, or on any issue.
Happy DoEventing!
-
Nov 28th, 2022, 10:58 AM
#14
Re: Form show vbmodal and the rest of the sub
Originally Posted by Eduardo-
Anyway IDK if this conversation makes much sense, since I'm quite sure I won't be able to make you change your mind a millimeter on this issue, or on using OERN, or on any issue.
LOL, I just used DoEvents *and* OERN in a single procedure above :-))
Btw, Dir (and GetAttr) bomb out when file/path does not exist so if one wishes to adhere to the one true Break on All Errors religion they have to stop using these built-in functions and start using API replacements instead.
cheers,
</wqw>
-
Nov 28th, 2022, 11:25 AM
#15
Re: Form show vbmodal and the rest of the sub
Originally Posted by wqweto
LOL, I just used DoEvents *and* OERN in a single procedure above :-))
Really? I see now!!!!!!!!!!!
I didn't read the code... I just saw DoEvents while scrolling the thread.
Originally Posted by wqweto
Btw, Dir (and GetAttr) bomb out when file/path does not exist so if one wishes to adhere to the one true Break on All Errors religion they have to stop using these built-in functions and start using API replacements instead.
cheers,
</wqw>
Yes, I guess you always can replace with something else and avoid OERN entirely, but IMO that complication is not justified.
The same for GoTo, GoSub and may be any other of the "hard rules" (I'm talk for myself, IDK or don't remember your position on those ones).
"Never use End" seems to be the one more justified, but I'm quite sure that could be challenged too.
Talking about how religions "forbids" things, and how the Bible that is not a religion approaches the same things, makes me think on 1 Co 10:23 and 1 Ts 5:21. But that would be a subject for Chit Chat or another section.
-
Nov 29th, 2022, 04:59 PM
#16
Thread Starter
Hyperactive Member
Re: Form show vbmodal and the rest of the sub
Originally Posted by wqweto
There is Event Start() declaration in Form2 in the sample above.
Hi wqweto,
Yes, you are right, but I do not really understand why in the example, it is ok, and when I add the 2 same forms to my project, to just test before trying to apply the method to my other form, I get this error.
Sohuld I remove if from Form 2 in my porject ?
Thanks
-
Nov 29th, 2022, 05:50 PM
#17
Re: Form show vbmodal and the rest of the sub
Man, this thread is confusing to me. Personally, I think of showing a form with vbModal as the same as calling a function ... the line of code where it's done is "paused" until the vbModal form is no longer modal (or the function returns, to complete the analogy).
And, to solve Couin's problem, I don't know why he just doesn't do his work inside the modal form (maybe in the Form_Activate event). To return to the analogy, that'd be like doing your work inside a called function, rather than scratching our head wondering why our code doesn't continue until the function completes and returns. As Dil said, that's the way it works.
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. To all, peace and happiness.
-
Nov 30th, 2022, 03:36 AM
#18
Re: Form show vbmodal and the rest of the sub
Originally Posted by Couin
Hi wqweto,
Yes, you are right, but I do not really understand why in the example, it is ok, and when I add the 2 same forms to my project, to just test before trying to apply the method to my other form, I get this error.
Sohuld I remove if from Form 2 in my porject ?
Thanks
You can use the IDE debugger to trace the code.
First trace the sample code and notice the control flow -- first Show vbModal, then Activate event which rises Start event.
Then trace your implementation to see where the control flow deviates from the sample code.
cheers,
</wqw>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|