Click here to Skip to main content
15,066,928 members
Articles / Programming Languages / Visual Basic
Posted 4 Jan 2013

Tagged as


6 bookmarked

Time controlled website rotation

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
4 Jan 2013CPOL4 min read
Shows how to set up a time controlled rotation of displaying different websites.


I have had instances in which I would require2 or more websites in a set of two to keep rotating at a given interval of time on different displays. The following would give you a basic guide on how to accomplish this and to get it automated. Yes, of course, you can manually type in the URL of each website into the Web browser, but the whole point is to get it automated.

Using the code

To make things a bit more complicated we would use the below scenario. Website1 and website2 on System1

  • From 10:00 AM to 4:00PM website1 ( should be displayed
  • If the time is out of the above given bracket, then website2 ( should be displayed

Website3 and website4 on System2

  • From 10:00 AM to 8:00PM website3 ( should be displayed
  • If the time is out of the above given bracket, then website4 (www.website4com) should be displayed

And the above should happen every day until another so called “doomsday” hoax comes out. You can have different website flipping or have different webpages of the same website flipping. Just put in the URL and your set to go. You can put in a timer control into the websites to do this intentional flipping, however, if the source code of the website isn’t in your control, then you are out of luck with this option. For the following, I am using VS2010 and its built in WebBrowser control.

First, fire up VS, and start a new windows project and give it an appropriate name. In the Solution Explorer open up Form1 in design view (right click and view designer). Add the below controls as shown in the image below and the explanation is provided below. And drag and drop a timer control with the Enabled property set to True and Interval set to 20000 (this Is in milliseconds which equals to 20 seconds).


  • Textbox marked as “1” is named txt_url1
  • Textbox marked as “2” is named txt_url2
  • Textbox marked as “3” is named txt_timer
  • Stop button is name btn_stop
  • Start button is name btn_start
  • The groupbox named “Info” contains just labels indicating the keyboard keys and their action
  • Groupbox “Select” contains two RadioButtons namely rb_1 and rb_2
  • The label on the bottom “Last Update :” is just to indicate the time when the timer was last run
  • The white space on the right indicated as 4 is the main part – the WebBrowser control

Set the below properties of the WebBrowser as indicated.

ScriptErrorsSuppressed = True 
ScrollBarsEnabled = False 
Dock = Right 
Anchor = Top, Left

Yup, all set to code now.

Double click the Start button so that the code page opens. Paste in the below in the start of the code page

Public Class Form1
    Dim flag As Integer
    Dim str_url1 As String
    Dim str_url2 As String
    Dim str_timer As Integer
    Dim flag_select As Integer

The next sub does the logical part of checking the time and choosing which page to load and when to do so.

Sub refresh_web()
    If flag_select = 1 Then
        If Not (TimeOfDay >= #10:00:00 AM# And TimeOfDay <= #4:00:00 PM#) Then
            If Not WebBrowser1.Url.ToString.Contains(txt_url1.Text) Then
                WebBrowser1.Navigate(txt_url1.Text) ‘Website 1
            End If
            If Not WebBrowser1.Url.ToString.Contains(txt_url2.Text) Then
                WebBrowser1.Navigate(txt_url2.Text)     ‘Website 2
            End If
        End If

    ElseIf flag_select = 0 Then
        If Not (TimeOfDay >= #10:00:00 AM# And TimeOfDay <= #8:00:00 PM#) Then
            If Not WebBrowser1.Url.ToString.Contains(txt_url1.Text) Then
                WebBrowser1.Navigate(txt_url1.Text)      ‘Website 3
            End If
            If Not WebBrowser1.Url.ToString.Contains(txt_url2.Text) Then
                WebBrowser1.Navigate(txt_url2.Text)       ‘Website 4      
            End If
    End If

    End If
    lbl_date.Text = Now ‘ this set the time of the last run
End Sub

Then the part that sets the page url in the txt_url1 and txt_url2

Sub select_page()
    If rb_1.Checked = True Then
        flag_select = 1

        txt_url2.Text = ""
        txt_url1.Text = " "
        flag_select = 0

        txt_url2.Text = " "
        txt_url1.Text = " "
    End If
    refresh_web() ‘this calls the time checking part that’s mentioned above
End Sub

Now, each time the RadioButtons are changed, the relevant pages should be selected. So, put in the below code into the CheckedChanged event of the RadioButtons.

Private Sub rb_1_CheckedChanged(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles rb_1.CheckedChanged
End Sub

Private Sub rb_2_CheckedChanged(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles rb_2.CheckedChanged
End Sub

For the timer to check the time of the day each time it runs, call the refresh_web() like the below.

Private Sub Timer1_Tick(sender As Object, e As System.EventArgs) Handles Timer1.Tick
End Sub

When the form loads, its better to put in a default set of webpage set to load so that the WebBrowser control doesn’t appear blank on startup.

Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    txt_url2.Text = ""
    txt_url1.Text = ""
    Me.WindowState = FormWindowState.Maximized
End Sub

For some visual control over the form itself on maximize and minimize, put in the below on the SizeChanged event of the Form.

Private Sub Form1_SizeChanged(sender As Object, e As System.EventArgs) Handles Me.SizeChanged
    If Me.WindowState = FormWindowState.Normal Then
        Me.FormBorderStyle = Windows.Forms.FormBorderStyle.Sizable
        Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
    End If
End Sub

Now comes the web browser functions for setting the page zoom. I had to put these because, at times, certain web pages do not completely fit into the web browser and into the screen. These are the ActiveX calls.

Private Enum ExecOpt
    'A few options for what we want to do, using ActiveX
    'This option will just carry out the default action
    'This option will prompt the user before carrying out an action  
    'This option will not prompt the user before carring out an action
End Enum

Private Enum Exec
    'This is the main ActiveX declaration for the webbrowser Zoom
End Enum

Next, two functions to set the zoom to 50% and 100% which uses the ActiveX calls. You can set these numbers as you would want depending on the screen fit.

Sub zoom50()
    On Error Resume Next
    'Declare an ActiveX instance for the webbrowser
    Dim wbInstance As Object = WebBrowser1.ActiveXInstance
    'Execute the zoom. The third parameter, the nubmer is the one which tells
    'the ActiveX instance how far to zoom in on the browser, in this case by 50%.
    wbInstance.ExecWB(Exec.OLECMDID_OPTICAL_ZOOM, _
               ExecOpt.OLECMDEXECOPT_DONTPROMPTUSER, 50, DBNull.Value)
End Sub

Sub zoom100()
    On Error Resume Next
    Dim wbInstance As Object = WebBrowser1.ActiveXInstance
End Sub

You can call either of the above Subs in a keypress event, which is explained below

The WebBrowser as such does not expose a KeyDown event, this means you would have to initiate the handler. How would you do that? Well, like below:

Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, _
           ByVal e As Windows.Forms.WebBrowserDocumentCompletedEventArgs) _
           Handles WebBrowser1.DocumentCompleted
    Dim document As HtmlDocument = WebBrowser1.Document
    'Attach KeyDown event handler
    AddHandler document.Body.KeyDown, New HtmlElementEventHandler(AddressOf WebBrowser1_KeyDown)
End Sub

Now for the keyboard events to be fired:

Private Sub WebBrowser1_KeyDown(ByVal sender As Object, ByVal e As HtmlElementEventArgs)
    ' If e.CtrlKeyPressed Then
    ' ElseIf e.ShiftKeyPressed Then

    If e.KeyPressedCode = 27 Then
        Me.WindowState = FormWindowState.Normal
        WebBrowser1.Dock = DockStyle.Right
    ElseIf e.KeyPressedCode = 13 Then
    ElseIf e.KeyPressedCode = Asc(Keys.C.ToString) Then
        WebBrowser1.Dock = DockStyle.Right
    ElseIf e.KeyPressedCode = Asc(Keys.X.ToString) Then
        WebBrowser1.Dock = DockStyle.Fill
    End If
End Sub

The above key mappings are what you have in the Info part of the Form’s UI.

Finally, for the functions in the Start and Stop buttons.

Private Sub btn_start_Click(sender As System.Object, e As System.EventArgs) Handles btn_start.Click
    Me.btn_stop.Enabled = True
    str_url1 = txt_url1.Text
    str_url2 = txt_url2.Text
    '  str_timer = (CInt(txt_timer.Text) * 1000)
    If txt_timer.Text = "" Then
        Timer1.Interval = 5 * 60 * 1000

        str_timer = (CInt(txt_timer.Text) * 60 * 1000)
        Timer1.Interval = str_timer
    End If


    Me.WindowState = FormWindowState.Maximized
    WebBrowser1.Dock = DockStyle.Fill
End Sub
Private Sub btn_stop_Click(sender As System.Object, e As System.EventArgs) Handles btn_stop.Click
    Me.btn_stop.Enabled = False
    WebBrowser1.Dock = DockStyle.Right
    Me.WindowState = FormWindowState.Normal
End Sub

Once you click the Start button, the browser would be maximised and the timer starts. If you would want to minimize this, press the ESC key on your keyboard and you would be on the main form so that you can change the settings.

The keyboard mappings can be customized with your liking I just went with the common trend.

Well there you are, all set to try and keep the pages rotating.

Points of Interest

The WebBrowser control is a powerful one, but it took me some time to understand some basic functions of it. On some browsers, the functions may not work, but I have tried this on IE 7 and IE 8. Works well.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
United Arab Emirates United Arab Emirates
No Biography provided

Comments and Discussions

QuestionWebBrowser Control Pin
Jim Meadors9-Feb-13 19:17
MemberJim Meadors9-Feb-13 19:17 
AnswerRe: WebBrowser Control Pin
joe_j10-Feb-13 0:18
Memberjoe_j10-Feb-13 0:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.