Delay loading WebBrowser or any other control

Applies To

OS:
VB:
NT, 9x, 2k, XP
6

One of the problems with using the WebBrowser control on your VB forms is that it causes a fairly large performance hit when starting the application. The same can be said for some commercial grids and other heavy-duty controls. If you need these controls on startup, there is not much that can be done. However, if you need the WebBrowser control later in the life of the application, you can delay loading the control until you actually need it.

The key here is the Form.Controls collection. Starting with VB6, you can add controls to it as you go. The only downside to this method is that you have to preselect the controls you are planning to use in Project/Components. Then, and this is the most important step, go to Project Properties, select the Make tab and uncheck the 'Remove information about unused ActiveX Controls' checkbox. There is a method to completely avoid this whole rigmarole, whereby there is no need to preselect wanted controls, but the event handling in this case is not very intuitive and very un-VB like. Ok, on to the project.

Project instructions
Add the following code to form
Private WithEvents browser as SHDocVwCtl.WebBrowser

Private Sub Command1_Click()
   Set browser = Controls.Add("Shell.Explorer.2", "browser", Frame1)
   With browser
      .Visible = True
      .Width = Frame1.Width - 2000
      .Height = Frame1.Height - 2000
      .Top = 1000
      .Left = 1000
      .Navigate "http://www.yahoo.com"
   End With
End Sub

Post instructions