Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming with Microsoft Visual Basic 2012 Chapter 1: An Introduction to Visual Basic 2012.

Similar presentations


Presentation on theme: "Programming with Microsoft Visual Basic 2012 Chapter 1: An Introduction to Visual Basic 2012."— Presentation transcript:

1 Programming with Microsoft Visual Basic 2012 Chapter 1: An Introduction to Visual Basic 2012

2 Lesson A Objectives After studying Lesson A, you should be able to: Start and customize Visual Studio 2012 Create a Visual Basic 2012 Windows application Manage the windows in the IDE Set the properties of an object Restore a property to its default setting Save a solution Close and open an existing solution Programming with Microsoft Visual Basic 20122

3 The Splash Screen Application Programming with Microsoft Visual Basic 20123 Figure 1-1 Splash screen for the Red Tree Inn

4 To start Visual Studio Professional 2012: –Click Start and point to All Programs –Select Microsoft Visual Studio 2012 –Click Start Visual Studio –Click WINDOW ( 視窗 ) on the menu bar –Click Reset Window Layout ( 重設視窗配置 ) –Click Yes at the prompt to restore the default layout –You can also select a different window layout using TOOLS→ Options on the menu bar Programming with Microsoft Visual Basic 20124 The Splash Screen Application (cont.) Figure 1-2 Microsoft Visual Studio Professional 2012 startup screen

5 Programming with Microsoft Visual Basic 20125 Setting Window Layout Figure 1-3 Options dialog box

6 To create a Visual Basic 2012 Windows application: –Click FILE on the menu bar, and then click New Project –Enter Splash Project in the Name Box –Locate a folder to store the project –Select Create Directory for Solution Programming with Microsoft Visual Basic 20126 The Splash Screen Application (cont.)

7 Programming with Microsoft Visual Basic 20127 Figure 1-4 Completed New Project dialog box in Visual Studio Professional 2012

8 The Splash Screen Application (cont.) Programming with Microsoft Visual Basic 20128 Figure 1-5 Solution and Visual Basic project

9 The Splash Screen Application (cont.) Managing the Windows in the IDE To open the Properties Window: –Click VIEW on the menu bar, and then click Properties Window –Or mouseover the Solution Explorer tab if the window is auto-hidden To close a window: –Use the Close button on the window’s title bar To minimize a window to a tab: –Click the Auto Hide (push-pin) button on the Solution Explorer window Programming with Microsoft Visual Basic 20129

10 10 The Splash Screen Application (cont.) Figure 1-6 Current status of the windows in the IDE

11 Windows Form Designer Window Programming with Microsoft Visual Basic 201211 Used to create a graphical user interface (GUI) Windows Form object –Create a UI by adding objects Buttons Text boxes –The title bar contains a default caption minimize, maximize, and close buttons Figure 1-7 Windows Form Designer window

12 Solution Explorer Window Programming with Microsoft Visual Basic 201212 Displays a list of projects contained in the current solution and items contained in each project Source file ( 原始檔 ) –A file containing program instructions (code) Form file ( 表單檔 ) –Contains code associated with a form –The first form’s code is stored in Form1.vb –The second form’s code is stored in Form2.vb Figure 1-8 Solution Explorer window 列出專 案內各 檔案

13 Properties Window Programming with Microsoft Visual Basic 201213 Properties –Attributes of objects that determine appearance and behavior Object box –Contains the name of the selected object Properties list –Helps identify and access properties Settings box –Shows the current values of properties Figure 1-9 Properties window

14 Properties of a Form File Programming with Microsoft Visual Basic 201214 Using the Properties window to change a form file’s name –Click Form1.vb Source file in Solution Explorer window –Click File Name in Properties list –Type Splash Form.vb.vb extension identifies file as source file –Press Enter

15 Properties of a Form File Programming with Microsoft Visual Basic 201215

16 Properties of a Windows Form Programming with Microsoft Visual Basic 201216 System.Windows.Forms is the namespace that contains the Form class A class is a block of code that specifies an object’s appearance and behavior The period that separates each word in System.Windows.Forms.Form is called the dot member access operator Figure 1-11 Properties window showing a partial listing of the form’s properties

