Share via


From the July 2002 issue of MSDN Magazine

MSDN Magazine

Readers Modify Our Pop-up Program
Download the code for this article:Ednote0207.exe (170KB)

A couple of months ago, (April 2002) we presented a simple program that catches scripts in Microsoft Internet Explorer as they attempt to launch pop-up windows. Several readers have sent us comments and improvements, so this month we thought we'd recognize some of their work.
      Before we begin, though, we should answer a couple of questions we received about our work. We called our code sample "pop-up stopper" in the headline, which happens to be the name of a commercial software product produced by Panicware (https://www.panicware.com). Since we're doing this as a programming exercise rather than a real venture, we suggest you visit their site (or any of several other vendors) if you want a more robust tool than what we've patched together.
      We were also asked for an implementation of the original program using the .NET Framework. (We used Visual Basic 6.0, which is mostly obsolete now.) The stumbling block to this was that the current release of Visual Studio .NET exposes a very specific Internet Explorer anomaly—the BeforeNavigate2 event of the WebBrowser control never fires. This is documented in Q311298.
      Mark Nadig improved the code we presented to fix the ParseURL routine so it handles HTTPS and FTP URLs. He also works in both a ban filter and an allow filter, and starts the program on the user's home page. His submission is found in nadig.zip. Here's an example of his code, which modifies the GetDir function to get the appropriate path:

  Function GetDir() As String
  
Dim GD$
Dim rc As Long
' first see if 2000/xp, use profile path
GD = Environ("UserProfile")

' if no profile path, use windows directory
If IsEmpty(GD) Or GD = "" Then
GD = String(255, " ")
rc = GetWindowsDirectory(GD, 254)
GD = Trim(GD)
GD = Left(GD, Len(GD) - 1)
End If ' no UserProfile env. Get windows path


strWorkDir = GD
GetDir = GD

End Function ' GetDir

 

      Now, back to that BeforeNavigate2 problem. Several people thought of possible ways to fix it, but one reader, Juan Alvarado, actually put two and two together and reimplemented WatchIE in Visual Basic .NET. After running it through the Visual Basic Upgrade Wizard, he was at first flummoxed by Q311298. After a bit of quick thinking, however, he plugged in the NavigateComplete2 event as a workaround until the BeforeNavigate2 roadblock is fixed.
      Not content to leave well enough alone, Juan also implemented the Remove functionality on the frmBanned dialog that we had left out. (We told you our code was unfinished!) Finally, he made some housekeeping changes to the code. For instance, he writes,

In Visual Studio .NET, the Activated event is fired whenever the object in question is made active, including when the form is first loaded. The Activated event fires after the Load event, resulting in the form becoming visible despite its Visible property being set to False in the Load event handler. The solution is to set the Visible property to False in the Activated handler as well.

It's as simple as this:

  Private Sub frmWatchIE_Activated(ByVal sender As Object, _
  
ByVal e As System.EventArgs) Handles MyBase.Activated
Me.Visible = False
End Sub

 

      Juan's complete code is available in the July 2002 code download at the link at the top of this article.
      Finally, if you're interested in code projects like these, there are several places you can find more. Gotdotnet.com hosts the .NET Code Wise Community (https://www.gotdotnet.com/team/codewise), which provides lots of resources for budding .NET programmers. They list at least a dozen community sites, including Code Project (https://www.codeproject.com), where you can locate and even update samples for the .NET community.
      Taking on projects like this on your own time is one of the best ways to learn a lot about .NET (or any programming model) in a relatively short time, and we strongly suggest it for anyone still looking to start their mental migration to the .NET Framework.
      Seeing that we're not prone to exaggeration, let us say that this project is quickly becoming a triumph of the human spirit! Keep 'em coming.