1 / 120

Chapter 3 – Fundamentals of Programming in Visual Basic

Chapter 3 – Fundamentals of Programming in Visual Basic. 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output. 3.1 Visual Basic Controls. Invoking Visual Basic Text Box Control Button Control Label Control List Box Control Name Property

candra
Download Presentation

Chapter 3 – Fundamentals of Programming in Visual Basic

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 3 – Fundamentals of Programming in Visual Basic • 3.1 Visual Basic Controls • 3.2 Visual Basic Events • 3.3 Numbers • 3.4 Strings • 3.5 Input and Output

  2. 3.1 Visual Basic Controls • Invoking Visual Basic • Text Box Control • Button Control • Label Control • List Box Control • Name Property • Fonts / Auto Hide • Positioning and Aligning Controls

  3. Make a small program or two The following slides show screen shots of the compiler, I’ll keep them up for review. Section 3.3 Numbers Next In-Class Example

  4. Visual Basic Start Page

  5. Start a New Project

  6. New Project Dialog Box

  7. Initial Visual Basic Screen

  8. Toolbox

  9. 3 Ways to Place a Control from the Toolbox onto the Form Window • Double-click • Drag • Click, Point, and Drag

  10. Four Controls at Design Time Text box To select a control, click on it. Sizing handles will appear when a control is selected.

  11. Text Box Control • Used for input and output • When used for output, ReadOnly property is set to True Tasks button Sizing handles

  12. Properties Window Press F4 to display the Properties window for the selected control. Categorized view Alphabetical view

  13. Properties Window Selected control Settings Properties

  14. Some Often Used Properties • Text • Autosize • Font.Name • Font.Size • ForeColor • BackColor • ReadOnly

  15. Setting Properties • Click on property name in left column. • Enter its setting into right column by typing or selecting from options displayed via a button or ellipses.

  16. Setting the ForeColor Property • Click on ForeColor. • Click on button at right of settings box. • Click on Custom tab to obtain display shown. • Click on a color.

  17. Font Property • Click on Font in left column. • Click on ellipsis at right of settings box to obtain display shown, • Make selections.

  18. Button Control • The caption on the button should indicate the effect of clicking on the button. • Text property determines caption.

  19. Add an "access key"

  20. Label Control • Used to identify the contents of a text box. • Text property specifies caption. • By default, label automatically resizes to accommodate caption on one line. • When the AutoSize property is set to False, label can be resized manually. Used primarily to obtain a multi-rowed label.

  21. List Box Control • Initially used to display several pieces of output. • Later used to select from a list.

  22. The Name Property • How the programmer refers to a control in code • Setting for Name property near top of Properties window. • Name must begin with a letter, be less than 215 characters long, and may include numbers and letters. • Use appropriate 3- or 4-character naming prefix

  23. Control Name Prefixes

  24. Renaming the Form • Initial name is Form1 • The Solution Explorer window lists a file named Form1.vb. • To rename the form, change the name of this file to newName.vb • newName should begin with prefix frm.

  25. Fonts • Proportional width fonts take up less space for "I" than for "W" – like Microsoft Sans Serif • Fixed-width fonts take up the same amount of space for each character – like Courier New • Fixed-width fonts are good for tables.

  26. Auto Hide • Hides Toolbox when not in use • Vertical push pin icon indicates auto hide is disabled. • Click the push pin to make it horizontal and enable auto hide. Push pin

  27. Positioning Controls Proximity line

  28. Aligning Controls Snap line

  29. Aligning Controls Snap line

  30. 3.2 Visual Basic Events • An Event Procedure Walkthrough • Properties and Event Procedures of the Form • The Header of an Event Procedure

  31. Event • An event is an action, such as the user clicking on a button • Usually, nothing happens in a Visual Basic program until the user does something and generates an event. • What happens is determined by statements.

  32. Sample Statements • txtBox.ForeColor = Color.Red • txtBox.Visible = True • txtBox.Text = “Hello World” General Form: controlName.property = setting

  33. Sample Form txtFirst txtSecond btnRed

  34. Focus • When you click on a text box, a cursor appears in the text box, and you can type into the text box. • Such a text box is said to have the focus. • If you click on another text box, the first text box loses the focus and the second text box receives the focus.

  35. Examples of Events • btnShow.Click • txtBox.TextChanged • txtBox.Leave General Form: controlName.event

  36. The three steps in creating a Visual Basic program: • Create the interface; that is, generate, position, and size the objects. • Set properties; that is, configure the appearance of the objects. • Write the code that executes when events occur.

  37. Code Window Page tab Class Name box Method Name box

  38. Structure of an Event Procedure Private Sub objectName_event(...) Handles objectName.event statements End Sub (...) is filled automatically with (ByVal sender As System.Object, ByVal e As System.EventArgs) Header

  39. Code Window Page tab Class Name box Method Name box

  40. Create an Outline for an Event Procedure; i.e. header and End Sub 1. Double-click on a control or 2. Use the Class Name and Method Name boxes. (We primarily use the first method.)

  41. Sample Form txtFirst txtSecond btnRed Double Click on txtFirst

  42. Code for Walkthrough Public ClassfrmDemo Private SubtxtFirst_TextChanged(...) Handles txtFirst.TextChanged txtFirst.ForeColor = Color.Blue End Sub End Class

  43. IntelliSense Automatically pops up to give the programmer help.

  44. Code Window Click tab to return to Form Designer

  45. Sample Form txtFirst txtSecond btnRed Double-click on btnRed

  46. Code for Walkthrough Public ClassfrmDemo Private SubtxtFirst_TextChanged(...) Handles txtFirst.TextChanged txtFirst.ForeColor = Color.Blue End Sub Private Sub btnRed_Click(...) Handles btnRed.Click End Sub End Class

  47. Code for Walkthrough Public ClassfrmDemo Private SubtxtFirst_TextChanged(...) Handles txtFirst.TextChanged txtFirst.ForeColor = Color.Blue End Sub Private Sub btnRed_Click(...) Handles btnRed.Click txtFirst.ForeColor = Color.Red End Sub End Class

  48. Event Procedure txtFirst.Leave • Select txtFirst from Class Name box drop-down list. • Select Leave from Method Name box drop-down list.

  49. Code for Walkthrough Private SubtxtFirst_Leave(...)Handles txtFirst.Leave End Sub Private SubtxtFirst_TextChanged(...) Handles txtFirst.TextChanged txtFirst.ForeColor = Color.Blue End Sub Private Sub btnRed_Click(...) Handles btnRed.Click txtFirst.ForeColor = Color.Red End Sub

  50. Code for Walkthrough Private SubtxtFirst_Leave(...)Handles txtFirst.Leave txtFirst.ForeColor = Color.Black End Sub Private SubtxtFirst_TextChanged(...) Handles txtFirst.TextChanged txtFirst.ForeColor = Color.Blue End Sub Private Sub btnRed_Click(...) Handles btnRed.Click txtFirst.ForeColor = Color.Red End Sub

More Related