17 Important Properties of a Windows Form Programming with Microsoft Visual Basic 201217 The Name Property Windows forms have a Name property –Use it to refer to the form in the program code Naming conventions are used for clarity –Hungarian notation for a variable frmSplash : “frm” indicates a form (data type of the variable), and “Splash” is the purpose of the variable –Camel case Enter the name of a VB object in lowercase and then capitalize the first letter of each subsequent word in the name

18 Important Properties of a Windows Form Programming with Microsoft Visual Basic 201218 The Text Property The Text property controls the text displayed in the form’s title bar –The default text for the Text property of Form 1 is Form1 Change the default name to something meaningful –The Name property is used by the programmer –The Text property is read by the user when the application is running

19 Important Properties of a Windows Form Programming with Microsoft Visual Basic 201219 The StartPosition Property The computer uses the form’s StartPosition property to determine the form’s initial position on the screen –Splash screens usually appear in the middle of the screen Choose CenterScreen for the StartPosition CenterScreen WindowsDefaultLocation

20 Important Properties of a Windows Form Programming with Microsoft Visual Basic 201220 The Font Property A font is the general shape of a character Font styles include regular, bold, and italic Font sizes (8, 10, 12, etc.) are in points, with 1 point equaling 1/72 of an inch The recommended font for applications is Segoe (pronounced SEE-go) –This is Segoe UI, Size 18

21 Important Properties of a Windows Form Programming with Microsoft Visual Basic 201221 The Size Property A form can be sized by dragging the sizing handles that appear around it You also can size an object by selecting it and then pressing and holding down the Shift key as you press the up, down, right, or left arrow key on the keyboard You also can set the object’s Size property, which contains two numbers, measured in pixels: –The first number represents the width of the form –The second number represents the height of the form

22 Programming with Microsoft Visual Basic 201222 Setting and Restoring a Property’s Value Figure 1-12 Status of the form in the IDE Click BackColor in the Properties list and then click the list arrow in the Settings box Click the Custom tab and then click a red square to change the background color of the form to red Now, right-click BackColor in the Properties list and then click Reset on the context menu The background color of the form returns to its default setting

23 Programming with Microsoft Visual Basic 201223 Saving, Closing, Opening, and Exiting a Solution Saving a Solution An asterisk (*) appears on the designer tab whenever you make a change to the form Save the solution by clicking FILE on the menu bar and then clicking Save All Closing the Current Solution Click FILE on the menu bar and then click Close Solution To exit Visual Studio 2012, click FILE on the menu bar and then click Exit Opening an Existing Solution Click FILE on the menu bar, and then locate and open the solution from the list of filenames available

24 Programming with Microsoft Visual Basic 201224 Lesson A Summary Starting Visual Studio Configuring Visual Studio Creating a Visual Basic 2012 Windows application Resetting a window layout Closing and opening windows Auto-hiding a window Setting property values (name, text, location, font, size, background color) Saving, closing, and opening solutions Exiting Visual Studio 2012 練習 p. 21~24

25 Lesson B Objectives After studying Lesson B, you should be able to: Add a VB control to a form Set the properties of a label, picture box, and button control Select multiple controls Center controls on the form Open the Project Designer window Start and end an application Enter code in the Code Editor window Terminate an application using the Me.Close() instruction Run the project’s executable file Programming with Microsoft Visual Basic 201225

26 Programming with Microsoft Visual Basic 201226 The Toolbox Window Class –A pattern from which an object is created Object –An instance of a class with particular settings for behaviors and attributes Toolbox window (or toolbox) –Contains tools for designing a user interface –Each tool represents a class –Controls are instantiated using tools from the toolbox VB Controls –VB GUI objects that appear on a form

27 The Toolbox Window (cont.) Programming with Microsoft Visual Basic 201227 Figure 1-13 Toolbox window showing the purpose of the Label tool

28 Programming with Microsoft Visual Basic 201228 The Label Tool Used to create a label control –Displays text that cannot be edited at run time Important properties –Name property A unique name for a control –Text property Specifies the value that appears in a label control –Location property Specifies the location of a control on a form Includes an X value and a Y value

29 The Label Tool (cont.) Programming with Microsoft Visual Basic 201229 Figure 1-15 Label control added to the form

30 Programming with Microsoft Visual Basic 201230 The Label Tool (cont.) Setting the Text Property Select the lblMsg control –Click text in the Properties list –Type A relaxing place to stay! –Press Enter –Verify that the AutoSize property is set to True Select the lblName control –Click text in the Properties list –Type Red Tree Inn –Press Enter

31 Programming with Microsoft Visual Basic 201231 The Label Tool (cont.) Setting the Location Property Select the lblMsg control –Click Location in the Properties list –Expand the Location property by clicking the plus box –Type 315, 175 –Press Enter Select the lblName control –Click Location in the Properties list –Expand the Location property by clicking the plus box –Type 315, 130 –Press Enter

32 Programming with Microsoft Visual Basic 201232 Changing a Property For Multiple Controls A control inherits the font attribute of the form –You may want to change the default values To simultaneously modify similar controls: –Click one control and then press and hold the Ctrl key –Click other similar control(s) in the form, or click and drag to group the controls –Set the property value in the Properties list

33 Programming with Microsoft Visual Basic 201233 Using the Format Menu The Format menu provides options to manipulate form controls –Align: Aligns selected controls by left, right, top, or bottom borders –Make Same Size: Makes the width and/or height of selected controls uniform –Center in Form: Centers one or more controls Select as the first control the one whose size or location you want to match –Called the reference control 練習 p. 30~34

34 Programming with Microsoft Visual Basic 201234 The PictureBox Tool Creates a picture box control –Displays an image on a form –Task box: Lists tasks associated with a picture box Use the task box to import an image into a project –Size Mode : StretchImage A project’s resource file contains imported images Figure 1-17 Open task list for a picture box Figure 1-18 Completed Select Resource dialog box

35 Programming with Microsoft Visual Basic 201235 The PictureBox Tool (cont.) Figure 1-19 Picture boxes added to the form 練習 p. 34~36

36 Programming with Microsoft Visual Basic 201236 The Button Tool Used to create a button control –Used to perform an action when clicked –Examples: OK and Cancel buttons Common means of exiting a Windows application: –Exit option on the FILE menu –Exit button

37 Programming with Microsoft Visual Basic 201237 Starting and Ending an Application Startup form –Displays when an application is first started The Project Designer window specifies the startup form or the executable code to be run –Right-click My Project in the Solution Explorer window and then click Open –Or, click PROJECT on the menu bar and then click Properties Application tab –Specify startup form

38 Starting and Ending an Application (cont.) Programming with Microsoft Visual Basic 201238 Figure 1-20 Application pane in the Project Designer window

39 Starting and Ending an Application (cont.) Programming with Microsoft Visual Basic 201239 Executable file A file that can be run outside of the IDE Its filename ends with.exe It is automatically created when a Visual Basic application is started Two ways to start an application in the IDE: Click DEBUG on the menu bar and then click Start Debugging Press F5 key 練習 p. 36~38

40 Programming with Microsoft Visual Basic 201240 The Code Editor Window Events –User actions such as click, right-click, mouse-over, … Event procedure –A set of program instructions that are processed when an event occurs Code Editor window –Provides templates for creating event procedures –To open it, right-click the form and then click View Code

41 Programming with Microsoft Visual Basic 201241 The Code Editor Window (cont.) Figure 1-22 Code Editor window opened in the IDE

42 Programming with Microsoft Visual Basic 201242 The Code Editor Window (cont.) Class Name list box –Lists names of objects included in the UI Method Name list box –Lists appropriate events for the selected object To select a control’s event in the Code Editor window: –Select the class name first –Then select the method name Syntax –The rules of a programming language

43 Programming with Microsoft Visual Basic 201243 Class Name and Method Name List Boxes Class Name list box Method Name list box

44 Programming with Microsoft Visual Basic 201244 Writing A Procedure Procedure header –The first line in the code template for an event procedure Procedure footer –The last line in the code template for an event procedure Keyword –A word that has a special meaning in a programming language –Color coded in the IDE procedure header procedure footer

45 Programming with Microsoft Visual Basic 201245 Event Sub Procedure Sub procedure –A block of code that performs a specific task Keywords in a sub procedure block: –Sub : Starts a sub procedure –End Sub : Indicates the end of a sub procedure –Private : Restricts the use of the procedure to the form where it appears The names of event sub procedure include the object name and event name –Example: btnExit.Click

