1 / 10

Visual Basic Controls and Variables: Basics and Best Practices

Explore basic controls like Label, Command Button, and Text Box in Visual Basic, along with variable declaration and naming conventions. Learn how to interact with users and display information effectively.

klees
Download Presentation

Visual Basic Controls and Variables: Basics and Best Practices

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. Controls • Control naming convention • Label: lblName • Command Button: cmdName • Text Box: txtName

  2. Variables • Variable: Temporary storage location for information • Variable declaration: each variable can be declared with the dim statement • Dim x as integer • Single declaration statement must not contain more than one variable without specifying its type. • Set VB to Require variable declarations in order to avoid misspelling a variable • (Tools, Options, Editor – Require Variable Declaration) • This way VB gives an error if a variable is not declared

  3. Variables • Naming convention (using prefix) • s : String • n : Integer • l : Long Integer • f : Single • d : Double • c : Currency • b : Boolean • v : Variant

  4. Small Application • To get data from a user you should use a Text Box • To display information for a user you should use a Label • The processing of the data should take place when the user clicks on a Command Button Label Text Box Command Button

  5. Changing Properties • To change a control’s property you assign the new property to it:label4.caption = fNumberOfKilos • To access a control on another form you should use the form name to prefix the property name:Form2.Label5.Caption =fCostPerKilo Control Name Property Form Name Control Name Property

  6. Controls: Text Box • The Text / Edit box is a control that handles user input / editing of text. • The characters that are written in the text box are held in the Text property.

  7. Controls: Text Box • We can assign the contents of the text box to a variable quite simply - for instance Sub Command1_click() Dim sMyName As String sMyName = Text1.Text End Will cause whatever to written by the user in the text box to be stored in the variable sMyName when the user clicks on Command button 1

  8. Val function • The contents of a text box is always character based - even if we type in numbers they will always be seen as simply a collection of characters and so cannot be used in any form of maths - that is we can’t add them, multiply or so on. • To turn them into numbers we must use the Val function. iMyNumber = Val(Text1.text) Finally the value is stored in the variable The Val function converts the symbols into numbers Here the text is stored in the text property

  9. Controls: Label • A label is intended for displaying information only • We can display information in it by assigning a value to the Caption property Command1_Click() Label1.Caption =“Intro to VB” End

  10. Running a Program • Click the Start button on the VB toolbar OR • Choose Run – Start OR • Press F5

More Related