[ Pobierz całość w formacie PDF ]
.Every time the Button1 control on the form is clicked, theButton1_Click subroutine is activated.To react to the Click event of the button, you must insert theappropriate code in this subroutine.The name of the subroutine is made up of the name of the control, followed by an underscoreand the name of the event.This is just the default name, and you can change it to anything you like(such as EvaluateLanguage, for this example, or StartCalculations).What makes this subroutine anevent handler is the keyword Handles at the end of the statement.The Handles keyword tells thecompiler what event this subroutine is supposed to handle.Button1.Click is the Click event of theButton1 control.If there were another button on the form, the Button2 control, you d have to writecode for a subroutine that would handle the Button2.Click event.Each control recognizes manyevents; for each control and event combination, you can provide a different event handler.Of course,we never program every possible event for every control.Note As you will soon realize, the controls have a default behavior and handle the basic events on their own.TheTextBox control knows how to handle keystrokes.The CheckBox control (a small square with a check mark) changes stateby hiding or displaying the checkmark every time it s clicked.The ScrollBar control moves its indicator (the button in themiddle of the control) every time you click one of the arrows at the two ends.Because of this default behavior of the controls,you need not supply any code for the events of most controls on the form.Rename Button1_Click subroutine to EvaluateLanguage.However, you shouldn t change thename of the event this subroutine handles.If you change the name of the control after you haveinserted some code in an event handler, the name of the event handled by the subroutine will beautomatically changed.The name of the subroutine, however, won t change.Let s add some code to the Click event handler of the Button1 control.When this button isclicked, we want to examine the text on the control and, if it s Visual Basic , display a message; ifnot, we ll display a different message.Insert the lines of Listing 1.1 between the Private Sub andEnd Sub statements.(I m showing the entire listing here, but there s no reason to retype the first andlast statements.)www.sybex.comCopyright ©2002 SYBEX, Inc., Alameda, CA2877c01.qxd 11/11/01 4:14 PM Page 15YOUR FIRST VB APPLICATION 15Listing 1.1: Processing a User-Supplied StringPrivate Sub EvaluateLanguage_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles Button1.ClickDim language As Stringlanguage = TextBox1.TextIf language = Visual Basic ThenMsgBox( We have a winner! )ElseMsgBox(language & is not a bad language. )End IfEnd SubHere s what this code does.First, it assigns the value of the TextBox1 control to the variable lan-guage.A variable is a named location in memory, where a value is stored.This memory location canbe read later in the code or set to a different value.Variables are where we store the intermediateresults of our calculation when we write code.Then the program compares the value of the languagevariable to the literal Visual Basic , and depending on the outcome of the comparison, it displaysone of two messages.The MsgBox() function displays the specified message in a small window withthe OK button.Users can view the message and then click the OK button to close the message box.Even if you re not familiar with the syntax of the language, you should be able to understandwhat this code does.Visual Basic is the simplest.NET language, and we will discuss the variousaspects of the language in detail in the following chapters.In the meantime, you should try to under-stand the process of developing a Windows application: how to build the visible interface of theapplication, and how to program the events to which you want your application to react.Making the Application More RobustThe code of our first application isn t very robust.If the user doesn t enter the string with the exactspelling shown in the listing, the comparison will fail.We can convert the string to uppercase andthen compare it to VISUAL BASIC to eliminate differences in case.To convert a string to upper-case, use the ToUpper method of the string class.The following expression returns the string storedin the language variable, converted to uppercase:language.ToUpperWe should also assume that the user may enter VB or VB.NET , so let s modify our code asshown in Listing 1.2.Listing 1
[ Pobierz całość w formacie PDF ]
-
Menu
- Index
- Caine Rachel Czas Wygnania 02 Nieznana
- Kosinski Jerzy Wystarczy Byc (SCAN dal 995)
- Jordan Robert Czarna Wieza (SCAN dal 930)
- Science Fiction (31) pazdziernik 2003
- Makuszynski Kornel Przyjaciel wesolego diabla
- 0275994317 The 9 11 Encyclopedia
- Krzysztof Borun, Andrzej Trepka Proxima
- Saul Bellow Dar Humboldta
- Pierscien z krwawnikiem Helena Sekula
- Roger Zelazny Mroz i Ogien (2)
- zanotowane.pl
- doc.pisz.pl
- pdf.pisz.pl
- filantrop.pev.pl