How to Program Button Events with VBScript

Using VBScript to Handle Button and Mouse Events

© Mark Alexander Bain

Dec 16, 2008
VBScript and Button Events, Mark Alexander Bain
Internet Explorer is the one web browser that uses VBScript and because of that it is very powerful - especially when it comes to handling events such as button clicks

It's not often that any web browser has a definite advantage over any other one, however, Internet Explorer does have one major advantage - VBScript; and it's not just the versatility of VBScript that makes it attractive - it's VBScript's ease of use. Take, for example, buttons on a web page.

Each button on a web page has a number of built-in events that can be triggered by the web page user and these events are:

  • onclick: triggered when the user clicks on the button
  • onmouseover: triggered when the mouse pointer is placed over the button
  • onmouseout: triggered when the mouse pointer is moved away from the button
  • ondblclick: triggered when the user double-clicks on the button
  • onmousedown: triggered when the mouse button is depressed
  • onmouseup: triggered when the mouse button is released
  • onmousemove: triggered when the mouse pointer is moved whilst over the button

And the beauty of these events is that each event has a subroutine associated with it.

The Onclick Subroutine

The obvious event for any button is the onclick event - but before the onclick event can be used the button needs to be defined using HTML:

<h1>VBSCript Button Events</h1>
<input type=button value="Click Me" id=button_1>
<div id=output_text></div>

Next the onclick subroutine is required - and the name of the subroutine always follows the same convention:

  • prefix - the id of the button
  • separator - an underscore
  • suffix - the name of the event

So, for example, the name for the subroutine, in this case, will be button_1_onclick:

<script language=vbscript>
sub button_1_onclick
output_text.innerHTML = "You clicked me"
end sub
</script>

If the web page is loaded into Internet Explorer and the button is clicked then the div area will be written to.

The Onmouseover and Onmouseout Subroutines

As well as having events associated with it, each button is also self-referencing, for example:

sub button_1_onmouseover
me.value = "Go on - Click me"
end sub

In this example the text of the button is updated when the mouse pointer is placed over it and, of course, the text can be changed back again when the mouse pointer is moved away from the button:

sub button_1_onmouseout
me.value = "Click me"
end sub

A suitable use for this may be to supply the user with instructions before they click the button.

The Ondblclick Subroutine

As well being able to click on the button, the user can also double-click on the the button:

sub button_1_ondblclick
output_text.innerHTML = "Calm down - once is enough"
end sub

And in addition to clicking and double-clicking the button two other events are associated with the button click - onmousedown and onmouseup.

The Onmousedown and Onmouseup Subroutines

The onmousedown subroutine is triggered when the mouse button is depressed over the button (and fires before the onclick subroutine):

sub button_1_onmousedown
window.status = "Clicking " & me.id
end sub

And the onmouseup subroutine is triggered when the mouse button is released (and fires after the onclick subroutine):

sub button_1_onmouseup
window.status = me.id & " clicked"
end sub

In both these examples the window.status is written to - that's the bar at the bottom of the Internet Explorer display.

The final subroutine fires whenever the user moves the mouse pointer over the button - but this should be used with care.

The Onmousemove Subroutine

The usage of the onmousemove subroutine should be limited as much as possible - simply because the subroutine is triggered whenever the user moves the mouse, for example:

<div id=mouse_moves></div>
<script language=vbscript>
dim mousemove_count : mousemove_count = 0
sub button_1_onmousemove
mousemove_count = mousemove_count + 1
mouse_moves.innerHTML = mousemove_count
end sub
</script>

The example shows that this event can be triggers hundreds of times per minute.

Summary

Each HTML button in Internet Explorer has a number of events associated with it:

  • onclick
  • onmouseover
  • onmouseout
  • ondblclick
  • onmousedown
  • onmouseup
  • onmousemove

And each event has its own subroutine which is named after the button and the event, all of which the web page programmer can implement very easily.


The copyright of the article How to Program Button Events with VBScript in Windows Programming is owned by Mark Alexander Bain. Permission to republish How to Program Button Events with VBScript in print or online must be granted by the author in writing.


VBScript and Button Events, Mark Alexander Bain
VBScript Code to Handle Button Events, Mark Alexander Bain
The Default Page, Mark Alexander Bain
The Onmouseover Event, Mark Alexander Bain
The Combined Internet Explorer Events, Mark Alexander Bain


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo