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

Friday, October 10, 2008

Get the Product Code of the Installed Product

[Not Sure but can be useful]
Private Function getInstalledProductCode(ByVal InstalledProductNameStr As String) As String
Dim retStr As String = "-1"
Try
Dim installer As Object
installer = CreateObject("WindowsInstaller.Installer")
Dim products As Object = installer.Products
For Each strProductCode As String In products
Dim strProductName As String = installer.ProductInfo(strProductCode, "InstalledProductName")
If strProductName.Contains(InstalledProductNameStr) = True Then
retStr = strProductCode
End If
Next
Catch ex As Exception

End Try
Return retStr
End Function

Generate Globally Unique Identifier (GUID)

Private Function GUID() As String

Return (System.GUID.NewGuid().ToString())

End Function

Monday, October 6, 2008

Attributes with VB.NET

1. Creating and Using Custom Attributes with VB.NET

The common language runtime allows programmers to add descriptive declarations to programming elements called Attributes. Attributes are like annotations that can be applied to assemblies, types, methods, or properties. Attributes are emitted into the assembly metadata and they can be extracted by using Reflection services of the .NET framework.

2. Attributes Programming in VB.NET

An attribute is a new code level language construct in all major .NET languages. It provides integration of declarative information to assemblies, classes, interfaces, members, etc. at the code level. The information can then be used to change the runtime behavior or collect organizational information. . . . . . .

Method Synchronization [MethodImplAttribute]

Code attributes in the Dot Net Framework can sometimes make programming easier. The MethodImplAttribute is one example of the hundreds of different attributes that you can use. It is in the System.Runtime.CompilerServices namespace. This attribute is particularly interesting to synchronization because it can synchronize an entire method with one simple command.

If you place the attribute before a function and supply the MethodImplOptions. Synchronized enumeration in the constructor, the entire method will be synchronized when called. The compiler will create output that wraps the whole function, SynchronizedMethodName, in a Monitor. Enter and Monitor. Exit block. When a thread starts to enter the method it will acquire a lock. Upon exiting the method, it will release the lock. Here is an example of using the attribute.

"<" METHODIMPLATTRIBUTE(METHODIMPLOPTIONS.SYNCHRONIZED) ">"

Private Sub SynchronizedMethodName()

End Sub

This attribute should only be used when an entire function needs to be synchronized, so it is rarely used. If you can exit the synchronized block of code before the end of the method or wait to enter it to the middle of the method, Monitor should be used, as the attribute would waste processing cycles by locking the whole method and not just what needs to be synchronized.


Note: There are two SynchronizationAttribute classes. One is in
System.EnterpriseServices and the other is in
System.Runtime.Remoting.Contexts. The first can only be used on
classes derived from ServicedComponent. The second can only be used on
classes derived from ContextBoundObject.