Download
visual basic 1 n.
Skip this Video
Loading SlideShow in 5 Seconds..
Visual Basic 1 PowerPoint Presentation
Download Presentation
Visual Basic 1

Visual Basic 1

47 Views Download Presentation
Download Presentation

Visual Basic 1

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Visual Basic 1 Basics

  2. Index of project examples in this ppt My CSCI 110 powerpoints will have an index of the VB examples generally as the 2nd slide. For this ppt show, here is an index of project examples • The Two-textboxes example • A Messagebox example • The F to C converter • Gaddis/Irvine text comments and chapter 1 screenshots

  3. About the Schneider text examples • VB 2005 Express edition can be downloaded free from MS to install on your own machine. • You will need the 2005 edition VB to run the examples on the (other) text CD. • However, my examples and the text examples should run in the labs, which have this edition of VB. • As the semester progresses I will update our labs, projects and these powerpoints to reflect more examples from our text.

  4. Here’s a VB form example: hourly wage calculations. We will write this program next week.

  5. About VB • VB is a (sort of) object-oriented language with a large IDE providing much developer support. • Because of this, the environment is as hard to learn as the language, or harder! • But it won’t be hard to develop impressive VB projects right away.

  6. VB IDE • IDE means integrated development environment. • An IDE provides a means of developing and test-running software. • The VB IDE is “part of” MS’s large .NET software system. • There is quite a lot of mid-size enterprise sw development going on in VB!

  7. VB version etc • Labs should have the 2005 version of VB in the programming folder. • This large program will take a while to load and VB programs will generally run fairly slowly from within the IDE. • You can download free .NET software for your home computer from Microsoft.

  8. To run VB: Click on the MS studio.net icon (probably in the programming folder)

  9. Select windows application

  10. Select new project (windows application), then select VB project from other types and click ok.

  11. Note: You can give a project a special name by typing something else where it says name when you select New Project. This is a good idea, because it will get hard to remember what’s what.

  12. Creating a VB application in the express edition…view toolbox selected

  13. A button with the hot key (alt-P) defined

  14. Selecting new vb project (as per above) will open the form design interface

  15. Selecting “View” on menu bar opens various window “view” options. Here, “view toolbox” was selected.

  16. Pull down the View options on the menubar • Use the toolbox to select “tools” (called “controls” in VB) for your project • Use the properties window(s) to set properties for your form and its control components. • Use the solution explorer window to view the different elements of your solution.

  17. Plopping components on your form • Either double clicking a component in the toolbox menu, or clicking the component in the toolbox then clicking on your form, will put a control on your form. • Once there, you can “select” it, resize it, align it, or drag it to where you want it to go, or add other properties to it like tab order or a tooltip.

  18. Here’s a form with a couple of textboxes plopped on it

  19. Some popular components • Textboxes, buttons, and labels are the most popular components. Textboxes hold text - often user input. • Labels are for labeling other components, usually. • Buttons can be “pressed” to fire an event. • Picture boxes can “hold” images • comboboxes allow multiple choices. • Listboxes are like multi-line textboxes, (Textboxes can also be set to be multiline). • Radiobuttons and checkboxes display available choices: the user can select/deselect them

  20. More on controls • Controls can be grouped into groupboxes to help rationalize a complicated display. • There are other types of controls as well – we won’t learn how to use them all this semester. • You are already familiar with many controls as a user of window applications.

  21. Running your VB application • At any time during development, as long as you have no errors, you can “run” your application. • To check for errors: Select build from the menubar and then select build solution (or rebuild) • To run or check for errors: Select Debug on the menu bar, and then pick start or press F5.

  22. Running an application with two textboxes (there’s no functionality)

  23. Note • You need to close your running application (window) before continuing development on it. Just click the X in the upper right corner of the running form’s window or, in the debug menu, select “stop debugging”. • It is useful to “build” or “debug” periodically to make sure you have what you want and what you have works.

  24. Selecting the form and editing the text property in the properties window allows you to change the text displayed on the form, its “name”, when the form comes up

  25. More “basic” development: Let’s add functionality to a form • Clicking on the blank form in the development window will cause a “code window” to pop up. You can provide code specifying the action to be taken when the form is clicked.

  26. Events • VB, VC++ and Java are event-driven languages. • This means mouse-clicks or letters typed at the keyboard may “fire” (start, initiate, cause) events. • When events are fired, the programmer can specify what is supposed to happen.

  27. Subroutines • Subroutines are the Basic program language name for programmer-specified functionality. • They are referred to as “sub” in the VB code. • VB helps you to write subs by providing stubs for any event-fired subroutine. • This saves memorizing some things. It also saves typing and time. • BUT: You must be careful: make sure the event sub which is stubbed in is the one you want. • Cutting and pasting stubbed subs can be dangerous since some stubbed values may still need editing.

  28. Our first vb sub • Let’s open a little message window when the user clicks anywhere on the form. • In VB, messagebox is the name of the little message window component. • Double-clicking on the form in development will switch us to a code window where a sub for this event-handler has been stubbed for us.

  29. Form Click sub stub • Below is the stub for form click. • Be careful, as VB may stub in a sub for form load. • In any case, you can edit the stub to look like this: Private Sub Form1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Click End Sub

  30. Our sub Private Sub Form1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Click MessageBox.Show("A message!", "first VB example", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) ‘comment…bold text is what you type End Sub

  31. Remarks about this sub • Fit code on one line or use the space-then-underscore to continue a VB statement onto the next line. • Important note: Most of these slides show code spilling onto multiple lines, which won’t work. • What you should type into the stubbed sub on one line is: MessageBox.Show("A message!", "first VB example", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

  32. More remarks on this subroutine Private Sub Form1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Click • Notice the name: Form1_Click. This is generated automatically, and would have specified a different name for the method if we had given our form a different name. • In general, ComponentName_Click is the name of the subroutine handling mouseclicks on a control component named ComponentName. • MyBase.Click is the event for clicking on the form. • We’ll learn more about the parenthetical arguments and the “Handles…” another time.

  33. Now, run the example (remember: select debug, then click start)An empty form appears, but…click on it

  34. A message box pops up

  35. An exercise to test your understanding: Fix your message box to look like this

  36. Setting properties for components • Clicking a component on your form will open its properties window (probably on the right.) • You can also open properties window by selecting view>properties • You can specify names and initial (text) values of control components. • You can resize labels (or textboxes) or change the text font, for example, if the text doesn’t fit. • You can align text in a component. • You can set colors.

  37. Now let’s change the form • Add a label: Set its text property to Enter fahrenheit. Give it a name like lblInput as its name property. • Add a textbox: set text property to blank contents, name property to something like txtInput • Add another textbox: set read-only property to true (see below) and name it, for example, txtOutput

  38. VB Naming conventions • Although you can name components almost anything you like, VB conventions recommend standard prefixes frm, lbl, btn, txt (and so on) for form, label, button, textbox (and so on). • I gave my label and textboxes the names: lblPrompt, txtInput, txtOutput. • Using standardized conventional names will help you remember what things are and what they are used for as your applications become more complicated.

  39. VB component properties • I put text in my label instructing the user what to do. • I set input’s text to blank. • I set output to be read-only (not editable). • ‘&’ in the text property of a button defines a hotkey for keyboard input. So, if the text on a button is “X&YZ” then typing the letter ‘Y’ on the keyboard is the same as clicking that button with the mouse. • See the next slides for setting properties.

  40. Setting properties • As you add components, clicking them will open the properties window. • In the properties window, you can give the components names, and set other property values.

  41. Setting properties • Clicking “elsewhere” on your form or in another window confirms property settings. • Of course, you can change properties anytime. • Remember to save your application each time you make changes.

  42. Let’s look at the form (start debugger). Except for the message box there’s still no real functionality

  43. What else do we want? • Get rid of the pop-up message box? Your choice. • Make the form “go away” when we are done. (This is already provided by Microsoft windows application code when the X is clicked in the upper right of the running application window.) • Add functionality: the famous F to C conversion from ninth grade. Recall the formula C=5.0/9.0*(F-32) . The parentheses and decimal points are needed. • Add a label for the answer • Add two buttons. Give them names. (VB convention would be to name them btnCompute and btnQuit)

  44. Exercise to test your understanding: add some components & set properties • Complete Lab 1 and Lab 2 for this week

  45. My new form: still no functionality

  46. event-driven programming, continued • VB makes handling events fairly easy, although it is still pretty technical. • As previously mentioned, double-clicking a control component in the form-development window brings up a code window with an empty subroutine already stubbed in. • You provide the specific code you want for your application. • It is still up to you to make sure this is what you really want!

  47. More on the Visual environment • VB and VC++ provide a lot of programmer support, prompting you with components, the proper code to provide, and the place your code should go. • When prompted (with a pop-up window) you may ignore the suggestions and keep typing, or make a selection and hit the enter key to save some typing.

  48. Back to the form • How do we add functionality? • In design-mode, double-clicking a component will open a code window where you can add the code you want for this “event”. • You can also open the code window from the VIEW menu. • Let’s add functionality to the compute button.

  49. What’s involved? • We need to create appropriate variables to hold necessary values. • We need to compute whatever information we need. • We need to display the result so the user can see it.

  50. The IPO model • I-P-O = input-process-output • Many programs in real life and in this class will follow the IPO model. • The user provides input, the program processes it somehow, and then generates output. • IPO is NOT the only model, transactions (at an ATM for example), are not simple IPO.