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
Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes