Preventing new form from stealing focus

M

M O J O

Hi,

I need to create a popup form what will not steal focus. I've searched
the net and I managed to put some code together.

Below is the result of my code, but I have a single question .... how
can I prevent form2 to show in taskbar? I've tried to set the
ShowInTaskBar to false, but then an exception is thrown.

Here's my code:

Form1 (master form):

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As_
System.EventArgs) Handles Button1.Click
Dim frm2 As New Form2
frm2.Show()
End Sub

Form2 (popup form):

Protected Overrides ReadOnly Property CreateParams() As_
System.Windows.Forms.CreateParams
Get
Dim cp As CreateParams = MyBase.CreateParams
cp.Style = cp.Style Or &H56000000
Return cp
End Get
End Property

How can I prevent the form to be shown in the taskbar?

Thank you in advance!!

M O J O
 
O

One Handed Man [ OHM ]

Set it to minimized.

Regards - OHM#

==============

Hi,

I need to create a popup form what will not steal focus. I've searched
the net and I managed to put some code together.

Below is the result of my code, but I have a single question .... how
can I prevent form2 to show in taskbar? I've tried to set the
ShowInTaskBar to false, but then an exception is thrown.

Here's my code:

Form1 (master form):

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As_
System.EventArgs) Handles Button1.Click
Dim frm2 As New Form2
frm2.Show()
End Sub

Form2 (popup form):

Protected Overrides ReadOnly Property CreateParams() As_
System.Windows.Forms.CreateParams
Get
Dim cp As CreateParams = MyBase.CreateParams
cp.Style = cp.Style Or &H56000000
Return cp
End Get
End Property

How can I prevent the form to be shown in the taskbar?

Thank you in advance!!

M O J O

Best Regards - OHMBest Regards - OHM (e-mail address removed)
 
M

M O J O

I do not understand how setting my form to minimized should prevent it
from taken focus nor showing in the task bar.

Please explain.

Thank you in advance.

M O J O
 
H

Herfried K. Wagner [MVP]

* M O J O said:
Below is the result of my code, but I have a single question .... how
can I prevent form2 to show in taskbar? I've tried to set the
ShowInTaskBar to false, but then an exception is thrown.

Which exception?
 
O

One Handed Man [ OHM ]

Actually, I appear to have had a mental abberation, I answered your question
without due thought, I appologise. Having thought about it though. Could you
know use the form load even to shift focus back to the parent form ?



OHM#
I do not understand how setting my form to minimized should prevent it
from taken focus nor showing in the task bar.

Please explain.

Thank you in advance.

M O J O

Regards - OHM# (e-mail address removed)
 
M

M O J O

Dear Herfried,

I couldn't provoke the exception now, don't know why I got an exception now.

If I set ShowInTaskBar to true, then focus is lost from the form1 and
form2 is never shown.

Any ideas??

Thanks!
M O J O
 
O

One Handed Man [ OHM ]

I've tried your code and I dont get and exception. Not does the form appear
in either the task bark or on screen

Regards OHM#

Hi,

I need to create a popup form what will not steal focus. I've searched
the net and I managed to put some code together.

Below is the result of my code, but I have a single question .... how
can I prevent form2 to show in taskbar? I've tried to set the
ShowInTaskBar to false, but then an exception is thrown.

Here's my code:

Form1 (master form):

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As_
System.EventArgs) Handles Button1.Click
Dim frm2 As New Form2
frm2.Show()
End Sub

Form2 (popup form):

Protected Overrides ReadOnly Property CreateParams() As_
System.Windows.Forms.CreateParams
Get
Dim cp As CreateParams = MyBase.CreateParams
cp.Style = cp.Style Or &H56000000
Return cp
End Get
End Property

How can I prevent the form to be shown in the taskbar?

Thank you in advance!!

M O J O

Regards - OHM# (e-mail address removed)
 
P

Peter Huang

Hi,

I think you may need to set the owner the form5, since form will have the
WS_CHILD style.

So you sould set the form5's owner as MainForm, this will restrict the
form5 to move inside the mainForm, but the mainform will not lost the focus.
Or you can set the form5's owner to the Desktop, this will let the form5 to
move around the desktop, but when you move the form5, the mainform will
lost the focus.

[MainForm]
Public Declare Function SetParent Lib "user32" Alias "SetParent" (ByVal
hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As Integer
Public Declare Function GetDesktopWindow Lib "user32" Alias
"GetDesktopWindow" () As IntPtr
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim frm As New Form5
SetParent(frm.Handle, GetDesktopWindow())
frm.ShowInTaskbar = False
frm.Show()
End Sub

[MainForm]
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim frm As New Form5
frm.Owner = Me
frm.ShowInTaskbar = False
frm.Show()
End Sub

[form5]
Protected Overrides ReadOnly Property CreateParams() As
System.Windows.Forms.CreateParams
Get
Dim cp As CreateParams = MyBase.CreateParams
cp.Style = cp.Style Or &H56000000
Return cp
End Get
End Property

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Me.Close()
End Sub

If you have any concern on this issue, please post here.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
M

M O J O

Hi Peter,

I've tried to set the forms owner to Desktop, but then it doesn't show.
Hmmm strange.

I imagine that a combo box's dropdown is a form with a listbox on it.
But if I click the listbox, the dropdown form does not take focus. This
is kinda what I'm trying to archive.

Any other solutions?

Thanks!!!!!!

M O J O
 
P

Peter Huang

Hi Mojo,

I want to make a clarify that the setparent API, is used to set the parent
of a window, not the owner. And I did not make a clear test on my code.

Here is an article about parent and owner of a window.
http://support.dundas.com/Articles/ArticleBody.asp?ArticleID=577

It seems that you want to make an app like the combo box.

I think you may try to set the form's style as follows.
Protected Overrides ReadOnly Property CreateParams() As
System.Windows.Forms.CreateParams
Get
Dim cp As CreateParams = MyBase.CreateParams
cp.Style = cp.Style Or &H40000000
cp.ExStyle = cp.ExStyle Or &H8000000
cp.ExStyle = cp.ExStyle And (Not &H40000)
Return cp
End Get
End Property

I also has sent a sample to you.

If you have any concern on this issue please post here.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
M

M O J O

Dear Peter,

Than you for your example, but it doesn't work :blush:( ....

It seams like this line 'cp.Style = cp.Style Or &H40000000' blocks the
listbox, so I cannot chose ane values.

Any ideas??

Thanks again! :blush:)

M O J O
 
P

Peter Huang

Hi Mojo,

You may try to handle the MouseMove and Click event of listbox.

Private Sub ListBox1_MouseMove(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseMove
ListBox1.SelectedIndex = ListBox1.IndexFromPoint(New Point(e.X,
e.Y))
End Sub

Private Sub ListBox1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles ListBox1.Click
Console.WriteLine(ListBox1.SelectedItem)
End Sub

Since the ListBox can not get focus, the stuff should be down by yourself.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top