Monday, May 18, 2009
ListBox Deselect Tip
Let lstBox be the listbox in a form then,
Private Sub lstBox_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lstBox.MouseDown
Try
If Not lstBox.SelectionMode = SelectionMode.None Then
lstBox.SelectedIndex = lstBox.IndexFromPoint(e.X, e.Y)
End If
If (Windows.Forms.Control.ModifierKeys And Keys.Control) <> 0 Then
If lstBox.SelectedIndex = lstBox.IndexFromPoint(e.X, e.Y) Then
LstbIMBoxList.SelectedIndex = -1
End If
End If
Catch ex As Exception
'
End Try
End Sub
This may fix the issue in many situations depending on the requirements.
Tuesday, May 5, 2009
Singleton class
Public Class MySingletonCls
Private Shared objSingle As MySingletonCls
Private Shared bIsCreated As Boolean
Private Sub New()
'Override the default constructor
End Sub
Public Shared Function CreateInstance() As MySingletonCls
If bIsCreated = False Then
objSingle = New MySingletonCls()
bIsCreated = True
Return objSingle
Else
Return objSingle
End If
End Function
End Class
Friday, May 1, 2009
Tip to fetch XML using LINQ
Function FetchXML(ByVal url As Uri) As XElement
Dim x = WebRequest.Create(url)
Using r = x.GetResponse, rs As New StreamReader(r.GetResponseStream)
Return XElement.Parse(rs.ReadToEnd)
End Using
Call the function with the location of the xml file to get the corresponding XML element, that can be used for needed purpose.
Tip to handle XML based info project
Upto this point, half is done. Now we just need to get a class which can carry this information for various purposes and which can be written directly to xml file and also can read data from xml file( easiest way is serialize/deserialize mechanism can be very useful)
Now to get exact class that can be serialized directly to xml can be easily obtained from xsd utility of Visual Studio. just open the Visual Studio Command Prompt, and get the xsd file for your xml file and then convert this file directly to required class file. So the steps that can be needed to get the class are as below:
1. Create xml file say 'MyXml.xml'
2. run xsd MyXml.xml, will result MyXml.xsd
3. run xsd MyXml.xsd /c /l:vb, will result MyXml.vb
[the class can be constructed in various ways by using xsd, for details refer xsd documentation.]
note, in step 2 and 3, if current directory for running the command is same that is containing the MyXml.xml file then use command as such, else specify the full path of the file(s). Also target for the resulting file also can be specified(refer xsd documentation).
now we are with a class for which an object can be easily serialized/deserialized. So create collection of objects to carry all the records, applly any type of manipulations and serialize with XMLSerializer to write it to xml directly without worring about most of the validations as most are handled by serializers.
Thursday, April 30, 2009
Protecting Assembly/DLL from illegal access tips
A friend assembly is an assembly that is permitted to access another assembly's Friend types and members.
So Its possible to protect illegal access to our secure functionality.
This is one of the solution that can be helpful in some situation.
The info of Friend Assemblies can found at the link http://msdn.microsoft.com/en-us/library/bb384772.aspx
Wednesday, April 29, 2009
Some Simple LINQ Examples for existing examples
creating an iterator that will give you the letters "a" to "z" as you enumerate it:
Dim letters(0 To 25) As Char
For i As Int32 = 0 To 25
letters(i) = ChrW(&H61 + i)
Next
By Using LINQDim letters = From i In Enumerable.Range(0, 26) _
Select ChrW(&H61 + i)
To read from a stream line-by-line,
search for a substring, and return the first couple of matches.strLine = myStreamReader.ReadLine
Do While strLine IsNot Nothing
If strLine.Contains(value) Then
count += 1
resultList.Add(strLine)
If count >= 3 Then Exit Do
End If
strLine = myStreamReader.ReadLine
Loop
Unfortunately, StreamReader doesn't have an
iterator that returns each line. Without something that is enumerable,
you can't form the basis of a LINQ query, so you're forced to write
your query imperatively. That's not as bad as it might first sound;
Building the query into the imperative procedural block does work,
although it's kind of on the ugly side.
If you have an extension method named Lines that
returns an IEnumerable with each item a line, then you can create a
query like this:Dim result = From line In myStreamReader.Lines _
Where line.Contains(value) _
Take 3
Tuesday, April 28, 2009
Tip for Alphablended area of image in list view
smallImgLst.ColorDepth=ColorDepth.Depth32Bit
largeImgLst.ColorDepth=ColorDepth.Depth32Bit
smallImgLst.ImageSize=New System.Drawing.Size(16,16)
largeImgLst.ImageSize=New System.Drawing.Size(32,32)
If there are some other tips to resolve such kind of issue then comments are invited for discussion.
Friday, April 24, 2009
Display Image in ComboBox
1. Set the property 'DrawMode' to OwnerDrawVariable
2. In the method that implement event 'DrawItem' of the combo box, for the case if index of current item is non empty then then draw the image. i.e.
If e.Index<>-1 Then
e.Graphics.DrawImage(imgList1.images(e.Index),e.Bounds.Left,e.Bounds.Top)
End If
3. In the method that implements the 'MeasureItem' event of the combo box the following can be used
e.ItemHeight =imgList1.ImgaeSize.Height
e.ItemWidth = imgList1.ImageSize.Width
These are the basic settings that are needed to display images in combo box, rest are depends on the requirements.
Drop the lines if you think that can be help better for similar kind of requirements.
Count Character Occurances In a String
Private Function countOccurance1(ByVal hayStack As String,ByVal needle As Char) As Integer
If String.IsNullOrEmpty(hayStack) Then Return 0
Dim chars() As Char = hayStack.ToCharArray
Dim count As Integer = 0
For Each ch As Char In chars
If ch = needle Then count += 1
Next
Return count
End Function
Private Function countOccurance2(ByVal hayStack As String, ByVal needle As String) As Integer
Return (Len(hayStack) - Len(Replace(hayStack, needle, ""))) / Len(needle)
End Function
Private Function countOccurance3(ByVal hayStack As String, ByVal needle As String) As Integer
Dim i As Long, sHayStack As String : sHayStack = hayStack
Do Until InStr(sHayStack, needle) = 0
sHayStack = Replace(sHayStack, needle, "", 1, 1) : i += 1
Loop
Return i
End Function
I've not tested which is more effiecient, but all provides the similar results for many number tests.
Wednesday, April 22, 2009
Open XML Format SDK 2.0
Open XML is an open ECMA 376 standard and is also approved as the ISO/IEC 29500 standard that defines a set of XML schemas for representing spreadsheets, charts, presentations, and word processing documents. Microsoft Office Word 2007, Excel 2007, and PowerPoint 2007 all use Open XML as the default file format.
The Open XML file formats are useful for developers because they use an open standard and are based on well-known technologies: ZIP and XML.
The Open XML Format SDK 2.0 is built on top of the System.IO.Packaging API and provides strongly typed part classes to manipulate Open XML documents. The SDK also uses the .NET Framework Language-Integrated Query (LINQ) technology to provide strongly typed object access to the XML content inside the parts of Open XML documents.
- Requirements:
Supported Operating Systems: Windows Server 2003; Windows Vista; Windows XP Service Pack 2
This download requires the following:
- The Microsoft .NET Framework version 3.5.
- Up to 120 MB of available disk space.
“Be aware that it’s a 90MB download (about 120MB installed). About 100MB of it is documentation. The library itself (DocumentFormat.OpenXml.dll) is 3.5MB” as specified in windowsclientdevelopment.
The Open XML Format SDK 2.0 went live on April 7, 2009, and is now up for grabs via the Microsoft Download Center. The SDK, and the underlying System.IO.Packaging API enables developers to leverage a simplified manipulation process involving OpenXML packages. And when it comes down to manipulation of OpenXML packages, version 2.0 of the SDK brings to the table support for strongly typed part and content classes.
“In version 1 of the Open XML SDK we released the Open XML Packaging API, which allows you to create, open and manipulate Open XML files at the package and part level. In the first CTP of version 2 we released the Open XML Low Level DOM and Stream Reading/Writing components, which allow you to create and manipulate objects within xml parts contained in an Open XML package. In the second CTP of version 2 we are providing schema level validation functionality,” revealed Brian Jones, Office program manager.
In addition, the April 2009 CTP of Open XML Format SDK 2.0 permits OpenXML documents to be validated. The validation functionality added to the latest version of the SDK is designed to permit developers to perform validation of Open XML docs in relation to variations of the Open XML Format. Essentially what validation support is designed to do is to help reduce the possibility of invalid or corrupt Open XML files.
“Manipulating Open XML Formats by using the Open XML Base layer makes it much easier for you to work on the Open XML files, but doing so does not guarantee the production of valid Open XML files. The new Schema Level Validation component provides a mechanism to help you discover Open XML errors within files and in your code. This component assists you in debugging and validating Open XML files based on the schemas,” Jones added.
This release includes a high-level DOM (document object model) for Open XML development, as well as several tools to streamline Open XML development:
* The OpenXmlDiff utility identifies differences in the markup in two Open XML documents.
* The Open XML Class Explorer helps you determine which strongly typed class to use for a specific task, and includes the text of the relevant section of the ECMA-376 spec for each class.
* The Open XML Document Reflector takes a target document as input, and with a few clicks it shows you the C# code needed to generate that document (or a section of it) with the Open XML SDK.
Sample code that supports the scenarios described in the series of articles titled Creating Documents by Using the Open XML Format SDK 2.0 (Community Technology Preview)
The first article is located at:
http://msdn.microsoft.com/en-us/library/dd440953.aspx
Some brief overview can be seen from the link
Thursday, April 16, 2009
Filling the DataGridView with all the files(upto deepest level) in specified directory
Retrieve All Files Info in the specified Folder (Using LINQ To Object)
Retrieve All Files in the specified Folder
If My.Computer.FileSystem.GetDirectoryInfo(rootDir).Name = "System Volume Information" ThenReturnEnd If
For Each recursiveDir As String In My.Computer.FileSystem.GetDirectories(rootDir)Call getCompleteFileList(recursiveDir, arrFileList)Next
For Each foundFile As String In My.Computer.FileSystem.GetFiles(rootDir,FileIO.SearchOption_
.SearchTopLevelOnly)arrFileList.Add(foundFile)Next
Tuesday, April 14, 2009
XML Operations: Create XML, Insert Data, Modify Data and Delete Data in XML File
- Create XML File
Private Sub CreateXmlFile()Dim xw As New XmlTextWriter("C:\mySampleXml.xml", System.Text.Encoding.UTF8)xw.WriteStartDocument()xw.WriteStartElement("nodes", "")xw.WriteStartElement("node", "")xw.WriteStartAttribute("id", "")xw.WriteString("1")xw.WriteEndAttribute()xw.WriteStartElement("child")xw.WriteString("sample10")xw.WriteEndElement()xw.WriteStartElement("child")xw.WriteString("sample11")xw.WriteEndElement()xw.WriteEndElement()xw.WriteStartElement("node", "")xw.WriteStartAttribute("id", "")xw.WriteString("2")xw.WriteEndAttribute()xw.WriteStartElement("child")xw.WriteString("sample20")xw.WriteEndElement()xw.WriteStartElement("child")xw.WriteString("sample21")xw.WriteEndElement()xw.WriteEndElement()xw.WriteStartElement("node", "")xw.WriteStartAttribute("id", "")xw.WriteString("3")xw.WriteEndAttribute()xw.WriteStartElement("child")xw.WriteString("sample30")xw.WriteEndElement()xw.WriteStartElement("child")xw.WriteString("sample31")xw.WriteEndElement()xw.WriteEndElement()xw.WriteEndElement()xw.Flush()xw.Close()Process.Start("C:\mySampleXml.xml")End Sub- Insert Data in XML File
Private Sub InsertDataToXmlFile()Dim xDoc As New XmlDocumentxDoc.Load("C:\mySampleXml.xml")Dim sourceRootNode As XmlNode = xDoc.SelectSingleNode("/nodes/node[@id=2]") 'Slects the root 'nodes'Dim newChildNode As XmlNode = xDoc.CreateNode(XmlNodeType.Element, "child", "")newChildNode.InnerText = "New Sample 22"'Child with attributeDim newChildNode1 As XmlNode = xDoc.CreateNode(XmlNodeType.Element, "child", "")Dim atr As XmlAttribute = xDoc.CreateAttribute("childID")atr.Value = "ch1"newChildNode1.InnerText = "New Sample 23"newChildNode1.Attributes.Append(atr)sourceRootNode.AppendChild(newChildNode)sourceRootNode.AppendChild(newChildNode1)xDoc.Save("C:\mySampleXml.xml")Process.Start("C:\mySampleXml.xml")End Sub- Modify Data in XML File
Private Sub ModifyDataInXmlFile()Dim xDoc As New XmlDocumentxDoc.Load("C:\mySampleXml.xml")Dim desiredNode As XmlNode = xDoc.SelectSingleNode("/nodes/node[@id=2]")Dim childNodes As XmlNodeList = desiredNode.SelectNodes("./child") ''.' specifies the current node and '/child' specifies child nodesFor Each curNode As XmlNode In childNodesIf curNode.HasChildNodes = True ThenIf curNode.Attributes.Count > 0 ThenIf curNode.Attributes("childID").Value = "ch1" ThencurNode.InnerText = "Updated Sample 23"End IfEnd IfEnd IfNextxDoc.Save("C:\mySampleXml.xml")Process.Start("C:\mySampleXml.xml")End Sub- Delete Data in XML File
Private Sub RemoveDataFromXmlFile()Dim xDoc As New XmlDocumentxDoc.Load("C:\mySampleXml.xml")Dim desiredNode As XmlNode = xDoc.SelectSingleNode("/nodes/node[@id=2]")Dim childNodes As XmlNodeList = desiredNode.SelectNodes("./child") ''.' specifies the current node and '/child' specifies child nodesFor Each curNode As XmlNode In childNodesIf curNode.HasChildNodes = True ThenIf curNode.Attributes.Count > 0 ThenIf curNode.Attributes("childID").Value = "ch1" ThencurNode.ParentNode.RemoveChild(curNode)End IfEnd IfEnd IfNextDim anotherDesiredNode As XmlNode = xDoc.SelectSingleNode("/nodes/node[@id=2]")anotherDesiredNode.ParentNode.RemoveChild(anotherDesiredNode)xDoc.Save("C:\mySampleXml.xml")Process.Start("C:\mySampleXml.xml")End SubPlease give any suggestions for better coding. Also add code for other possible cases that can be possible for other various situations.- Create XML File
Monday, April 13, 2009
Convert Bitmap picture to Byte() and back to picture
Let pic be the picture as
Dim pic as Bitmap
and Let bytes() be the required converted picture into byte.
Dim bytes() As Byte
Then the code to convert the picture to bytes as below:
If picture IsNot Nothing Then
Dim BitmapConverter As System.ComponentModel.TypeConverter = System.ComponentModel.TypeDescriptor.GetConverter(picture.[GetType]())
bytes= DirectCast(BitmapConverter.ConvertTo(picture, GetType(Byte())), Byte())
Else
bytes= Nothing
End If
Also to get picture back to bitmap format from the byte object the code will be like as below:
If bytes IsNot Nothing ThenUses:
pic= New Bitmap(New MemoryStream(value))
Else
pic = Nothing
End If
The picture can't save directly in XML file and is required to be converted to like bytes only then can be saved to XML file. Similarly later it can be retrieved back to picute in bitmap format by using the above procedure.
Thursday, March 12, 2009
SendKys
System.Windows.Forms.SendKeys.Send("your key comes here")
there are various codes for various keys. The list of syntax of all the keys is given below:
Key Code
BACKSPACE {BACKSPACE}, {BS}, or {BKSP}
BREAK {BREAK}
CAPS LOCK {CAPSLOCK}
DEL or DELETE {DELETE} or {DEL}
DOWN ARROW {DOWN}
END {END}
ENTER {ENTER}or ~
ESC {ESC}
HELP {HELP}
HOME {HOME}
INS or INSERT {INSERT} or {INS}
LEFT ARROW {LEFT}
NUM LOCK {NUMLOCK}
PAGE DOWN {PGDN}
PAGE UP {PGUP}
PRINT SCREEN {PRTSC}
RIGHT ARROW {RIGHT}
SCROLL LOCK {SCROLLLOCK}
TAB {TAB}
UP ARROW {UP}
F1 {F1}
F2 {F2}
F3 {F3}
F4 {F4}
F5 {F5}
F6 {F6}
F7 {F7}
F8 {F8}
F9 {F9}
F10 {F10}
F11 {F11}
F12 {F12}
F13 {F13}
F14 {F14}
F15 {F15}
F16 {F16}
To specify keys combined with any combination of the SHIFT, CTRL, and ALT keys, precede the key code with one or more of the following codes:
Key Code
SHIFT +
CTRL ^
ALT %
Tuesday, February 17, 2009
Getting String object as Stream IO.Stream object
Dim msObj As MemoryStream = New MemoryStream(System.Text.Encoding.ASCII.GetBytes(myString))
MyObjThatNeedsStreamObj.Load(CType(msObj , Stream))
Tuesday, January 27, 2009
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:
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:
. . . . . . . .. . . . . . . .. . . . . . . .
WMI Scripting
WMI (Windows Management Instrumentation) - Home
Introduction to WMIWho 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
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