| This page includes a number of interesting programming tips that the book forgot. Email us if you would like to add a tip of your own (I'll include your name). |
| 001: Executing JavaScript from Visual Basic .NET code |
|
The following statement displays a popup window using the alert method in JavaScript: Response.Write("<" & "script>alert('Please enter a password.');<" _
& "/script>")
|
| 002: Loading an image file into a PictureBox control |
|
To load an image file into a PictureBox control at runtime, call the FromFile method in the Image class, passing it a filename. Assign the result to the Image property of your PictureBox. Suppose our PictureBox is named picKayak, and the picture file (located in the project's \bin directory) is named "NeckyAmaruk.jpg". The following code does the job: picKayak.Image = Image.FromFile("NeckyAmaruk.jpg")
|
| 003: Using the SelectedIndices property of a ListView control |
|
The SelectedIndexChanged property can fire even when no item in a ListView is selected. To avoid runtime errors, use a Try-Catch block around references to SelectedIndices(0). In the following, our ListView is named lvwSample:
Try
Dim index As Integer = lvwSample.SelectedIndices(0)
Catch
End Try
|
| 004: Using conditional compiler directives to test your program |
|
The #Const, #If, #Else, and #End If compiler directives let you decide which blocks of code are to be compiled into your program. For example, you might want to include specific code when testing a class or procedure. Otherwise, when the program is ready for distribution, you can include a different block of code. In the following example, we assign either True or False to testmode, alternating between separate blocks of source code: #Const testmode = True 'assign True or False . . #If testmode = True Then 'testing code goes here #Else 'production code goes here #End If |
| 005: Preventing a Form from closing |
|
If you want to prevent a Windows desktop form from closing, create a handler for the form's Closing event. In the handler, write: e.Cancel = True. Nothing the user does will make the form close, short of terminating the process from the Windows Task Manager. |
| 006: Checking for end of file when calling StreamReader.ReadLine |
|
When you're using a StreamReader to read from a text file, the Peek property equals -1 when the end of the file is found. If a StreamReader were named reader, the following While statement would continue the loop until no more data was found: While (reader.Peek <> -1) |
| 007: Executing scripts in Web forms |
|
JavaScript is a rich language with many capabilities that is available to you when programming in ASP.NET. To create and execute a script, call the RegisterStartupScript function. In the following template, key can be any identifier. The script itself is entered where it says enter script here: RegisterStartupScript("key", "<script>enter script here</script>")
If your script contains a quoted string, surround it with single quotes. For example: RegisterStartupScript("script1", "<script>alert('Caution!');</script>")
Alternatively, use two quotes in a row for embedded quotes: RegisterStartupScript("script1", "<script>alert(""Caution!"");</script>")
|
| 008: Displaying dialog windows from Web forms |
|
Desktop applications can call the MessageBox function to display popup messages, but this function is not available in Web forms. You can, however, call the JavaScript alert, confirm, and prompt functions. The following call to confirm transfers control to a new form if the user clicks on the OK button: RegisterStartupScript("script1", "<script>if(confirm(" _
|