Tuesday, January 27, 2009

Opening a Form by Type or Name

Opening a Form by Type or Name
You have a class that is able to trap an event, say the doubleclick of a textbox, and has to open a form when it occurs. If it's always the same form, that's easy, but what if you want it to be determined during runtime? This is a quite common story: doubleclick on article opens the detail form of that article, doubleclick on supllier opens that particular form. If you use a form variable, how do you say to the class which form should be opened? This will not work

Code:
Dim FormToOpen as Form 'you don't want to open it right away, you want to know when the time comes what form to open
FormToOpen = Form1

You could say FormToOpen = new Form1, but then 1. an instance would be immediately created, even if it never will be used: memory leak. 2. you can never close the form, you'll always have to use the same instance created when setting the form. Consequently, you can never open more than 1 instance using this way.

The solution: use the type. If you know the type, the Activator class will enable you to open (and return) an instance of that form at any time and as much times as you like. (The Activotor class exposes the function CreateInstance which returns an instance of the specified object)
The following code creates an instance of the type form1 and shows it.

Code:
CType(Activator.CreateInstance(GetType(Form1)), Form).Show()
. . . . . . . .. . . . . . . .. . . . . . . .


WMI Scripting

WMI (Windows Management Instrumentation) - Home

Introduction to WMI

Who will benefit from learning about WMI (Windows Management Instrumentation) commands? Here are the people who I had in mind as I wrote this section. Network managers who want to collect specific data from one (or more) server. IT professionals who what to know precisely what's happening in their Microsoft operating system. Those techies who love remote control without hassle. . . . . . . . . . . . . . . . . . . . . . . . . . .

------------------------------------------------

WMI Scripting can be used to retrieve many useful information related to running machine in a very easy way.


Tutorial Learning Points

1) Sometimes simple phrases have a beauty all of their own. Yet, often simple phrases hide all the hard work, what you do not see is all the experiments that did not work and all the extra code that you don't need. Perhaps the best way to re-enforce this theme is to point what NOT to put in the WQL clause.

2) ("Select * from Win32_LogicalDisk where objItem.FreeSpace > 10000"). No need for objItem, just the pure property name. As with all scripting learn to apply the punctuation correctly.

Example 2 - Select only File Systems that are NTFS.

The key WQL clause is where FileSystem = 'NTFS'")

("Select * from Win32_LogicalDisk where FileSystem = 'NTFS'")

Tutorial Learning Points

1) In truth, text filters are harder than numeric filters. The greatest problems are with the speech marks.

("Select * from Win32_LogicalDisk where FileSystem = 'NTFS'"). Again looks easy, but here are some mistakes. Where FileSystem = ' NTFS '". It is outrageous that blank spaces should cause problems - but they do.

2) Here is another error, note the wrong type of speech marks. where FileSystem = "NTFS"". The spacing is correct but NTFS should be bracketed by a pair of single speech marks, the double quotes draws an error message.

. . . . . .. . . . . .. . . . . .. . . . . .. . . . . .. . . . . .. . . . . .. . . . . .


Monday, January 19, 2009

Saving all the images from ImageList to Disk

Public Sub saveImageListToDisk()
Dim img As Image
Dim iCnt As Integer
For Each img In ImagList1.Images
iCnt = iCnt + 1
img.Save("C:\MyImages\" & iCnt & ".png")
Next
End Sub