Monday, May 18, 2009

ListBox Deselect Tip

In ListBox, I've found that we can not deselect a entry once we selected any one of the entry in the list, even selecting no entry area. But I've found one of the solution to fix this issue as :
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

End Function

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

Many times we need to manage the projects that just process specific information. Like if we consider a project that handles information about student(name,rno,course,class). So each student will have this information. To very easy way, if we create an XML file with one of the student. Creating a xml with this info is not very tough job.
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.