Visual Studio VB.NET Sample Code
Changing the Cursor to an Hour Glass
Showing a Processing Image on a Web Form
Using ASP & VB.NET

I have always wanted to be able to perform the tasks indicated in the title of this article and after spending several days trying different approaches I ended up with the following which suits my needs. It utilizes Java Script and a Confirmation Box to cause post backs when you need them.

Note: A portion of this process uses the Confirmation Box control created by Vvaibhav Gawali which can be found at: www.codeproject.com/Articles/13866/A-Simple-Event-Driven-Confirmation-Box-Messege-Box

Download just the DLL now

Download the complete article as a Word document here: Hour Glass Cursor - Processing Image.doc



1. A reference is required to: MessageBox.dll

2. At the top of the aspx form you want to use place the following line in the Source view:

<%@ Register Assembly="MessageBox" Namespace="Utilities" TagPrefix="cc2" %>

3. At the bottom of the same page place the following lines. I will be using two different confirmation boxes as shown:

< cc2:MessageBox ID="MessageBox" runat="server" Style="z-index: 101; left: 0px; position: absolute; top: 100px" Width="112px" />
< cc2:MessageBox ID="MessageBox1" runat="server" Style="z-index: 101; left: 0px; position: absolute; top: 100px" Width="112px" />

4. Near the top of the source page add the following Java script:

< script type="text/javascript" >

function ShowHourGlass() {

document.body.style.cursor = 'wait';
show_image('/images/waitimage.gif', 300, 200, 'Wait Image');
}

function ShowDefault() {

document.body.style.cursor = 'default';
}

function show_image(src, width, height, alt) {

var img = document.createElement("img")
var xWidth = (screen.width-300)/2
img.src = src;
img.width = width;
img.height = height;
img.alt = alt;
img.style.position = "absolute"
img.style.left = xWidth+"px"
img.style.top = "50px"

// This next line will be added it to the tag when this function is called document.body.appendChild(img);
}

function moveBy(obj, x, y) {

obj.xpos += x
obj.left = obj.xpos
obj.ypos += y
obj.top = obj.ypos
}

< /script >

5. You will need at least one button to get things started: and whatever image you want to use. I used an animated gif which is available here.

asp:Button ID="btnWait" runat="server" Text="Send Emails" OnClick="btnWait_Click"

waitimage.gif

6. Here is some sample code:


'Requires a Reference to MessageBox.dll

Public Class WaitCursor
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub

Protected Sub btnWait_Click(sender As Object, e As EventArgs) Handles btnWait.Click

'the following code calls the Java Script Function / ShowHourGlass() / which will run when the page is reposted by the btnWait.Click event
'as the function runs it will change the cursor to the HourGlass and call the / showimage() / function which shows and locates the / waitimage.gif/ an animated gif

ScriptManager.RegisterStartupScript(Me, Me.GetType(), "ShowHourGlass", "ShowHourGlass();", True)

' the next line invokes the MessageBox.dll to show the confirmation box and request user input. When the user responds the MessageBox.GetMessageBoxResponse is called

MessageBox.Confirm("The Emails are ready to send. Click on OK to begin. This may take a few minutes to complete.")


End Sub

Private Sub MessageBox_GetMessageBoxResponse(sender As Object, e As Utilities.MessageBox.MessageBoxEventHandler) Handles MessageBox.GetMessageBoxResponse

'the following is obvious. When the user clicks OK the / RunProcess() / sub is called and whatever you want to run is started. I am sending out several hundred emails. If the user clicks cancel then the image goes away and the default cursor is shown using the Javas Script function / ShowDefault() /

If e.ButtonPressed = Utilities.MessageBox.MessageBoxEventHandler.Button.Ok Then

RunProcess()
Else
If e.ButtonPressed = Utilities.MessageBox.MessageBoxEventHandler.Button.Cancel Then
ScriptManager.RegisterStartupScript(Me, Me.GetType(), "ShowDefault", "ShowDefault();", True)
End If
End If

End Sub

Private Sub RunProcess()

' the first line simulates any process you want to run. The second line invokes the second message box control which only offers the OK option and
'the MessageBox1.GetMessageBoxResponse is then called.
MessageBox1.Alert("Emails have been sent. See below for the addresses that have bounced.")

End Sub

Private Sub MessageBox1_GetMessageBoxResponse(sender As Object, e As Utilities.MessageBox.MessageBoxEventHandler) Handles MessageBox1.GetMessageBoxResponse

' the image goes away and the default cursor is shown using the Javas Script function / ShowDefault() /
If e.ButtonPressed = Utilities.MessageBox.MessageBoxEventHandler.Button.Ok Then
ScriptManager.RegisterStartupScript(Me, Me.GetType(), "ShowDefault", "ShowDefault();", True)

End If

End Sub