Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form loses modal property when made invisible 2

Status
Not open for further replies.

JDAtNorthStar

Programmer
Oct 24, 2001
31
0
0
US
I'm not sure if this is a VB bug or what but here is my problem: I'm creating a form using form.Show vbModal. I later set the visible property to False and eventually back to True. When it becomes visible, it is no longer modal. Why is this? Is there a way to make it modal again after making it visible?

This is an issue because when it isn't modal, it allows the user to switch back to the form that contains the button that showed the form in the first place. If they click the button again, it raises runtime error 400 since the form is already displayed.

Any help would be greatly appreciated.
 
I tried Hide and it didn't change the behavior. Per VB help, the Hide method sets the visible property to False so it makes sense that Hide didn't make a difference. Are you suggesting that I use Hide for other reasons such as performance?
 
vbModal is meant to stop the program from executing while the form is open. When you make it invisible it probably resets it to be non-modal, otherwise you'd never get focus back to your main form. It's probably a safeguard MS put in to keep you from having a program hang and not being able to get out of it. I'd say the easiest way to solve your problem is to store all pertinent info on the form into variables, then unload the form and reload it again.

There's no chr$(27)!
 
The idea behind a modal form is that the user can only deal with that form until you decide otherwise. To hide it kind of defeats the purpose. Instead I would unload the form instead of hiding it and then load it again when needed. Or don't set it to modal.

I set forms to modal only if they are single purpose forms. Like a search form, option, login or a custom message box form.

Thanks and Good Luck!

zemp
 
Wouldn't you be able to do formx.hide then do
formx.show vbmodal again when you want to make it visible again?
 
What code is in the button that shows the second form? A second firing of Form.Show shouldn't give an error.

If there is something odd in the second form, then simply test if it's loaded before using Form2.Show

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
You can call a form with vbmodal in this form copy code

Private Sub Timer1_Timer()
Screen.ActiveForm.Enabled = True
Form4.Show vbModal
Timer1.Interval = 0
End Sub

Private Sub Command3_Click()
Form4.Hide
Screen.ActiveForm.Enabled = False
Timer1.Interval = 2000
End Sub

I think that's you want
 
I don't know you have found your solution but I can give you other alternative.
You need two forms and a module

then first form frmmodul with text1 textbox and command1 as command

code:
Option Explicit

Private mTransparent As Boolean

Private Sub Command1_Click()
Unload Me
End Sub

Private Sub Form_Load()
FormTransparent Me, Array(Timer1)
mTransparent = True
End Sub

Private Sub Form_Unload(Cancel As Integer)
Timer1.Enabled = False
End Sub

Private Sub Timer1_Timer()
Select Case mTransparent
Case False
FormTransparent Me, Array(Timer1)
mTransparent = True
Case True
FormOpaque Me, Array(Timer1)
mTransparent = False
End Select
End Sub

Second Form frmTest with command1 as command-button

Option Explicit

Private Sub Command1_Click()
frmModal.Show vbModal
MsgBox "Nach dem modalen Anzeigen des Forms ausgeführter Code..."
End Sub


and least the module with

Option Explicit

Private Declare Function CreateRectRgn Lib "gdi32" _
(ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, _
ByVal Y2 As Long) As Long
Private Declare Function SetWindowRgn Lib "user32" _
(ByVal hWnd As Long, ByVal hrgn As Long, _
ByVal Redraw As Boolean) As Long

Public Sub FormTransparent(Form As Form, _
Optional DoNotTouchEnabledControls As Variant)

Dim nRgn As Long

nRgn = CreateRectRgn(0, 0, 0, 0)
SetWindowRgn Form.hWnd, nRgn, True
zEnable False, Form, DoNotTouchEnabledControls
End Sub

Public Sub FormOpaque(Form As Form, _
Optional DoNotTouchEnabledControls As Variant)

Dim nControl As Control
Dim nControls() As Control
Dim i As Integer

zEnable True, Form, DoNotTouchEnabledControls
With Form
SetWindowRgn .hWnd, 0, True
ReDim nControls(0 To .Controls.Count)
On Error Resume Next
For Each nControl In .Controls
Set nControls(nControl.TabIndex) = nControl
Next
End With
Err.Clear
For i = 0 To UBound(nControls)
With nControls(i)
.SetFocus
If Err.Number Then
Err.Clear
Else
Exit For
End If
End With
Next 'i
End Sub

Private Sub zEnable(ByVal Enabled As Boolean, Form As Form, _
Optional DoNotTouchEnabledControls As Variant)

Dim nControl As Control
Dim nControls As Collection
Dim i As Integer
Dim nControlsA() As Control

Set nControls = New Collection
If Not IsMissing(DoNotTouchEnabledControls) Then
For i = LBound(DoNotTouchEnabledControls) To _
UBound(DoNotTouchEnabledControls)
nControls.Add DoNotTouchEnabledControls(i), _
CStr(ObjPtr(DoNotTouchEnabledControls(i)))
Next
End If
On Error Resume Next
With Form
For Each nControl In Form.Controls
nControls.Add nControl, CStr(ObjPtr(nControl))
If Err.Number = 0 Then
nControl.Enabled = Enabled
End If
Err.Clear
Next
.Enabled = Enabled
End With
End Sub


Waiting for your comments

peterguhl@yahoo.de
 
Thanks to everyone for their comments and suggestions. zemp's comment on 8/7 was right on the money so I modified my code to unload the form instead of hiding it.

Jeff
 
So what you're saying is that you had the answer a month ago, and the rest of us were just wasting our time because you didn't bother to come back and tell us. Read faq222-2244 to see the other reasons why you should post acknowledgements in a timely manner

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
johnwm: I apologize for not posting an acknowledgement right away. At the time, it seemed obvious to me that the question was answered but I certainly understand why this is a good practice and I will be sure to immediately post acknowledgements in the future.

Thanks for steering me to the FAQ. I read the entire FAQ and saw some great suggestions. I also found it interesting that items #1 and #2 both begin with "Be nice". Item #2 also includes this statement: "Bad tempered replies don't help anyone". In my judgement, I would say your post didn't fit within the parameters of these suggestions/guidelines.
 
My answer certainly wasn't intended to come over as 'bad-tempered' and I apologise if you saw it that way. It can be rather frustrating for responders, not realising that an issue has been satisfactorily answered, to spend time and effort following up on a thread and finally get an answer 'I got the answer a month ago'. Your last post in August ended with a question, and I didn't realise that this signified a conclusion to the issue.

Now that is all clarified and apologised, welcome to the forum, and we all look forward to your further participation with both questions and answers

[smile]


________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top