1 / 32

Working with Variables, Constants, Data Types, and Expressions in GroupBox Control

Learn how to work with variables, constants, data types, and expressions in the GroupBox control. Understand the properties and functionalities of the GroupBox control, Radio Button controls, and the Layout Toolbar. Discover how to lock controls on a form and create variables and constants with proper naming conventions.

pearlt
Télécharger la présentation

Working with Variables, Constants, Data Types, and Expressions in GroupBox Control

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 4 Working with Variables, Constants, Data Types, and Expressions

  2. GroupBox Control • The GroupBox Control is used as a container for other controls. • This is an example of a container control … a control that serves as a holding area for other controls, indicating that the controls are somehow related. • More often than not you’ll find radio buttons inside of the GroupBox Control.

  3. Radio Button Controls • The Radio Button Control presents a set of choices … such as the number of years in the car loan. • Radio Buttons are placed in groups so that only ONE may be selected at a time. • In order to do this it is important that you draw the radio buttons directly into the GroupBox Control. • If you do it right, you won’t have to write any additional code to enforce this rule – it will just work! • If you want more than one group of radio buttons just add more than one GroupBox control! • The prefix for naming radio button controls is “rad”. Example: radYears

  4. GroupBox Control Properties • A GroupBox Control can only have a rectangular shape. • A GroupBox Control can have a label, indicated by its Text Property Value. • When RadioButtons are added inside a GroupBox, only one can be selected at a time. • In general, you don’t have to rename your GroupBoxes since you don’t usually write code for them. FYI though: the naming prefix is “grp”

  5. Radio Button Properties • As you’ve learned, only one radio button within a group can be on at once. • The Checked Property keeps track of which one is selected. • The checked property is either True or False. • When one button in a group becomes true, all the others automatically become false. • FlatStyleProperty determines the 3D appearance of the control • Text Property – defines the visible text that displays on the control.

  6. Using the Layout Toolbar • The Layout Toolbar contains tools that allow you to adjust the alignment, spacing, and size of any group of controls on the form. • This really simplifies the task of changing the size and position of the controls on your form! • To select more than one control: Press and hold down the CTRL key and then click on the other controls! • Most popular features on the layout toolbar: • Make same size • Align Lefts, Rights, Tops and Bottoms • Center Horizontally and Center Vertically • Increase or Decrease Spacing Between Controls

  7. For Example: Selecting Multiple Controls and Using the Align Rights Button

  8. Cool Shortcut to put in for the End-User • It is helpful to the end user if they can just hit the enter key and have the program execute it’s intended purpose. That is done by setting the default button. • Just select the AcceptButton property in the Form’s Property List. Just choose the button that should be the default!

  9. Locking Controls on a Form • Locking Controls – This disallows the ability to move controls or modify control sizes on a form during design time – protects you from yourself! • Click on your form, then go up to Format on the menu bar, and select Lock Controls

  10. Constants and Variables • A value is a number or string that programmers use in the code. • For example 150, 0.83, 8.14, “yes” or “no” • A variable represents a location in computer memory that can change values as the code executes • A constant represents a location in computer memory that cannot be changed during execution

  11. VARIABLES • Variables are used in code statements to temporarily store values so that they may be used again in other calculations. • Variables are created by programmers. They must have a name and a type. • The name and type are decided on by the programmer. • To decide upon the type you must determine what kind of data the variable will store..

  12. Data Types • The data type of a variable or constant determines what kind of data it can store. • Here is a table with some basic .NET data types!

  13. VARIABLE NAMES • There are rules to naming your variables: • The name must begin with a letter, not a # ! • The name cannot be more than 16,383 characters.  • The name cannot contain punctuation or blank spaces. • The easiest way to create a variable is to assign a value to the variable’s name in a codestatement. For ex: dblrate=3.5 or strName=“John”.

  14. Naming Conventions for Variables • You don’t have to remember all of these at once … you’ll get the hang of them as you start creating and naming variables! • Just start the variable name off with the proper prefix. Then give it a name that describes what it is storing! • Short == shr • Integer == int • Long == lng • Byte == byt • Decimal == dec • Single == sng • Double == dbl • Char == chr • String == str • Boolean == bln • Date == dtm • Object == obj

  15. MORE ON VARIABLES • Variables created in this way may hold either numbers (numeric data) or characters (string data) • Character data is called a string and must be placed in quotation marks. Ex: name=“Christy” • Examples of code statements that assign variables: • Numeric Data --dblPrice = 5 • String Data --strShow = “Hamlet” • Value of another variable --dblCost = dblprice • value of an expression --dblCost = dblprice * dblsale • value of an expression --dblprice = dblprice * 1.05 • Note : the receiving variable -- must be on the left side.

  16. Understanding the Option Strict Statement • When you use assignment statements .NET does not require you to use the same data type on both sides of the equal sign. • .NET will attempt to make it work but this can lead to a bunch of errors at run time. • For example… lets say you declare a variable as a short and then try to store a value of 50,000 in it. Shorts only go up to about 32,000. This will cause the program to come to a screeching halt. • The Option Strict Statement can be used to instruct Visual Basic .NET to force you to ensure that all assignment statements use the same data type on both sides of the equal sign.

  17. Using Option Strict • This makes it a bit more tedious to code BUT the benefit is that .NET will let you know when you are doing it wrong and prompt you to fix it! • Being in Option Strict mode also means that you have to declare every variable! • That’s good for beginning programmers. It keeps you honest!  • In the code window on line 1, type Option Strict On

  18. DECLARING A VARIABLE • Generally speaking, variables need to be declared. The computer needs you to tell it the name of yourvariableand what type of variable you want it to be. • There are many different types of variables. • Some types are integer, string, single and double. • To declare a variable you use the keywordDIM. • Example: Dim intNumber as integer • Scope refers to a variables ability to be accessed throughout a program. It is defined by where the variable is declared. • The scope can be limited to one procedure … for example: the variable will work within a given calculate button procedure OR it can be available throughout an entire form. Let’s give some examples:

  19. Declaring Global Variables • Global variables are those that are available to all the procedures on a given form. • To declare a global variable type the Dim line in the Public Class Section of the Code Window. (Right below the REM Lines where you put your name!) • Also, put a “g” in front of the name to indicate that it’s a global variable! • For Example:

  20. Declaring Local Variables • Local variables are those that only work in a single procedure. • To declare a local variable go to the specific event procedure where you want the variable to work and write the DIM statements there! • For example: btnCalculate_Click Event

  21. Declaring ConstantsUsing the Keyword Const

  22. Converting Data Types • Because Option Strict is on in this chapter, you have to insure that all the data being used by the calculations has the same data type … if you don’t, you’ll get an error! • For example: In the main project the nudLoanAmount box has a Decimal Value Type. Your calculation needs to go into a Double Variable. Therefore you need to convert it. • Luckily, .NET has simple-to-use conversion statements. For example:

  23. Coding a Form Load Event Procedure • Every event has a default event … for command buttons it is the Click Event. • For Forms it is the Form Load Event. • This event executes when the form first loads into memory. • Therefore, any code you have in the Form_Load Event will trigger whenever the form loads!

  24. Numeric Expressions • An expression can perform a calculation, manipulate characters, call a function, or test data. • A numeric expression is any expression that can be evaluated as a number. • A numeric expression can include values, variables, constants, as well as certain control properties. • Expressions often contain math problems … therefore arithmetic operators and order of operations come into play!

  25. ARITHMETIC EXPRESSIONS • VB follows the normal order of operations – which is a predetermined order of performing calculations within an expression. (PEMDAS) • Arithmetic operators in Visual Basic • ^ -- used to raise the power of an exponent • * -- used to multiply two numbers • / -- used to divide 2 #’s and return a decimal result • \ -- used to divide 2 #’s and return an integer result • MOD -- Used to divide 2 #’s and return only the remainder • + -- used to sum 2 #’s • - -- Used to subtract 2 #’s or indicate a negative value

  26. Sometimes you DON’T have to reinvent the wheel! Let’s say you want to do a calculation that millions before you have also wanted to do…. Perhaps it’s already set up for you? VB programmers created a set of Intrinsic Functions – functions that are built into .NET. You used one of those Intrinsic Functions in your main project. It was the PMT Function. This is a financial function that will return the payment for a loan based on periodic, constant payments and a constant interest rate. Pmt(rate, number of periods, pv, fv, due) Intrinsic Functions

  27. The Pmt Function

  28. Using the Pmt Function • This is what it looked like in your code! • There is a list of additional intrinsic functions on page 4.59 in your book!

  29. Using the Format$ Function • The Format$ Function is an Intrinsic function that allows you to apply a “better look” to your output. • For instance, what if you want to round an answer to a certain number of decimal places OR you want the answer to display as a $$$ value. Use the format$ function! • Format$ -- function that allows the programmer to determine how the output will appear on the screen. • Ex: Format$( dblNum * dblprice, “currency”) • Some popular formats are general number, currency, fixed, standard and percent

  30. The Format$ Function

  31. Chapter 4 THE END!

More Related