March 4, 2009

Vb.net - Enter only Character Values

Private Sub txtCity_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
Handles txtCity.KeyPress
e.Handled = OnlyCharacter(e.KeyChar)
End Sub

Public Function OnlyCharacter(ByVal Value As String) As Boolean
If (Microsoft.VisualBasic.Asc(Value) <> 90) _
And (Microsoft.VisualBasic.Asc(Value) <> 122) Then
'space accepted
If (Microsoft.VisualBasic.Asc(Value) <> 32) Then
OnlyCharacter = True
End If
End If
If (Microsoft.VisualBasic.Asc(Value) = 8) Then
OnlyCharacter = False
End If
End Function

Vb.net - Enter only Numeric Values

Private Sub txtbox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtbox1.KeyPress
e.Handled = OnlyNumeric(e.KeyChar)
End Sub

Public Function OnlyNumeric(ByVal Value As String) As Boolean
OnlyNumeric = True
If (Microsoft.VisualBasic.Asc(Value) > 47) And (Microsoft.VisualBasic.Asc(Value) < 59) Then
OnlyNumeric = False
End If
If (Microsoft.VisualBasic.Asc(Value) = 8) Then
OnlyNumeric = False
End If
End Function

Vb.net - Save File Dialog box

dim strSaveFile as string

With SaveFileDialog1
.CheckFileExists = False
.Filter = "Word Documents(*.doc)*.doc"
.FilterIndex = 1
If .ShowDialog() = Windows.Forms.DialogResult.OK Then
strSaveFile = .FileName()
End If
End with

Vb.net - Generate word file from world file template

Private Sub GenrateWord()
Try
Dim openFileDialog As New OpenFileDialog
Dim a() As Object = {}
openFileDialog.FileName = System.AppDomain.CurrentDomain.BaseDirectory() & "Final.doc"
Dim filename As Object = openFileDialog.FileName
Dim wordType As Type = Type.GetTypeFromProgID("Word.Application")
Dim wordApplication As Object = Activator.CreateInstance(wordType)
wordType.InvokeMember("Visible", Reflection.BindingFlags.SetProperty, Nothing, wordApplication, New Object() {False})
Dim wordDocuments As Object = wordType.InvokeMember("Documents", Reflection.BindingFlags.GetProperty, Nothing, wordApplication, a)
Dim wordDocument As Object = wordType.InvokeMember("Open", Reflection.BindingFlags.InvokeMethod, Nothing, wordDocuments, New Object() {filename})
Dim activeWindow As Object = wordDocument.[GetType]().InvokeMember("ActiveWindow", Reflection.BindingFlags.GetProperty, Nothing, wordDocument, Nothing)
Dim selection As Object = activeWindow.[GetType]().InvokeMember("Selection", Reflection.BindingFlags.GetProperty, Nothing, activeWindow, Nothing)
selection.[GetType]().InvokeMember("WholeStory", Reflection.BindingFlags.InvokeMethod, Nothing, selection, Nothing)
selection.[GetType]().InvokeMember("Copy", Reflection.BindingFlags.InvokeMethod, Nothing, selection, Nothing)
wordDocument.[GetType]().InvokeMember("Close", Reflection.BindingFlags.InvokeMethod, Nothing, wordDocument, Nothing)
wordApplication.[GetType]().InvokeMember("Quit", Reflection.BindingFlags.InvokeMethod, Nothing, wordApplication, Nothing)
Dim data As IDataObject = Clipboard.GetDataObject()
Dim text As String = data.GetData(DataFormats.Rtf, True).ToString()

Dim fs As New FileStream(strSaveFile, FileMode.Create, FileAccess.Write)
'declaring a FileStream and creating a document file named file with access mode of writing
Dim s As New StreamWriter(fs)
'creating a new StreamWriter and passing the filestream object fs as argument
text = text.Replace("{TODate}", System.DateTime.Now.Year().ToString())
s.Write(text)
'writing text to the newly created file
s.Close()
MessageBox.Show("Process has been done Successfully.", Msgboxtitle, MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As Exception
MessageBox.Show(ex.Message, Msgboxtitle, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub