For A Good Program Design?: Visual Basic Basic of of
For A Good Program Design?: Visual Basic Basic of of
Obviously, you have to know what problem the application is supposed to solve before you can
write the application. As the designer of an application, it is your responsibility to expand the
level of detail in the end user requirements enough so that an appropriate software system can be
developed. Bridging the gap between the end user's business knowledge and your technical
knowledge is very important. One design tool that may help everyone understand the application
is the use of a prototype. A prototype is a partially functional or nonfunctional version of the
application to give the user an idea of what the final product will look like. This is easy to do in
Visual Basic and can be useful in extracting more requirements from the user.
requirements and avoid hard-coding values where appropriate, you can create code that you can
use again and again
TIP 5: Test Your Design
After you write a program but before it is technically finished, there is usually a testing process
to find bugs in the program. The primary goal of testing is, of course, to validate that the program
successfully performs the desired tasks. Another goal of an application tester is to attempt to
break your application through the use of borderline and invalid input.
Q2. Explain use of date and time in VB. With the help of Example.
Ans. Using Date and Time Values
Visual Basic .NET provides the versatile Date data type to store date and time values. The
following lines of code are examples of using date variables:
Dim
Dim
Dim
Dim
The first line of the previous code uses the pound (#) symbol to assign a date value directly to a
variable of type date. The next two examples use methods to convert strings containing date and
time expressions to the Date type. The final line of code shows how the constructor method can
be used to initialize a date by supplying parameters for the year, month, and day.
Depending on how you need to use dates in your program, you may use any of the preceding
methods. Listed next are a couple of points to keep in mind when working with the Date data
type:
1. The Date data type always stores a date and a time value to the millisecond, whether or
not you need both parts. As we will see, there are methods and properties to extract only
the parts you need for display or calculation purposes.
2. Visual Basic's Date data type is based on the .NET DateTime data type, so some of the
functions (such as Convert.ToDateTime) will reference the .NET spelling. When
declaring variables you may use either.
Q3. Explain nested If With the help of Example.
Ans. Nested If statements are probably the most correct in an academic sense, because there is
essentially only one path through the function. One If statement is executed, which happens to
contain another If, and so on. The drawback is that if you have a lot of nested Ifs and many lines
of code, it may be difficult to keep all the nested conditions straight when reading the code.
Suppose we write a program to show the average grades in a numeric value If any of the grades
entered is not a valid numeric grade, you should not average them but instead report this to the
user.
If ValidGrade(txtTest1.Text) Then
If ValidGrade(txtTest2.Text) Then
If ValidGrade(txtTest3.Text) Then
' INSERT LISTING 7.1 HERE
End If
Else
End If
MessageBox.Show ("Invalid grade entered!")
End If
Q5. What are the properties that are common to all controles?
Explain with examples.
Text: the text contained in the control. You can set the Text property either at design time using
the Properties window or at runtime using program code such as txtManager.Text = "Chris
Cawein". Your user can change the Text property at runtime by simply typing in the text box.
Determines how text is aligned on the button. Like the ImageAlign property, there
are nine possible values; MiddleCenter is the default.
Text Align: Specifies where the text specified by the Text property is aligned within the
boundaries of the control. There are nine possible locationsany of the four edges, any of the
four corners, and MiddleCenter. The default is MiddleLeft. Click the drop-down arrow to specify
the location
Appearance If set to Button, the control looks like a Button control whose appearance toggles
between pressed and unpressed. The default setting is Normal, which causes it to
look like a normal check box.
AutoCheck If for some reason you do not want your user to be able to check or uncheck the
box by clicking on it, set AutoCheck to False. The default value of True causes
the control's state to change whenever the user clicks it.
CheckAlign Specifies where the check box is located within the boundaries of the control.
There are nine possible locationsany of the four edges, any of the four corners,
and MiddleCenter. The default is MiddleLeft. Click the drop-down arrow to
specify the location.
Checked
CheckState Indicates the "check state" of the check box. Possible values are Unchecked,
Checked, and Indeterminate (grayed).
FlatStyle
Determines the appearance of the check box and how it changes when the user
moves the mouse over the button. The Standard setting (which is the default)
causes it to be displayed like a typical check box. Other possible settings are Flat
and Popup.
Q6. What are window Common dialog box controls? Explain any two
with examples
1-The FileDialog Controls
The CommonDialog class provides a FileDialog class that, in turn, provides two
dialog controlsthe OpenFileDialog control and the SaveFileDialog control
that allow your program's users to specify the name and location a file to be
opened or saved, respectively. The techniques involved in using these two
controls are quite similar.
2- The FontDialog Control
From time to time, you may want to give your users a way to specify a font to be
used for a report, label, and so on. The FontDialog control gives you an easy way
to present a Windows-standard font dialog box from which the users may make
their selection.To use a FontDialog control, you simply call its ShowDialog
method. After the user makes his selection and clicks OK, the font he chose is
stored in the control's Font property. If the user clicks Cancel, then the changes he
made to the font displayed in the FontDialog control are discarded.If you want the
FontDialog control to display a specific font on startup, you can set its Font
property before calling it. For example, you could use the following line of code
to set the FontDialog control's Font property to be the same as the Label control
lblTest:
FontDialog1.Font = lblTest.Font
As with the Save and Open dialog boxes, the Font dialog box doesn't actually
change the font. It simply reportsthrough its Font propertythe font that the
user has selected.
Let's enhance this chapter's test application to show how the FontDialog control
works. Follow these steps:
1. With the sample program's main form displayed, open up the Toolbox and
scroll down to the FontDialog control. Double-click its tool in the Toolbox
to add an instance to the tray area of frmDialogBoxes.
2. Add another Button control to frmDialogBoxes. Name this button
cmdFontDialog and set its Text property to Font Dialog.
3. Add a Label control near the cmdFontDialog button. Set its Name
property to lblTest and its Text property to This is sample text.
4. Add the following code to cmdFontDialog's Click event handler:
5. FontDialog1.Font = lblTest.Font
6. FontDialog1.ShowColor = True
7. FontDialog1.ShowDialog()
lblTest.Font = FontDialog1.Font
This code causes the dialog box's initial font selection to match lblTest's Font
property. The second line of code specifies that the dialog box should allow the
user to choose the font's color as well.
3.The ColorDialog Control
Much like the FontDialog control, the ColorDialog control presents a standard
Windows "color picker" dialog box to the user. After he selects a color and clicks
OK, the program has access to his chosen color through the control's Color
property. The user has the option of choosing one of the standard colors, or
creating and selecting a custom color.
Let's get right into an example. Follow these steps to add a ColorDialog control
test to our test program:
1. With the sample program's main form displayed, open up the Toolbox and
scroll down to the ColorDialog control. Double-click its tool in the
Toolbox to add an instance to the tray area of frmDialogBoxes.
2. Add yet another Button control to frmDialogBoxes. Name this button
cmdColorDialog and set its Text property to Color Dialog.
3. Add the following code to cmdColorDialog's Click event handler:
4. ColorDialog1.Color = lblTest.BackColor
5. ColorDialog1.ShowDialog()
lblTest.BackColor = ColorDialog1.Color
This code sets the initial value of the ColorDialog control to the
background color of lblTest and shows the dialog box. When the user
has made his selection and clicked OK, the background color of the
lblTest Label control is set to whatever colour is selected
Q7. Define MDI . how will you create a parent child form using MDI.
Many of the applications that you create in Visual Basic consist of a series of independent forms.
Each of these forms is displayed separately on the screen and is moved, maximized, or
minimized separately from any other form. With this type of interface, you cannot easily
organize the forms or deal with them as a group. Even with this limitation, this interface is a
good one to use for many programs and is probably the most prevalent interface design. An
alternative to this standard interface is the Multiple Document Interface. Forms in an MDI
application have a parent/child relationship, which is controlled through form properties. An
MDI application has one or more parent forms that contain most (or all) of the other forms in the
program. Other forms can be child forms, which are contained within the parent, or standard
forms, which are not. With an MDI application, you can easily organize all the child forms or
minimize the entire group of forms just by minimizing the parent form
MDI Parent Forms
An MDI parent form is a standard Windows form whose properties have been
modified slightly. Several characteristics define a typical MDI parent form:
An MDI parent form acts as a container for child forms; that is, the child forms are drawn
inside the confines of the parent forms.
The parent form automatically provides scrollbars if one or more child forms are
positioned outside the visible area of the parent.
The MDI parent window and all child windows are represented by a single icon on the
Windows taskbar. If the parent form is minimized and then restored, all the child forms
are returned to the same layout as they had before the application was minimized.
To create an MDI parent form, you simply set the IsMDIContainer property of a standard
Windows form to True. When you create a child form (at runtime), you set the MDIParent
property of the new form instance to the name of the MDI parent form, and the child form will
be displayed within the parent window
MDI Child Forms
Just as MDI parent forms have characteristics that define their behavior, MDI child forms also
behave in a certain way. The characteristics of an MDI child form are as follows:
Each child form is displayed within the confines of the parent form. A child form cannot
be moved outside the boundaries of the MDI parent form.
When a child window is minimized, its icon is displayed in the parent window, not on the
Windows taskbar.
When a child form is maximized, it fills the entire inner area of the parent form. Also, the
parent form's title bar contains the Text properties of both the parent and maximized child
forms, with the child form's Text property being enclosed in square brackets after the Text
property of the parent; for example, Text of Parent Form [Text of Child Form].
When one child form is maximized, all other child forms are maximized as well.
To create an MDI child form, you create an instance of a form at runtime and set that form's
MDIParent property to the Name property of an MDI parent form before showing it.
To see the Opacity property in action, using a scrollbar perform the following steps:
1. Add a Horizontal scrollbar control to your form. Set its Name property to scrOpacity.
2. The Min and Max properties of the scrollbar should already be set to 0 and 100,
respectively. Set the Value property of the scrollbar to 100.
3. Add a Label control to your form. Set the Name property to lblOpacity.
4. Add the following two lines of code to the scrollbar's Scroll event:
5. Me.Opacity = scrOpacity.Value / 100
lblOpacity.Text = Me.Opacity.ToString
The TransparencyKey property is similar to the Opacity property in that it allows you to see
through the form, but with two important differences:
The transparent parts of the form do not accept events such as the mouse click; in other
words you can still access the controls on an opaque form even if you can't see it, but a
transparent area of a form allows you to click through it.
Form transparency is either total or not based on a color; opacity is a percent value that
applies to all colors.
To demonstrate a transparent form, add a Label control to your form and set the
BackgroundColor property of the label to blue. Set the TransparencyKey property of the form to
the same color as the label background. When you run the program, your form will appear to
have a hole in itclicking in the transparent area will activate the window behind the form.
All objects with the same characteristics (data and functions) constitute one class.
A class and an object of that class has the same relationship as a data type and a
variable.
Declaring a class doesnt create any objects, just as mere existence of data type
int doesnt create any variables.
Obtain summary information about the data in tables such as total and average values.
Create, modify, or delete database objects such as tables, indexes, and queries.
A SQL statement contains one or more lines of text, much like a code statement in Visual Basic.
As you can see from the preceding list, SQL statements fall into two basic categories: The first
three items in the list are examples of how SQL is used to manipulate data in the database, the
last item demonstrates how SQL can be used to define the database itself. In this chapter we will
be mostly concerned with data manipulation statements
Optional Arguments.
Typically, you must include the same number of arguments in the calling statement as are present
in the definition of the procedure. Occasionally, however, you may need to create a procedure
that has one or more optional arguments. You can accomplish this by using the Optional keyword
before one or more of the arguments in the argument list portion of the Sub statement. Consider
an enhancement to the LogPrint procedure whereby under certain circumstances you want to
display information to the user via a message box in addition to writing a line to the log file. You
can modify the declaration of the LogPrint procedure to include an optional second argument
that represents text to be displayed in a message box as follows:
Public Sub LogPrint(ByVal strMessage As String, _
Optional ByVal strDisplayMsg As String = "")
ByVal, which stands for by value, means the value of the argument is passed to the
procedure from the calling program. This is the default argument-passing mechanism and
is generally used with input-only arguments.
ByRef, which stands for by reference, means that the calling program passes a memory
reference to the procedure. This method of argument declaration can not only be used to
send information into a procedure, but also to return information to the calling program.
For example, the following procedure gets the height and width of a rectangle from the
arguments list and then calculates the area and perimeter of the rectangle. These values are
returned through the arguments list:
Sub CalcRectangle(ByVal nWidth as Integer, ByVal nHeight as Integer, _
ByRef nArea as Integer, ByRef nPerimeter as Integer)
nArea = nWidth * nHeight
nPerimeter = 2 * (nWidth + nHeight)
End Sub