Introduction
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 (www.website1.com) should be displayed
- If the time is out of the above given bracket, then website2 (www.website2.com) should be displayed
Website3 and website4 on System2
- From 10:00 AM to 8:00PM website3 (www.website3.com) 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
RadioButton
s 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
Else
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
Else
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 = "http://website1.com"
txt_url1.Text = "http://website2.com "
Else
flag_select = 0
txt_url2.Text = "http://website3.com "
txt_url1.Text = "http://website4.com "
End If
refresh_web() ‘this calls the time checking part that’s mentioned above
End Sub
Now, each time the RadioButton
s are changed, the relevant pages should be selected.
So, put in the below code into the CheckedChanged
event of the
RadioButton
s.
Private Sub rb_1_CheckedChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles rb_1.CheckedChanged
select_page()
End Sub
Private Sub rb_2_CheckedChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles rb_2.CheckedChanged
select_page()
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
refresh_web()
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 = "http://website1.com"
txt_url1.Text = "http://website2.com"
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
Else
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
OLECMDEXECOPT_DODEFAULT = 0
OLECMDEXECOPT_PROMPTUSER = 1
OLECMDEXECOPT_DONTPROMPTUSER = 2
End Enum
Private Enum Exec
OLECMDID_OPTICAL_ZOOM = 63
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
Dim wbInstance As Object = WebBrowser1.ActiveXInstance
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
wbInstance.ExecWB(Exec.OLECMDID_OPTICAL_ZOOM, ExecOpt.OLECMDEXECOPT_DONTPROMPTUSER, 100, DBNull.Value)
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
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.KeyPressedCode = 27 Then
Me.WindowState = FormWindowState.Normal
WebBrowser1.Dock = DockStyle.Right
ElseIf e.KeyPressedCode = 13 Then
zoom50()
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
If txt_timer.Text = "" Then
Timer1.Interval = 5 * 60 * 1000
Else
str_timer = (CInt(txt_timer.Text) * 60 * 1000)
Timer1.Interval = str_timer
End If
Timer1.Start()
WebBrowser1.Refresh()
refresh_web()
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
Timer1.Stop()
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 member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.