Interested on Microsoft technologies? Read Visual Studio Dot Net

Tutorial 7: Interaction Commands and Functions

Reason of this project:
The Microsoft.VisualBasic.Interaction class exposes many useful commands and methods that were available in Visual Basic like AppActivate, Beep, Callbyname...
This tutorial shows how to work with some of them.
Project details:
1- From VB 6.0 to VB .net 2.0
2- Useful interaction commands
3- Samples of interaction commands

1- From VB 6.0 to VB .net 2.0

There was several changes on VB .net (it's a big change and that's clear). Although in an effort to make the transition from VB 6 to VB 2005 as easy as possible, Microsoft has gathered most of the Visual Basic 6.0 commands and functions in the Microsoft.VisualBasic.dll assembly.
Some of the commands and functions was deleted, other still the same and others changed to the .net syntax.
For Example:
VbCrlf which is used on VB 6.0 (as a separator) still alive on VB .net 2.0 but a new syntax is already available for use.
ControlChars.crlf is the new method
As you are using VB .net I recommend that use the new syntax, and I'm going to introduce some of the important change between the old and the new syntax.

2- Useful interaction commands

VB 6.0 syntax contain many useful interaction commands and function like the Message Box for example. Those commands still alive but have been improved in the .net 2.0 version.

Shell Function:
The shell function runs an executable program, it works like the command line so you can use it as a command line.
The shell function is capable of returning an integer indicating if the program still running or not. The shell function was expanded in the .net 2.0 version and supports an additional argument that enables you to specify whether to wait until the shelled program terminates with an optional timeout.
Use of the function
Shell (Application Path, Style, Wait, Timeout)

Example:
' Run Notepad.exe and wait until the user terminates it.
Shell ("notepad", AppWinStyle.NormalFocus, True)


AppActivate Function:
AppActivate is a cool function that may be helpful if you are developing application for Windows. Now let see how this works and what it do. The AppActivate function as it names, activate a currently running application.
How it activates? Simply by giving the focus to the application Window.
The function is quite useful and require only to know the windows caption!
Use of the function
AppActivate ( Title) or AppActivate (ProcessID)

NB: You have to choose either to use the title or the processID and not both!

Now let's see this example
' Activate the NotePad Window
AppActivate("Untitled - Notepad")

Now as we know those simple functions we can use them to build helpful projects and application. Now see the sample of the interaction commands.

3- Samples of interaction commands

This sample is exactly a direct (little expanded) application of the two functions that we have studied together right now.
There's only one sample and if you run it you'll get a window with 2 buttons.
Each button introduce a sample for each function. The button caption let you distinguish them.

The Shell sample:
Run a notepad application and return a message if the NotePad was closed.
If the timeout was exceeded (5 seconds), return a message that the NotePad still running
The AppActivate:
The following sample run a notepad with no focus and use AppActivate to give it the focus.
So the sample use both of the functions

Download the source code

The Zip file contains:
-The sample source code
-The readme.txt file
-The tutorial.txt file

Still have question:
Go to our blog (http://thedotnetsource.blogspot.com). Select the tutorial post. Post a comment describing your problem.
If you have a general question, we highly recommend the MSDN Forums as the best Dot Net forums in the net.

Bookmark and share this tutorial
Related Ads

0 comments:

Related articles