Saturday, June 30, 2007

Opening & Closing Notepad using .Net

Opening & Closing of Application using .Net / Create New process in .Net / Shell Function in .Net

A program is not Userinterface and database connections alone, it also needs to interact with other applications. Many a times, this interaction happens internally, like updating Word Template, Printing out a document etc, but there are times where one needs to open an document through the Application.

Let us have a sample app that opens a Notepad on click of a command button. We have a sample form with a button (see below).



Now add the process control from the Components Control (see below)





This control has no design features and will be used in run-time and it straightway gets docked in the tray (see below)




Change the following properties of the control.



This can also be changed during run time.



Now in the Button Click event write the following code:

Private Sub ButtonNotepad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles loadNotepadButton.Click
' http://dotnetdud.blogspot.com/
ProcessNotepad.EnableRaisingEvents = True
ProcessNotepad.Start()

End Sub


The EnableRaisingEvents property indicates whether the component should be notified when the operating system has shut down a process. The EnableRaisingEvents property is used in asynchronous processing to notify your application that a process has exited. To force your application to synchronously wait for an exit event (which interrupts processing of the application until the exit event has occurred), use the WaitForExit method.

This will start the Notepad Application. If you want receive the closure of application use the following event

Private Sub ProcessNotepad_Exited(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProcessNotepad.Exited
' Coded for http://dotnetdud.blogspot.com/
MessageBox.Show("Notepad has been closed ", "Dot Net Tips & Tricks", MessageBoxButtons.OK)
End Sub

If you want to close the application use the kill method.

Private Sub ButtonClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles closeNotepadButton.Click
If ProcessNotepad.HasExited = False Then
ProcessNotepad.Kill()
End If
End Sub


We have used Notepad, as it is simple and easy. Try it with other stuff!!

See Also:

Show All Processes using VBA

Run a VB6.0 Executable from Excel/Word Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

No comments:

Post a Comment

kbAlertz.com :: Visual Studio 2005

kbAlertz.com :: Visual Studio 2008

kbAlertz.com :: Visual Basic 2005