46 Programming with Microsoft Visual Basic 201246 Me.Close() Instruction and IntelliSense Method –A predefined procedure –Can be called (or invoked) when needed Me.Close() method –Causes the current form to close –If it is the only form in an application, the application terminates IntelliSense feature Provides appropriate selections as pop-up lists when coding Type the first few letters of your choice Then use the arrow keys to select an item in the list 練習 p. 38~42

47 Programming with Microsoft Visual Basic 201247 The IntelliSense feature Figure 1-25 List displayed by the IntelliSense feature

48 Programming with Microsoft Visual Basic 201248 Lesson B Summary To add a control to a form, use a tool from the toolbox Use the label control for text that users cannot edit You can set the properties of multiple controls simultaneously To start and stop an application in the IDE, use DEBUG on the menu bar Use the Code Editor window to display an object’s event procedures The Me.Close() method closes the current form at run time

49 Lesson C Objectives After studying Lesson C, you should be able to: Set the properties of a timer control Delete a control from the form Delete code from the Code Editor window Code a timer control’s Tick event procedure Prevent the user from sizing a form Remove and/or disable a form’s Minimize, Maximize, and Close buttons Print an application’s code and interface Programming with Microsoft Visual Basic 201249

50 Using the Timer Tool Splash screens typically close automatically –They do not usually contain an Exit button –Use a timer control to close the splash screen Timer tool –Used to create a timer control Timer control –Processes code at regular time intervals –Does not appear on the user interface at run time –Important properties Interval property –Specified in milliseconds Enabled property –Allows the timer to run if set to True Programming with Microsoft Visual Basic 201250

51 Using the Timer Tool (cont.) Tick event –Occurs each time an interval has elapsed –Condition: The timer must enabled Add code to the Tick event procedure –Code will be run when the Tick event occurs Component tray –A special area below the form design area in the IDE –Stores controls not visible in the user interface at run time Programming with Microsoft Visual Basic 201251

52 Using the Timer Tool (cont.) Programming with Microsoft Visual Basic 201252 Figure 1-29 Timer control placed in the component tray 練習 p. 48~50

53 FormBorderStyle Property of a Form FormBorderStyle property –Determines the border style of a form Example settings: –Sizable: Allows the user to change a form’s size by dragging its borders at run time –FixedSingle: A fixed, thin border line –None: No border Programming with Microsoft Visual Basic 201253

54 The MinimizeBox, MaximizeBox, and ControlBox Properties MinimizeBox property –Determines whether the Minimize button appears on the form’s title bar MaximizeBox property –Determines whether the Maximize button appears on the form’s title bar ControlBox property –Determines whether the title bar appears on the form Splash screens typically do not contain a title bar –To remove a title bar, delete the contents of the form’s Text property Programming with Microsoft Visual Basic 201254

55 Programming with Microsoft Visual Basic 201255 Figure 1-31 Completed splash screen The MinimizeBox, MaximizeBox, and ControlBox Properties (cont.) 練習 p. 50~51

56 Printing the Application’s Code and Interface Printing the application’s code and user interface helps you understand and maintain the application To print the interface : –Run the application –Press Prnt Scrn on the keyboard –Paste into Word or Paint and then print To print the code : –Click the Splash Form.vb tab to make the Code Editor window the active window –Click FILE on the menu bar, and then click Print to open the Print dialog box Programming with Microsoft Visual Basic 201256

57 Programming with Microsoft Visual Basic 201257 Figure 1-32 Print dialog box Printing the Application’s Code and Interface (cont.)

58 Programming with Microsoft Visual Basic 201258 Lesson C Summary The timer control processes code at specified intervals A form’s MinimizeBox and MaximizeBox properties enable or disable the Minimize and Maximize buttons on the form A form’s ControlBox property determines whether the icon and control buttons appear in the title bar A form’s FormBorderStyle property controls the appearance of the form’s border Print Visual Basic code from the Code Editor window Print the user interface from the designer window


Download ppt "Programming with Microsoft Visual Basic 2012 Chapter 1: An Introduction to Visual Basic 2012."

Similar presentations


Ads by Google