ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Complex focus problem (https://www.excelbanter.com/excel-programming/338495-complex-focus-problem.html)

RB Smissaert

Complex focus problem
 
Have a modeless userform that can load a modal userform.
This modeless userform behaves like an application through some API code.
Amongst other things it means an icon will show in the taskbar.
A problem arises when the modeless userform loads a modal form, another
application gets activated, say Outlook and then the icon of the modeless
form gets clicked.
What happens then is that the modeless userform is on top of the modal form,
but the modeless form can't get the focus as this is in the modal form. So
you are stuck. You can get out of this with Alt + tab though.
Other than making the modal form modeless, which is no good solution, what
would be the best approach to avoid this problem?
Thanks for any advice.

RBS


Peter T

Complex focus problem
 
Hi Bart,

I'm probably missing something but maybe something like

' in the modeless form
Me.hide
Userform2.show vbmodal
' mbFlag = true
Me.show
' mbFlag = false

Might need to temporarily set a modular boolean to prevent any unwanted code
running in the modeless form's activate event.

Regards,
Peter T


"RB Smissaert" wrote in message
...
Have a modeless userform that can load a modal userform.
This modeless userform behaves like an application through some API code.
Amongst other things it means an icon will show in the taskbar.
A problem arises when the modeless userform loads a modal form, another
application gets activated, say Outlook and then the icon of the modeless
form gets clicked.
What happens then is that the modeless userform is on top of the modal

form,
but the modeless form can't get the focus as this is in the modal form. So
you are stuck. You can get out of this with Alt + tab though.
Other than making the modal form modeless, which is no good solution, what
would be the best approach to avoid this problem?
Thanks for any advice.

RBS




RB Smissaert

Complex focus problem
 
Hi Peter,


The thing is that I don't want to hide the modeless form.
I think I have solved this though with some simple API code:

Private Declare Function FindWindow _
Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

Private Declare Function SetWindowLong _
Lib "user32" Alias _
"SetWindowLongA" (ByVal hwnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As
Long

Private Const GWL_HWNDPARENT As Long = -8


Sub SetFormParent(strFormCaption As String, _
Optional strParentFormCaption As String = "")

Dim hWndForm As Long
Dim hWndParentForm As Long

hWndForm = FindWindow(vbNullString, strFormCaption) 'the modal form

If Len(strParentFormCaption) = 0 Then
hWndParentForm = FindWindow(vbNullString, MainForm.Caption) 'the
modeless form
Else
hWndParentForm = FindWindow(vbNullString, strParentFormCaption)
'the modeless form
End If

SetWindowLong hWndForm, GWL_HWNDPARENT, hWndParentForm

End Sub

Then in the Initialize event of the modal form I do:

SetFormParent Me.Caption

It has the slight side-effect that if you click the modeless form when the
modal form is loaded the modal
form will flash a few times, but this is no problem. It does solve the
problem of the modal being hidden
behind the modeless form when clicking the taskbar icon of the modeless
form.
So, I think this is OK now.
As I have quite a few forms where I have to do this, it is nice that is only
one line of code extra.


RBS




"Peter T" <peter_t@discussions wrote in message
...
Hi Bart,

I'm probably missing something but maybe something like

' in the modeless form
Me.hide
Userform2.show vbmodal
' mbFlag = true
Me.show
' mbFlag = false

Might need to temporarily set a modular boolean to prevent any unwanted
code
running in the modeless form's activate event.

Regards,
Peter T


"RB Smissaert" wrote in message
...
Have a modeless userform that can load a modal userform.
This modeless userform behaves like an application through some API code.
Amongst other things it means an icon will show in the taskbar.
A problem arises when the modeless userform loads a modal form, another
application gets activated, say Outlook and then the icon of the modeless
form gets clicked.
What happens then is that the modeless userform is on top of the modal

form,
but the modeless form can't get the focus as this is in the modal form.
So
you are stuck. You can get out of this with Alt + tab though.
Other than making the modal form modeless, which is no good solution,
what
would be the best approach to avoid this problem?
Thanks for any advice.

RBS





Peter T

Complex focus problem
 
That works very well. Only thing I'm confused about, you say -

It has the slight side-effect that if you click the modeless form when the
modal form is loaded the modal
form will flash a few times,


I cannot replicate that problem, because if the modal form is loaded and is
the front window, cannot click anything outside of the form. Ie, cannot
click the modeless form.

Regards,
Peter T

"RB Smissaert" wrote in message
...
Hi Peter,


The thing is that I don't want to hide the modeless form.
I think I have solved this though with some simple API code:

Private Declare Function FindWindow _
Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

Private Declare Function SetWindowLong _
Lib "user32" Alias _
"SetWindowLongA" (ByVal hwnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As
Long

Private Const GWL_HWNDPARENT As Long = -8


Sub SetFormParent(strFormCaption As String, _
Optional strParentFormCaption As String = "")

Dim hWndForm As Long
Dim hWndParentForm As Long

hWndForm = FindWindow(vbNullString, strFormCaption) 'the modal form

If Len(strParentFormCaption) = 0 Then
hWndParentForm = FindWindow(vbNullString, MainForm.Caption)

'the
modeless form
Else
hWndParentForm = FindWindow(vbNullString, strParentFormCaption)
'the modeless form
End If

SetWindowLong hWndForm, GWL_HWNDPARENT, hWndParentForm

End Sub

Then in the Initialize event of the modal form I do:

SetFormParent Me.Caption

It has the slight side-effect that if you click the modeless form when the
modal form is loaded the modal
form will flash a few times, but this is no problem. It does solve the
problem of the modal being hidden
behind the modeless form when clicking the taskbar icon of the modeless
form.
So, I think this is OK now.
As I have quite a few forms where I have to do this, it is nice that is

only
one line of code extra.


RBS




"Peter T" <peter_t@discussions wrote in message
...
Hi Bart,

I'm probably missing something but maybe something like

' in the modeless form
Me.hide
Userform2.show vbmodal
' mbFlag = true
Me.show
' mbFlag = false

Might need to temporarily set a modular boolean to prevent any unwanted
code
running in the modeless form's activate event.

Regards,
Peter T


"RB Smissaert" wrote in message
...
Have a modeless userform that can load a modal userform.
This modeless userform behaves like an application through some API

code.
Amongst other things it means an icon will show in the taskbar.
A problem arises when the modeless userform loads a modal form, another
application gets activated, say Outlook and then the icon of the

modeless
form gets clicked.
What happens then is that the modeless userform is on top of the modal

form,
but the modeless form can't get the focus as this is in the modal form.
So
you are stuck. You can get out of this with Alt + tab though.
Other than making the modal form modeless, which is no good solution,
what
would be the best approach to avoid this problem?
Thanks for any advice.

RBS







RB Smissaert

Complex focus problem
 
Peter,

You can if the modal form is smaller than the modeless form and doesn't
cover it all.
This is the situation with all my forms.

RBS

"Peter T" <peter_t@discussions wrote in message
...
That works very well. Only thing I'm confused about, you say -

It has the slight side-effect that if you click the modeless form when
the
modal form is loaded the modal
form will flash a few times,


I cannot replicate that problem, because if the modal form is loaded and
is
the front window, cannot click anything outside of the form. Ie, cannot
click the modeless form.

Regards,
Peter T

"RB Smissaert" wrote in message
...
Hi Peter,


The thing is that I don't want to hide the modeless form.
I think I have solved this though with some simple API code:

Private Declare Function FindWindow _
Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

Private Declare Function SetWindowLong _
Lib "user32" Alias _
"SetWindowLongA" (ByVal hwnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long)
As
Long

Private Const GWL_HWNDPARENT As Long = -8


Sub SetFormParent(strFormCaption As String, _
Optional strParentFormCaption As String = "")

Dim hWndForm As Long
Dim hWndParentForm As Long

hWndForm = FindWindow(vbNullString, strFormCaption) 'the modal
form

If Len(strParentFormCaption) = 0 Then
hWndParentForm = FindWindow(vbNullString, MainForm.Caption)

'the
modeless form
Else
hWndParentForm = FindWindow(vbNullString, strParentFormCaption)
'the modeless form
End If

SetWindowLong hWndForm, GWL_HWNDPARENT, hWndParentForm

End Sub

Then in the Initialize event of the modal form I do:

SetFormParent Me.Caption

It has the slight side-effect that if you click the modeless form when
the
modal form is loaded the modal
form will flash a few times, but this is no problem. It does solve the
problem of the modal being hidden
behind the modeless form when clicking the taskbar icon of the modeless
form.
So, I think this is OK now.
As I have quite a few forms where I have to do this, it is nice that is

only
one line of code extra.


RBS




"Peter T" <peter_t@discussions wrote in message
...
Hi Bart,

I'm probably missing something but maybe something like

' in the modeless form
Me.hide
Userform2.show vbmodal
' mbFlag = true
Me.show
' mbFlag = false

Might need to temporarily set a modular boolean to prevent any unwanted
code
running in the modeless form's activate event.

Regards,
Peter T


"RB Smissaert" wrote in message
...
Have a modeless userform that can load a modal userform.
This modeless userform behaves like an application through some API

code.
Amongst other things it means an icon will show in the taskbar.
A problem arises when the modeless userform loads a modal form,
another
application gets activated, say Outlook and then the icon of the

modeless
form gets clicked.
What happens then is that the modeless userform is on top of the modal
form,
but the modeless form can't get the focus as this is in the modal
form.
So
you are stuck. You can get out of this with Alt + tab though.
Other than making the modal form modeless, which is no good solution,
what
would be the best approach to avoid this problem?
Thanks for any advice.

RBS








RB Smissaert

Complex focus problem
 
There still is a slight problem when there is a message box rather than a
modal form.
Just will have to do an Alt + Tab then.

RBS


"Peter T" <peter_t@discussions wrote in message
...
That works very well. Only thing I'm confused about, you say -

It has the slight side-effect that if you click the modeless form when
the
modal form is loaded the modal
form will flash a few times,


I cannot replicate that problem, because if the modal form is loaded and
is
the front window, cannot click anything outside of the form. Ie, cannot
click the modeless form.

Regards,
Peter T

"RB Smissaert" wrote in message
...
Hi Peter,


The thing is that I don't want to hide the modeless form.
I think I have solved this though with some simple API code:

Private Declare Function FindWindow _
Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

Private Declare Function SetWindowLong _
Lib "user32" Alias _
"SetWindowLongA" (ByVal hwnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long)
As
Long

Private Const GWL_HWNDPARENT As Long = -8


Sub SetFormParent(strFormCaption As String, _
Optional strParentFormCaption As String = "")

Dim hWndForm As Long
Dim hWndParentForm As Long

hWndForm = FindWindow(vbNullString, strFormCaption) 'the modal
form

If Len(strParentFormCaption) = 0 Then
hWndParentForm = FindWindow(vbNullString, MainForm.Caption)

'the
modeless form
Else
hWndParentForm = FindWindow(vbNullString, strParentFormCaption)
'the modeless form
End If

SetWindowLong hWndForm, GWL_HWNDPARENT, hWndParentForm

End Sub

Then in the Initialize event of the modal form I do:

SetFormParent Me.Caption

It has the slight side-effect that if you click the modeless form when
the
modal form is loaded the modal
form will flash a few times, but this is no problem. It does solve the
problem of the modal being hidden
behind the modeless form when clicking the taskbar icon of the modeless
form.
So, I think this is OK now.
As I have quite a few forms where I have to do this, it is nice that is

only
one line of code extra.


RBS




"Peter T" <peter_t@discussions wrote in message
...
Hi Bart,

I'm probably missing something but maybe something like

' in the modeless form
Me.hide
Userform2.show vbmodal
' mbFlag = true
Me.show
' mbFlag = false

Might need to temporarily set a modular boolean to prevent any unwanted
code
running in the modeless form's activate event.

Regards,
Peter T


"RB Smissaert" wrote in message
...
Have a modeless userform that can load a modal userform.
This modeless userform behaves like an application through some API

code.
Amongst other things it means an icon will show in the taskbar.
A problem arises when the modeless userform loads a modal form,
another
application gets activated, say Outlook and then the icon of the

modeless
form gets clicked.
What happens then is that the modeless userform is on top of the modal
form,
but the modeless form can't get the focus as this is in the modal
form.
So
you are stuck. You can get out of this with Alt + tab though.
Other than making the modal form modeless, which is no good solution,
what
would be the best approach to avoid this problem?
Thanks for any advice.

RBS








Peter T

Complex focus problem
 
I can't add anything useful, but doesn't a message box always appear first
on top. If user then Alt-tabs he should know how to get back again.

Regards,
Peter T

"RB Smissaert" wrote in message
...
There still is a slight problem when there is a message box rather than a
modal form.
Just will have to do an Alt + Tab then.

RBS


"Peter T" <peter_t@discussions wrote in message
...
That works very well. Only thing I'm confused about, you say -

It has the slight side-effect that if you click the modeless form when
the
modal form is loaded the modal
form will flash a few times,


I cannot replicate that problem, because if the modal form is loaded and
is
the front window, cannot click anything outside of the form. Ie, cannot
click the modeless form.

Regards,
Peter T

"RB Smissaert" wrote in message
...
Hi Peter,


The thing is that I don't want to hide the modeless form.
I think I have solved this though with some simple API code:

Private Declare Function FindWindow _
Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

Private Declare Function SetWindowLong _
Lib "user32" Alias _
"SetWindowLongA" (ByVal hwnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long)
As
Long

Private Const GWL_HWNDPARENT As Long = -8


Sub SetFormParent(strFormCaption As String, _
Optional strParentFormCaption As String = "")

Dim hWndForm As Long
Dim hWndParentForm As Long

hWndForm = FindWindow(vbNullString, strFormCaption) 'the modal
form

If Len(strParentFormCaption) = 0 Then
hWndParentForm = FindWindow(vbNullString, MainForm.Caption)

'the
modeless form
Else
hWndParentForm = FindWindow(vbNullString, strParentFormCaption)
'the modeless form
End If

SetWindowLong hWndForm, GWL_HWNDPARENT, hWndParentForm

End Sub

Then in the Initialize event of the modal form I do:

SetFormParent Me.Caption

It has the slight side-effect that if you click the modeless form when
the
modal form is loaded the modal
form will flash a few times, but this is no problem. It does solve the
problem of the modal being hidden
behind the modeless form when clicking the taskbar icon of the modeless
form.
So, I think this is OK now.
As I have quite a few forms where I have to do this, it is nice that is

only
one line of code extra.


RBS




"Peter T" <peter_t@discussions wrote in message
...
Hi Bart,

I'm probably missing something but maybe something like

' in the modeless form
Me.hide
Userform2.show vbmodal
' mbFlag = true
Me.show
' mbFlag = false

Might need to temporarily set a modular boolean to prevent any

unwanted
code
running in the modeless form's activate event.

Regards,
Peter T


"RB Smissaert" wrote in message
...
Have a modeless userform that can load a modal userform.
This modeless userform behaves like an application through some API

code.
Amongst other things it means an icon will show in the taskbar.
A problem arises when the modeless userform loads a modal form,
another
application gets activated, say Outlook and then the icon of the

modeless
form gets clicked.
What happens then is that the modeless userform is on top of the

modal
form,
but the modeless form can't get the focus as this is in the modal
form.
So
you are stuck. You can get out of this with Alt + tab though.
Other than making the modal form modeless, which is no good

solution,
what
would be the best approach to avoid this problem?
Thanks for any advice.

RBS










RB Smissaert

Complex focus problem
 
Yes, Alt + Tab will always get you out, but some people don't know about
this.
I can't see a way to fix this with API or anything else.
It will just have to be mentioned in the help.

RBS

"Peter T" <peter_t@discussions wrote in message
...
I can't add anything useful, but doesn't a message box always appear first
on top. If user then Alt-tabs he should know how to get back again.

Regards,
Peter T

"RB Smissaert" wrote in message
...
There still is a slight problem when there is a message box rather than a
modal form.
Just will have to do an Alt + Tab then.

RBS


"Peter T" <peter_t@discussions wrote in message
...
That works very well. Only thing I'm confused about, you say -

It has the slight side-effect that if you click the modeless form when
the
modal form is loaded the modal
form will flash a few times,

I cannot replicate that problem, because if the modal form is loaded
and
is
the front window, cannot click anything outside of the form. Ie, cannot
click the modeless form.

Regards,
Peter T

"RB Smissaert" wrote in message
...
Hi Peter,


The thing is that I don't want to hide the modeless form.
I think I have solved this though with some simple API code:

Private Declare Function FindWindow _
Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

Private Declare Function SetWindowLong _
Lib "user32" Alias _
"SetWindowLongA" (ByVal hwnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As
Long)
As
Long

Private Const GWL_HWNDPARENT As Long = -8


Sub SetFormParent(strFormCaption As String, _
Optional strParentFormCaption As String = "")

Dim hWndForm As Long
Dim hWndParentForm As Long

hWndForm = FindWindow(vbNullString, strFormCaption) 'the modal
form

If Len(strParentFormCaption) = 0 Then
hWndParentForm = FindWindow(vbNullString, MainForm.Caption)
'the
modeless form
Else
hWndParentForm = FindWindow(vbNullString,
strParentFormCaption)
'the modeless form
End If

SetWindowLong hWndForm, GWL_HWNDPARENT, hWndParentForm

End Sub

Then in the Initialize event of the modal form I do:

SetFormParent Me.Caption

It has the slight side-effect that if you click the modeless form when
the
modal form is loaded the modal
form will flash a few times, but this is no problem. It does solve the
problem of the modal being hidden
behind the modeless form when clicking the taskbar icon of the
modeless
form.
So, I think this is OK now.
As I have quite a few forms where I have to do this, it is nice that
is
only
one line of code extra.


RBS




"Peter T" <peter_t@discussions wrote in message
...
Hi Bart,

I'm probably missing something but maybe something like

' in the modeless form
Me.hide
Userform2.show vbmodal
' mbFlag = true
Me.show
' mbFlag = false

Might need to temporarily set a modular boolean to prevent any

unwanted
code
running in the modeless form's activate event.

Regards,
Peter T


"RB Smissaert" wrote in message
...
Have a modeless userform that can load a modal userform.
This modeless userform behaves like an application through some API
code.
Amongst other things it means an icon will show in the taskbar.
A problem arises when the modeless userform loads a modal form,
another
application gets activated, say Outlook and then the icon of the
modeless
form gets clicked.
What happens then is that the modeless userform is on top of the

modal
form,
but the modeless form can't get the focus as this is in the modal
form.
So
you are stuck. You can get out of this with Alt + tab though.
Other than making the modal form modeless, which is no good

solution,
what
would be the best approach to avoid this problem?
Thanks for any advice.

RBS












All times are GMT +1. The time now is 10:03 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com