[ Pobierz całość w formacie PDF ]
.A control that displays multiple items, like the ListBox or theComboBox control, returns the currently selected item in the Text property.TabStop As you know, only one control at a time can have the focus on a form.To move thefocus from one control to the other on the same form, you press the Tab key (and Shift+Tab tomove the focus in reverse order).The TabStop property determines whether the control belongsto the so-called tab order.If True (which is the default value), you can move the focus to the con-trol with the Tab key.If False, the control will be skipped in the tab order.TabIndex A numeric value that determines the position of the control in the Tab order.The con-trol with the smallest TabIndex value is the one that has the focus when the form is first loaded.Ifyou press Tab once, the focus will be moved to the control with the next larger TabIndex value.Visible Sometimes we want to make a control invisible.We do so by setting its Visible prop-erty to False (the default value of the property is True).www.sybex.comCopyright ©2002 SYBEX, Inc., Alameda, CA2877c01.qxd 11/11/01 4:14 PM Page 2828 Chapter 1 GETTING STARTED WITH VB.NETA Few Common EventsAs you have already seen, and will also see in the coming chapters, much of the code of a Windowsapplication manipulates the properties of the various controls on the form.The code of the applica-tion resides in selected event handlers.Each control recognizes several events, but we rarely programmore than one event per control.In most cases, most of the controls on the form don t have anycode behind them.The events that are most frequently used in programming Windows applicationsare shown next.Click This is the most common event in Windows programming, and it s fired when a controlis clicked.DoubleClick Fired when the control is double-clicked.Enter Fired when the control received the focus.Leave Fired when the control loses the focus.We usually insert code to validate the control scontent in this event s handler.MouseEnter Fired when the mouse pointer enters the area of the control.This event is fired once.If you want to monitor the movement of the mouse over a control, use the MouseMove event.MouseLeave This is the counterpart of the MouseEnter event, and it s fired when the mousepointer moves out of the area of the control.XXXChanged Some events are fired when a property changes value.These events includeBackColorChanged, FontChanged, VisibleChanged, and many more.The control s properties canalso change through code, and it s very common to do so.To set the text on a TextBox controlfrom within your code, you can execute a statement like the following:TextBox1.Text = a new captionYou may wish to change the background color of a TextBox control if the numeric value dis-played on it is negative:If Val(TextBox.Text) 12 ThenexpYear = expYear + 1expMonth = 1End IfThe Month property of the Date type returns the month of the date to which it s applied as anumeric value.Some programmers prefer the multiple-line syntax of the If& Then statement, even if itcontains a single statement, because the code is easier to read.The block of statements between theThen and End If keywords form the body of the conditional statement, and you can have as manystatements in the body as needed.www.sybex.comCopyright ©2002 SYBEX, Inc., Alameda, CA2877c03.qxd 11/11/01 4:15 PM Page 137FLOW-CONTROL STATEMENTS 137If& Then& ElseA variation of the If& Then statement is the If& Then& Else statement, which executes one block ofstatements if the condition is True and another block of statements if the condition is False.Thesyntax of the If& Then& Else statement is as follows:If condition Thenstatementblock1Elsestatementblock2End IfVisual Basic evaluates the condition; if it s True, VB executes the first block of statements and thenjumps to the statement following the End If statement.If the condition is False, Visual Basic ignoresthe first block of statements and executes the block following the Else keyword.Another variation of the If& Then& Else statement uses several conditions, with the ElseIfkeyword:If condition1 Thenstatementblock1ElseIf condition2 Thenstatementblock2ElseIf condition3 Thenstatementblock3Elsestatementblock4End IfYou can have any number of ElseIf clauses.The conditions are evaluated from the top, and if oneof them is True, the corresponding block of statements is executed.The Else clause will be executedif none of the previous expressions are True.Listing 3.10 is an example of an If statement withElseIf clauses.Listing 3
[ Pobierz całość w formacie PDF ]