devxlogo

How to Set Topmost Forms

How to Set Topmost Forms

Setting topmost forms is easier with .NET than with previous Windows languages, since you no longer have to refer to an API call. Simply setting the .topmost form property to true will force the form to the top layer of the screen, while leaving the user able to enter data in the form(s) below. Note: this is NOT the same as a modal form, where all program activity from the calling module stops at the place the modal form is called, resuming when the modal form is closed. The topmost and lower forms are running in parallel.

Here’s how to set the lower form text highlighting. By default, when the lower form loses focus, the active control (let’s say it’s a textbox for example), will lose the highlighting on the text. If your topmost form selects text to overwrite a selection on your lower form, that means you can no longer see the word you highlighted to overwrite it. To fix this, set the me.activecontrol.hideselection property to false from the topmost form. You will need to make a global instance of the parent form to be able to refer to it’s properties:

in the form loaddim ff as ThisFormTypeff = me 'me being of type thisFormType

If you do something to cause a messagebox or error message to appear, the topmost form will appear OVER the messagebox, essentially freezing the application, since the user can no longer answer the messagebox OR do anything with the form.

There are two workarounds for this problem:

  • Set the topmost property to false just before running a routine, then set it back to true after any messageboxes have appeared (messageboxes are modal, so the program flow stops until it is answered). The problem with this method is that by setting and unsetting the topmost property, the highlighting text on the lower form (as referred to above) will disappear regardless of being true or false.
  • Have a ShowErrorsFlag as boolean. When calling a function that may create a messagebox or error message, set the flag to false and suppress the messages (the downside here is you will never see any errors, which may be OK if the function is trivial).

Here’s what this looks like:

    Public Function SaveChanges(ByVal ShowErrorsFlag As Boolean) As Boolean        'note, when calling this fuction from the same form (the topmost form)        'set the ShowErrorsFlag = true, if calling from a LOWER form, always         'set ShowErrorsFlag = false, so no error messages will lock up the forms        Dim s As String        Dim RetInt As Integer        If ShowErrorsFlag Then              Me.TopMost = False        End If        Try            s = "UPDATE [genhelp] "            s = s & " SET [localmessage] ='" & DoubleChar(Me.txtBottomLineHelp.Text) & "', "            s = s & "     [helpContextID] = '" & Val(Me.txtContextID.Text) & "', "            s = s & "     [lastModified] = #" & Now & "# "            s = s & " WHERE [formname] = '" & Me.LblFormName.Text & "' "            s = s & " AND [controlname] = '" & Me.LblControlName.Text & "' "            RetInt = RunUpdateSQL(s, True)        Catch ex As Exception            If ShowErrorsFlag Then                MsgBox(Err.Number & " " & Err.Description & " In SaveChanges ")            End If        End Try        If ShowErrorsFlag Then            Me.TopMost = True        End If        If RetInt > 0 Then            IsDirtyFlag = False            Return True        End If    end function

devx-admin

Share the Post: