MaximumPC 2004 09

(Dariusz) #1

How To


 MA XIMUMPC SEPTEMBER 2004


4 Tie the new GUI to the program


The GUI is designed, so let’s get down
to coding. Because this How-To is an
introduction to Windows Forms pro-
gramming, we’re going to show you
the fastest, easiest way to write code to
handle Form1’s button clicks.
In this project we’re going to reuse
the Cryptography class from the previ-
ous programming How-To (if you don’t
have that code, you can download
it from http://www.maximumpc.com/images/
programming_files.zip ). Code reuse is
one of the nice features of object-
oriented programming (OOP), which
was covered in the previous program-
ming How-To. We need to import the
Cryptography class file (Cryptography.
vb) from the MaxPC01 project into our
new MaxPC02 project.
To do so, right-click the project file
MaxPC02 in Solution Explorer and then
select Add, Add Existing Item from the
context menu. Use the dialog box to
navigate to the folder where you have
saved the MaxPC01 project and then
select the Cryptography.vb file. The file
will appear in Solution Explorer under
the MaxPC02 project and will also be
copied to the MaxPC02 directory.
Let’s start off with something easy.
Double-click btnExit. This will take you
to the code view of Form1 and your
cursor should be positioned inside a
special type of method, an event
handler named “btnExit_Click.” Clicking

the Exit button causes or “raises”
the Click event, and the code in this
method “handles” the Click event for
btnExit. As the name suggests, clicking
btnExit will allow us to halt execution
of our application. A really simple way
to achieve this goal is by using
“Application.Exit(),” so that’s the only
code needed for this event handler.
Note that because there’s nothing in
the parenthesis, we’re not giving the
Application.Exit function any variables.
Luckily, Application.
Exit doesn’t require
variables, whenever
it’s called, the app
simple closes.
Now that your
application actually
has some
functionality, you can
test it if you’d like.
From within the IDE,
click the “Start”
button, which is
located in the top
menu area. Visual
Studio will build and,
if there are no errors,
run MaxPC02 for you.
Once you’re finished
gawking at the form,
press the Exit button
and get back to work!

5 Encrypt, decrypt, rinse, repeat


Now, let’s look at how the encryption
function actually works. Double-
click btnEncrypt, which tells Visual
Studio to generate the skeleton of the
btnEncrypt_Click event handler. Turn
your eyes to the sample code on the
next page, and we’ll step through the
code and explain what the function
actually does.
The first thing the program checks
is whether the user has entered any
plaintext by using the methods and
properties of the TextBox class. If you
didn’t catch on earlier, the controls
that you added to your form weren’t
static, immutable widgets but dynamic

class members that
are instantiated
when you run
your program. For
example, txtPlaintext
is a member of
the TextBox class
and has a property
named Text. Not only
can we set the value
of this property at
design time, we can
also get the value of
this property when
the program is run.
This means that

DEFINING TERMS


EVENT: A notification or signal
that is generated by a user action,
such as a button click or a key
press. Events can also be raised by
internal application activities, such
as saving a file or an incoming IM.

CODE VIEW: An editable view of
the code that makes the Windows
Form work.

EVENT HANDLER: A method that
is invoked because of an event.

METHOD: The procedures associ-
ated with an object.

Visual Basic makes it easy to reuse old code. All you have to
do is browse to the location of the previous programming
How-To and add the cryptography.vb file to your new project.

DEFINING TERMS


RUNTIME: Runtime is the moment when a program is
loaded into computer memory.

MESSAGEBOX: A MessageBox in .NET is a small form
used to alert or inform the user using text, icons, or sound.

INTELLISENSE: IntelliSense is a feature of Microsoft’s
Visual Studio that displays information as you type in the
code editor. IntelliSense can supply syntax information as
well as complete words and help you beautify code.

CLASS: A named entity composed of related data and the
methods that act on that data. A class is to an object what
a blueprint is to a house.

OBJECT: An instance or occurrence of a class that has
been created during program execution. Think of a cookie
cutter as the class and the cookies as the objects.
Free download pdf