473,769 Members | 3,828 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

TopMost problems

Hello,

I have a VB6 app that calls a VB.NET app via named pipes and a third
party component. Inside the VB.NET app I'm trying to force a form to
the top, at least temporarily. Inside a form's public sub I'm doing:

Public Sub ThisIsMySub(The seAreMyArgument s)
Me.Show
Me.WindowState = FormWindowState .Normal
Me.TopMost = True
Me.TopMost = False
End Sub

However, my form doesn't pop on top like I'd expect it to. BTW the form
is an MDIParent.

I wrote a simple proof-of-concept VB.NET application with a timer
control on an MDI Parent form. I do the same steps as above it works as
I'd expect. When the timer_tick event fires my MDIParent form pops on
top. I can then move another app on top of it or minimize my MDIParent
form. The next time the timer_tick event fires it pops back on top.

The only thing that I could figure out is that another form in my app
had it's TopMost property set to True. I have a StatusBar form that
does this, but I did some checking and its unloaded by the time I do my
"Me.TopMost = True
" Any idea what could be going on?

Thanks,
Eric
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05
12 7537
In article <OD************ **@TK2MSFTNGP11 .phx.gbl>, E Goforth wrote:
Hello,

Do the AllowSetForegro undWindow and SetForegroundWi ndow APIs make the
window in the foreground permanently? I just want it to pop to the top
of the z-order, but then allow the user to put other windows on top, if
they want to.

Eric Goforth
Raleigh, NC

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


No they don't make them on top permanently. All they do is let you
steal the focus. Of course, using the
AllowSetForegro undWindow/SetForegroundWi ndow - you are setting it up so
that your second app can steal focus from your first, but it still will
not be able to steal focus from any other app. To make it universal,
you have to use the AttachThreadInp ut method...

I think though that you have another issue going on. Can you remind me
of how your doing the communication - was it with pipes?

--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #11
Hello,

I added the following declarations to my MDIParent form:

---- Begin code ------------------

Private Declare Function GetTopWindow Lib "user32.dll " (ByVal hWnd
As IntPtr) As IntPtr
Private Declare Function SetForegroundWi ndow Lib "user32" (ByVal
hwnd As IntPtr) As IntPtr
Private Declare Function GetWindowText Lib "user32" Alias
"GetWindowTextA " (ByVal hwnd As IntPtr, ByVal lpString As String, ByVal
cch As Long) As Long
Private Declare Function SetWindowPos Lib "user32" Alias
"SetWindowP os" (ByVal hwnd As IntPtr, ByVal hWndInsertAfter As Long,
ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long,
ByVal wFlags As Long) As Long

Private Const HWND_TOP As Integer = 0
Private Const HWND_BOTTOM As Integer = 1
Private Const HWND_TOPMOST As Integer = -1
Private Const HWND_NOTOPMOST As Integer = -2
Private Const SWP_NOSIZE As Long = &H1
Private Const SWP_NOMOVE As Long = &H2

Inside my public sub I have:

Me.Show()
Me.WindowState = FormWindowState .Normal

Trace("frmMain. LoadMyForm", "++++++++++++++ +++++++++++ (2) - Me.Handle =
" & Me.Handle.ToStr ing & ", TopWindow = " &
GetTopWindow(Me .Handle).ToStri ng)

'Me.TopMost = True
Dim strTest As String
GetWindowText(G etTopWindow(Me. Handle), strTest, 100)

