Programming with Microsoft Visual Basic 2015 (MindTap Course List)
Programming with Microsoft Visual Basic 2015 (MindTap Course List)
7th Edition
ISBN: 9781285860268
Author: Diane Zak
Publisher: Cengage Learning
Expert Solution & Answer
Book Icon
Chapter 4.LC, Problem 1RQ
Program Description Answer

The text box’s “KeyPress” event occurs when pressing any key in the keyboard.

Hence, correct answer is option “D”.

Expert Solution & Answer
Check Mark

Explanation of Solution

The KeyPress event:

If a key is pressed within a control (i.e. the control has been focused), then the KeyPress event will occur.

  • The keyPress event procedure sends the pressed key’s character to the “e” parameter of the keyPress event.
    • The acceptance of pressing key has been handled by using the property “Handled”.

e.Handled

  • “Handled” property contains the Boolean values “True” or “False”.
  • If the “Handled” property of the control is “true”, then the control cancels the pressed keys.

  e.Handled = True

  • If the “Handled” property of the control is “false”, then the control accepts the pressed keys.

  e.Handled = False

Example:

The sub procedure “CancelKeys” allows the text boxes “txtCode” and “txtSales” to accept only the numbers and the backspace key.

Private Sub CancelKeys(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _

Handles txtCode.KeyPress, txtSales.KeyPress

'Allows only numbers and the Backspace key in the 'text box

If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> ControlChars.Back Then

    'Cancel the key

            e.Handled = True

        End If

    End Sub

The above example is used to accept only the numeric value keys and the backspace key.

Sample Output

Output:

Note: Here, it does not accept the keys other than the numeric and backspace key.

Enter “12” as code. Then, press backspace key to delete the 2.

Screenshot of “Bonus Calculator” form

Programming with Microsoft Visual Basic 2015 (MindTap Course List), Chapter 4.LC, Problem 1RQ , additional homework tip  1

The 2 will be deleted after pressing the backspace key.

Screenshot of “Bonus Calculator” form

Programming with Microsoft Visual Basic 2015 (MindTap Course List), Chapter 4.LC, Problem 1RQ , additional homework tip  2

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
The Paste Special dialog box offers the ____ option, which enables you to copy and paste a cell range that contains one or more blank cells where the blank cells are not pasted over any existing values in the range into which they are pasted.
ListBox controls have an __________ method that erases all the items in the Items property. a. Items.Erase b. Items.Remove c. Items.Clear d. Items.Empty
You can modify the tab order by changing a control’s _______ property.
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:9781337102124
Author:Diane Zak
Publisher:Cengage Learning