Solved

showDialog not working for me

Posted on 2006-11-26
5
466 Views
Last Modified: 2008-01-09
Using .NET 1.1, Windows Forms

i I have a .NET dll that I call from a PowerBuilder application. The .NET dll opens a windows form. Unless the user explicitly closes the windows form, I don't want them to be able to click out of it. In other words, the form should remain the top most form until it is explicitly closed. I tried form.ShowDialog() but it doesn't work because I can still click on the Powerbuilder window behind the windows form. Any ideas on how I can prevent the form from losing focus unless it is explicity closed?

Thanks

San
0
Comment
Question by:Sanmarie
5 Comments
 
LVL 39

Expert Comment

by:appari
ID: 18016356
add the following in to a module

 Private Const SWP_NOMOVE = 2
    Private Const SWP_NOSIZE = 1
    Private Const HWND_TOPMOST = -1
    Private Const HWND_NOTOPMOST = -2
    Private Const FLAGS = SWP_NOMOVE Or SWP_NOSIZE

    Private Declare Auto Function SetWindowPos Lib "user32" _
          (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


    Public Function SetTopMostWindow(ByVal hWnd As IntPtr, ByVal Topmost As Boolean) As Long
        ' Function sets a window as always on top, or turns this off

        Try

            If Topmost = True Then 'Make the window topmost
                SetTopMostWindow = SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, FLAGS)
            Else
                SetTopMostWindow = SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, FLAGS)
                SetTopMostWindow = False
            End If
        Catch ex As Exception
            Throw
        End Try

    End Function


and call it from form load event to set the form as always on top window

SetTopMostWindow me.handle, true
0
 
LVL 18

Assisted Solution

by:Ravi Singh
Ravi Singh earned 100 total points
ID: 18016443
Windows forms in .NET have a TopMost property:

form.TopMost = True
form.Show()
0
 
LVL 39

Assisted Solution

by:appari
appari earned 100 total points
ID: 18016447

or simply set

Me.TopMost = True

in formload event
0
 
LVL 5

Accepted Solution

by:
sivachirravuri earned 300 total points
ID: 18017234
Private Sub frm_LocationChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.LocationChanged
Me.CenterToScreen()
Me.TopMost = True
End Sub
0
 

Author Comment

by:Sanmarie
ID: 18020181
Sorry for taking so long to get back to you. The topMost property worked.
However, it did not work 100% in the LocationChanged event. I had to move the form around for it to work. The LostFocus event of the window did the trick

Private Sub form1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.LostFocus
        Me.CenterToScreen()
        Me.TopMost = True
    End Sub

Thanks

San

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question