Trace("frmMain. LoadMyForm", "(2) Prior to doing the SetForegroundWi ndow,
TopWindow Name = " & strTest)

'SetForegroundW indow(Me.Handle )
SetWindowPos(Me .Handle, HWND_TOPMOST, Me.Left, Me.Top, Me.Width,
Me.Height, SWP_NOMOVE Or SWP_NOSIZE)

Trace("frmMain. LoadMyForm", "(2) After doing the SetForegroundWi ndow
Me.Handle = " & Me.Handle.ToStr ing & ", TopWindow = " &
GetTopWindow(Me .Handle).ToStri ng)

Trace("frmMain. LoadMyForm", "(2) - Me.TopMost = " & Me.TopMost.ToSt ring)
Trace("frmMain. LoadMyForm", "(2) - Me.Visible = " & Me.Visible.ToSt ring)
Trace("frmMain. LoadMyForm", "(2) - Me.WindowState = " &
Me.WindowState. ToString)
Trace("frmMain. LoadMyForm", "(2) - Me.Position = " & Me.Left.ToStrin g &
", " & Me.Top.ToString )

'Me.TopMost = False
SetWindowPos(Me .Handle, HWND_NOTOPMOST, Me.Left, Me.Top, Me.Width,
Me.Height, SWP_NOMOVE Or SWP_NOSIZE)

Trace("frmMain. LoadMyForm", "(3) - Me.TopMost = " & Me.TopMost.ToSt ring)

---- End code ------------------

It shrinks my window and puts it all the way in the upper left hand
corner of the screen. I thought that the coordinates system in .NET and
Windows was different, but would have expected the "SWP_NOMOVE Or
SWP_NOSIZE" to prevent that.

Other than that, it works the same as doing the Me.TopMost = True
shortly followed by a Me.TopMost = False. My form doesn't pop to the
top of the z-order that I can see.

My tracefile shows:

10/16/2003 1:46:59 PM : frmMain.LoadMyF orm :
+++++++++++++++ ++++++++++ (2) - Me.Handle = 8390128, TopWindow
= 11142560
10/16/2003 1:46:59 PM : frmMain.LoadMyF orm :
(2) Prior to doing the SetForegroundWi ndow, TopWindow Name =
10/16/2003 1:46:59 PM : frmMain.LoadMyF orm :
(2) After doing the SetForegroundWi ndow Me.Handle = 8390128,
TopWindow = 11142560
10/16/2003 1:46:59 PM : frmMain.LoadMyF orm :
(2) - Me.TopMost = False
10/16/2003 1:46:59 PM : frmMain.LoadMyF orm :
(2) - Me.Visible = True
10/16/2003 1:46:59 PM : frmMain.LoadMyF orm :
(2) - Me.WindowState = Normal
10/16/2003 1:46:59 PM : frmMain.LoadMyF orm :
(2) - Me.Position = -1, 103
10/16/2003 1:46:59 PM : frmMain.LoadMyF orm :
(3) - Me.TopMost = False
10/16/2003 1:46:59 PM : frmMain.LoadMyF orm :
Exit routine
Eric Goforth
Raleigh, NC

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #12
In article <O$************ **@TK2MSFTNGP11 .phx.gbl>, E Goforth wrote:
Hello,

I added the following declarations to my MDIParent form:

---- Begin code ------------------

<snip>
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Some of your declares are wrong... Any way, here is an example of using
SetForegroundWi ndow...

Option Strict On
Option Explicit On

Public Class Form1
Inherits System.Windows. Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeCompo nent()

'Add any initialization after the InitializeCompo nent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.Componen tModel.IContain er

'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Timer1 As System.Windows. Forms.Timer
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()
Me.components = New System.Componen tModel.Containe r
Me.Timer1 = New System.Windows. Forms.Timer(Me. components)
'
'Timer1
'
Me.Timer1.Enabl ed = True
Me.Timer1.Inter val = 10000
'
'Form1
'
Me.AutoScaleBas eSize = New System.Drawing. Size(5, 13)
Me.ClientSize = New System.Drawing. Size(608, 453)
Me.IsMdiContain er = True
Me.Name = "Form1"
Me.Text = "Form1"

End Sub

#End Region

Private Declare Function GetCurrentThrea dId Lib "kernel32" () As
IntPtr

Private Declare Function AttachThreadInp ut Lib "user32" _
(ByVal idAttach As IntPtr, _
ByVal idAttachTo As IntPtr, _
ByVal fAttach As Boolean) As Boolean

Private Declare Function GetWindowThread ProcessId Lib "user32" _
(ByVal hWnd As IntPtr, _
ByVal lpdwProcessId As IntPtr) As IntPtr

Private Declare Function SetForegroundWi ndow Lib "user32" (ByVal
hWnd As IntPtr) As Boolean
Private Declare Function GetForegroundWi ndow Lib "user32" () As
IntPtr

Private Sub Timer1_Tick(ByV al sender As System.Object, ByVal e As
System.EventArg s) Handles Timer1.Tick
If Me.WindowState = FormWindowState .Minimized Then
Me.WindowState = FormWindowState .Normal
End If

Dim foregroundThrea d As IntPtr =
GetWindowThread ProcessId(GetFo regroundWindow( ), IntPtr.Zero)
Dim currentThread As IntPtr = GetCurrentThrea dId()

If Not foregroundThrea d.Equals(curren tThread) Then
AttachThreadInp ut(foregroundTh read, currentThread, True)
End If

SetForegroundWi ndow(Me.Handle)

If Not foregroundThrea d.Equals(curren tThread) Then
AttachThreadInp ut(foregroundTh read, currentThread, False)
End If

End Sub

Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
Dim f As New Form2
f.MdiParent = Me
f.Visible = True
End Sub
End Class

HTH
--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
6851
by: JaguarX | last post by:
I have an application that needs a window to stay on top of the other ones, I thought that TopMost = true would be suitable for this feature but it also makes this window to stay on top of all other application running on my PC, Does anybody know How can I prevent this TopMost window appear when I switch to the other applications?? _______________________ JaguarX
3
7862
by: Hovhannes Asatryan | last post by:
Hello guys. I have a problem with a topmost form. I am writting in C#. I have Mdi Form wich has 2 child forms. I want to set one of them as a topmost window, but when I sets MdiParent the TopMost property does not working. How can I set the one form as a topmost? Can anyone send me code example?
2
3839
by: Max | last post by:
Hello, My VB.NET application displays a Windows form in a panel on my main form. While my application has the focus, I want the form in the panel to have the "topmost" position, even if it is not the active form. Everything works as expected, but if the user selects another application by pressing a tab in the taskbar, then my "topmost" form still occupies the "topmost"
1
2029
by: SamSpade | last post by:
I learned about the TopMost property from a recent post which solved some problems. But I have a situation where a control wants to make the form it is on be the topmost. It could raise an event but be cleaner if it could do it directly. I tried the following but the compiler thinks Parent is a control (they don't have a topmost property.)
6
4752
by: Strahimir Antoljak | last post by:
I'm running VS.NET 2002 and keep having problems with form keeping on top with its TopMost property. Sometimes my program places the invoked form on top sometimes not. Sometimes when I click on the form it stays on top as it should, but sometimes TopMost property does not pick up so if I click to some other program it covers the form which supposed to remain on the top. Has anyone experienced similar problems? is there a fix? or I am...
2
1857
by: facicad | last post by:
I would like to set topmost another prog. from my program. Ex: I use AutoCAD, run my prog. from autocad. My prog. is topmost but went I would like to pick some object in autocad, I set TopMost to false and I would like to autocad bring to front, but for autocad object, it not have TopMost properties
0
1788
by: hzgt9b | last post by:
Using VB .NET 2003, I am having problems with a windows application that I have written: Here's the situation. The application I created has a main form that has its TopMost property set to True (so that the form is always on top). During the execution of the application, a new process is created using "Process.Start(<some app>)". The problem is that after I start a new process, my main form no longer has focus, so all keyboard events are...
1
2593
by: Phil Galey | last post by:
I have a small application, which is to always stay on top. Another application, which it is supposed to stay on top of, is QuarkXPress 5. I'm using Me.TopMost = True in the Form1_Deactivate event, and it works almost perfectly. It always stays on top, except when I click on one of the tools in the floating tool bar of QuarkXPress. Anything else in QuarkXPress is no problem ... document, menus, etc. But click on anything on...
1
2044
by: Gary Brown | last post by:
Hi, I have a dialog box that must behave as MessageBox does with regard to the TopMost property. It must be TopMost if and only if the calling form is TopMost. A static method creates the dialog box. Is there a way of determining if the application is TopMost from a static method? There are some inelegant workarounds but I would prefer to do whatever MessageBox does.
0
9579
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10205
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10035
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9851
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7401
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3949
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3556
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2811
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.