Thursday, December 11, 2008

C Programmer’s Guide to Getting a little work done in Visual Basic 6.0

Introduction

The goal of this document is to allow someone with a little knowledge of C write very simple

programs in Visual Basic. By no means does it tell you all you need to know to be a good Visual

Basic programmer, but I hope it gives you a feel for the language. After reading this, if you want

to write programs with lots of bells and whistles, you can buy a Visual Basic book and teach

yourself the fancy stuff.

Making a new Project

When you start up Visual Basic it should bring up the new project window (see below). (If it

doesn’t display the window when you start it up, select “New Project” from the file menu.)

• Click on Standard EXE and say OPEN

Creating a window

In Visual Basic, before you write any code, you have to design your form. Unlike in C, you can’t

do anything in Visual Basic without a form. You can design fancy forms, but we’re just going to

do a very simple one that will allow you to get input from the user and display output.

When your new project opens, you get a blank form labeled Form 1. This form is actually the

window that your program will run in. You can see what it looks like running by pressing the

“play” button (looks like a triangle) at the top of the page. Pressing the “stop” button (the square)

stops the program.

Most people like their programs to have a special name at the top, instead of just form 1.

• Go to the properties menu on the right side of the Visual basic window. It looks like the picture

below.

• Highlight the words “Form1” under caption and change it to “My Way Cool Program”

• Hit play to look at your cool form, and then hit stop.

Adding a Button

Let’s give your user something to click

• On the toolbox (picture at right) double click on the button (right

below the box labelled “ab|”

• Drag the butoon down a bit on your form so it’s on the lower third of

the form.

• Go to the properties box. Change the Caption of your button to

Push Me

• Also in the properties box, change the (name) of your button from

Command1 to

my_button

• Your form should now look like the picture below.

• Run your program. You will now have a button you can click (but

that doesn’t do anything!)

Writing “Hello, World” (or, Adding a picture box)

Now let’s start doing a little programming. We’re going to write a program that prints “Hello,

World” on the screen when you press the button.

• On the toolbox (picture at right) double click on the desert picture to get a picture box.

• Slide it down and stretch it out a bit so that your picture looks like the one below

• Go to the properties box. Change the name of your picture box (at the top, under (Name) )

from Picture1 to

my_output

Here’s how we want your program to work: When the user pushes the button, we want to print

something on the picture window. Do this as follows:

• Double click on your button (that says “push me”) to tell visual basic you want to program

what happens when the user clicks the button.

• Write the following program:

Private Sub my_button_Click()

my_output.Print "Hello, World"

End Sub

This program basically says “when my button gets clicked, print “Hello, World” on the picture

box called my_output. Close the program window, and run your program.

If you want, add another line right after the first print line that says

my_output.Print "How are you?"

and run your program again.

Adding a text box

It’s convenient to give your user a place to put information.

• On the toolbox (picture at right), double click on the “text box” button (labelled “ab|”)

• Go to the “Text” property, and erase the value on the right.

• Go to the (Name) of your textbox, and change it to

my_input

• Move the objects around so that your form looks like the picture below.

• Run your program. Now you can type whatever you want in the text box. It doesn’t do anything,

but it’s fun to type.

Adding A label

Labels help your user know that they are expected to enter data.

• Click once on the capital letter A and drag it on your form to add a label. (You could also double

click like before)

• Change the caption (on properties) of your label to:

Feet:

• Click on the font property and then click on the 3 dots and change it to Times New Roman 12

point bold

• Use the drop down menu (triangle) on the properties to switch to the my_input textbox (or

double click on the text box), and change the text to

0

(i.e. the number zero)

• Modify your form so it looks like the picture below

Some Programming Information

At this point you’re almost ready to write some more complex visual Basic Programs. Here’s

some basic information you need to know. Here are some common variable declarations in C and

the corresponding declarations in VB. Notice that VB does not use semicolons in the declarations.

Assigning values to variables in VB is similar to C. Note, there are no semicolons in VB

assignment statements.

Getting input from the user is a little complicated in VB, because you have to know where they

are putting it. On our form, the user will be typing in the my_input window. Here’s how you do it.

NOTE: If you want to input more than one thing from the user, you need to create more input

boxes.

Declaring Variables

Declaration in C Declaration in Visual Basic

int my_favorite_num; Dim my_favorite_num as Integer

float weight; Dim weight as Single

float height; Dim height as Single

Some Assignment Statements

Assignment in C Assignment in Visual Basic

my_favorite_num = 5; my_favorite_num = 5

weight = 6.2; weight = 6.2

height = weight; height = weight

weight = weight + 2; weight = weight + 2

Getting input from the user

Input in C from stdin Input in the my_input text box in VB

scanf("%d",

&my_favorite_num);

my_favorite_num = Val(my_input.Text)

scanf("%f",

&height)

height = Val(my_input.Text)

Comments

Comments in C Comments in VB

/* This is a comment in C

*/

’Comments in VB Start with a quote

’and go to the end of the line.

’Multi-line comments need a quote

’at the start of each line

A more complex program: converting feet to inches

Double click on the button again to bring up the code window. Edit the code so it looks like this:

Private Sub my_button_Click()

'Step 1: Declare two variables

Dim how_many_feet As Single

Dim computed_inches As Single

'Step 2: Get your input

how_many_feet = Val(my_input.Text)

'Step 3: Convert from feet to inches

computed_inches = how_many_feet * 12

'Step 4: Clear the picture window

my_output.Cls

'Step 5: Print your answer in the picture window

' Note that semicolons allow you to print things on

' the same line (i.e. they stop a \n)

my_output.Print how_many_feet;

my_output.Print " feet is the same as ";

my_output.Print computed_inches;

my_output.Print " inches!"

End Sub

More Programming Information

Conditionals in C and VB

Conditionals in C Conditionals in VB

if (expression)

{

code;

more code;

even more code;

}

If expression Then

code

more code

even more code

End If

if (expression)

{

code;

more code;

}

else

{

some more code;

and a bit more;

}

If expression Then

code

more code

Else

some more code

and a bit more

End If

Comparison Operators

C VB

> >

>= >=

< <

<= <=

!= <>

= = =

Another program to try:

Private Sub my_button_Click()

'Step 1: Declare two variables

Dim how_many_feet As Single

Dim computed_inches As Single

'Step 2: Get your input

how_many_feet = Val(my_input.Text)

'Step 3: Clear the output window

my_output.Cls

'Step 4: If the input is negative, complain

' Otherwise do the computation

If how_many_feet <>

my_output.Print "Please enter a positive number for feet!!!"

Else

computed_inches = how_many_feet * 12

my_output.Print how_many_feet;

my_output.Print " feet is the same as ";

my_output.Print computed_inches;

my_output.Print " inches!"

End If

End Sub

Loops

Acknowledgements

Thanks to Schneider’s “An Introduction to Programming Using Visual Basic 6.0” for some

introductory ideas.

Loops in C and VB

C VB

while (expression)

{

code;

more code;

}

Do While

code

more code

Loop

for (i=5; i<=23; i++)

{

code;

more code;

}

For i = 5 to 23

code

more code

Next i

for (i=10; i<=30; i=i+5)

{

code;

more code;

}

For i = 10 to 30 Step 5

code

more code

Next i

Thursday, November 20, 2008

Visual Basic Objective Questions

Visual Basic Isnull(), IsEmpty() determines weather any variable has been initialize or not


True False
A
Visual Basic IsDate() function returns true if its argument is a valid date and time



True False B
Visual Basic It is possible to declare 'Dynamic Array' in visual basic.



True False A
Visual Basic In visual basic 'Break' statement could be used along with "Select Case"


True False B
Visual Basic Constants are processed faster than variables :


True False A
Visual Basic It is possible in visual basic to specifying array limit like from 1 to 10


True False A
Visual Basic It is possible to build an application without using any form:

True
False A
Visual Basic Function can return array as return value:


True False A
Visual Basic When using do loop-while statement then the statements within the loop body will be executed only once if the condition does not fulfilled


True False A
Visual Basic ABS() function will generate a hole value when used with a number with fraction part (ex: 125.26598)


True False A
Visual Basic Time() function is used to recover date & time.


True False B
Visual Basic Now() function will return the current drive and directory you are working on as return value.


True False B
Visual Basic The amount of text any one can place in text box is maximum 64 kb.


True False A
Visual Basic In a text box control the default caption for text box is text1.


True False B
Visual Basic It is possible to change the password character property of text box control at run time.


True False B
Visual Basic Sorted property of list box control is a design time property and cannot be changed in runtime.

True False
A
Visual Basic Sorted property of list box control is a design time property and cannot be changed in runtime.
True False A
Visual Basic List count property returns total number of items in list box control.

True False
A
Visual Basic When someone uses the code like list1.list(1); then it will return the first item of the list box control.


True False B
Visual Basic It is possible to insert a picture in a option button control.


True. False A
Visual Basic It is not possible to change the back color property of option button control at run time.

True False
B
Visual Basic by default 'Dim myvar' this statement:

True False A
Visual Basic Sort is a method by which elements can be sorted in flexgrid control


True False B
Visual Basic CommonDialog control is the default control that anyone can find in the toolbar when a new project is started


True False B
Visual Basic The title of the dialog box can be changed.


True False A
Visual Basic Dialog title property is used to change the title of any dialog box

True False
A
Visual Basic Flag property is used to adjust the function of each common dialog box

True False
A
Visual Basic If the Flag constant for the font common dialog box is cdlCFPrinterFonts then it causes the dialog box to show only the fonts supports by the printer specified by the hdc property


True False A
Visual Basic CommonDialogs control is visible at runtime


True False B
Visual Basic Activate event is called before load event


True False B
Visual Basic Terminate is a valid event in form operation

True False
A
Visual Basic In runtime it is not possible to change the form size.


True False B
Visual Basic The default startup object can not be changed in a project


True False B
Visual Basic It is possible to access a menu without using mouse, to access the menu ;pressing the Ctrl key and the letter assigned to access the menu


True False B
Visual Basic It is possible to change the shortcut key assigned to any menu for accessing within the menu editor.


True False B
Visual Basic The project extension name of a VB project is .vbj


True False B
Visual Basic Delete method of the recordset of Data Control or Data Access Object is delete the record which is pointed out by the record pointer.


True False A
Visual Basic The other Single Document Interface forms are by default child of MDI form when MDI form is inserted.


True False B
Visual Basic It is possible to load a MDI form without any childform.


True False A
Visual Basic The arrange property of MDI form is available at design time.

True False
B
Visual Basic In case of visual basic, IDE means :



Internal Database Engineering. Integrated Database Environment. Integrated Development Environment. C
Visual Basic The full form of IIS is :



Indian Institute of Science. Internet Information Service Industrial Information Services. B
Visual Basic In visual basic you can draw something in



Picture box control. Image control. Shape control. A
Visual Basic To run an application you have to press :




F3 F6 F5 F7 C
Visual Basic In visual basic the default unit is :




centimeter. Inch. Dpi. Twips . D
Visual Basic Currency variable stores fixed point numbers with :





2 decimal digits. 3 decimal digits. 4 decimal digits. 6 decimal digits. C
Visual Basic The size of 'Boolean' data type is :



1 Byte. 2 Bytes. 4 Bytes. 8 Bytes. B
Visual Basic In database application, any field does not contain any values can be recognized by:


Nothing value. Null value. Error value. Empty value. B
Visual Basic Redim statement is used to :



Rename a variable. Rename an array. Redimension an array. C
Visual Basic Function Add(Num1 as integer, Num2 as integer) as integer
Add=Num1+Num2
Num1=0
Num2=0
End function

This body is an example of calling up a function by:
By optional argument. By value. By reference.
C
Visual Basic To get the property window in visual basic you have to press

F6 F3 F4 F5 C
Visual Basic To get the property window in visual basic you have to press :

F3 F6 F4 F6 C
Visual Basic Visual Basic produce:

3 types of executable code. 2 types of executable code. 4 types of executable code none of the above. B
Visual Basic In visual basic Bool variable stores

2 bytes 1 bytes 4 bytes none of the above. D
Visual Basic By default 'Dim myvar' this statement:

allocates memory for integer variable allocates memory for variant variable allocates memory for Double variable allocates memory for Boolean variable B
Visual Basic Suppose there are two forms; form1 and form2 ; if there are codes like : In form1.active event
Form2.show

And in form2.active event
Form1.show

Then what will be the output ?


Two forms will be just showed. Two forms will be showed and the control will passed continuously to each other. Nothing will be displayed None of the above. B
Visual Basic A single function of visual basic takes:



Fixed number of parameters Unlimited number of parameters All of the above None of the above B
Visual Basic In a programme body :
Private sub form_load()
X=inputbox("First No. :")
Y=inputbox("Second No. :")
Z=val(X) +val(Y)
Print Z
End sub

What will be the output ?


The summation of two numbers given in two input box It will show the numbers given in the input box side by side It will show nothing None of the above C
Visual Basic when using 'do until-loop' statement as stated below
I=10
Do until I>5
Print I
I=I+1
Loop

This statement will print



it will generate a run time error no output will be shown 10 11 12 13 14 15 none of the above B
Visual Basic which control structure are working under false condition

do-while loop & do loop-while while-wend & for loop do-while loop & while wend loop do-until loop & do loop-until D
Visual Basic what will be the output of the code below :
private sub command_click()
dim I as integer
I=0
Do
Print I
Loop until I>10
End sub



it will generate compile time error it will generate runtime error it is an endless loop 0 1 2 3 4 5 6 7 8 9 10 C
Visual Basic which should be included when an application is used without any forms




subroutines function main sub-main D
Visual Basic It is possible to pass different number parameters to a function when call the function on different time.

To do this one can use in the parameter list of that function paramarray keyword with the array declaration argument should be passed as array none of the above B
Visual Basic To break a loop abnormally when satisfying a condition, we can use




Break statement Exit statement Both i & ii could be used. None of the above. B
Visual Basic What will be the output when the statements below will execute :

Dim a as integer
a=0
while(a<5)
print a
a=a+1
end





It will generate the output like 0 1 2 3 4 It will generate a runtime error It will generate a compiler error It will display nothing C
Visual Basic You can get the ASCII value of any character or number by using

ASC() function ASCII() function There is no function to get the ascii value. A
Visual Basic Say there is a string "Ramcharan"; when someone using Mid() function like MID("Ramcharan",2) then what will be the output:

"Ra" "Ch" "Amcharan" None of the above C
Visual Basic What is the default value of MaxLength property of text box control?

255 characters 10 characters 0 characters Any of the above C
Visual Basic Instr$(text1.text,"visual") will returns :


No. of times the string "visual" is present in the string in text1.text. It puts the control where it finds the text "visual" in the string given in text1.text. First occurrence of the text "visual" within the string given in text1.text None of the above. C
Visual Basic What is the default value for multi-select property of list box control.




1 2 0 none C
Visual Basic If you want a list box control with check box option, which property of list box control you will have to change.




Check box property Check style property Style property None C
Visual Basic Which property of list box control reports the number of selected items.




ListIndex Selected Selcount NewIndex C
Visual Basic Through which property of option button control one can change the font color of the caption.




Back color Font color Fore color None C
Visual Basic If there is a control array of label for 10 elements, then what will be the fifth element in the array?



label(5) label(3) label(4) None
C
Visual Basic The fundamental property of RichTextBox control is

Text property TextRTF property RTFText property All of the above C
Visual Basic One can change or read the alignment of one or more paragraph of rich text box control through

Text Alignment property AlignmentSet property SelAlignment property None C
Visual Basic RichTextBox1.BulletIndent=5 ; what will be the effect of this code if used in any program

It will set the bulleted indentation by the specified value It will create a list of bullet of 5 items All of the above A
Visual Basic To add the commondialog control to any project one has to include it from


File menu->component->Microsoft Common Dialog Control 6.0 Project menu->Component-> Microsoft Common Dialog Control 6.0 Component menu->project-> Microsoft Common Dialog Control 6.0 None of the above B
Visual Basic CommonDialog1.ShowOpen
Filename1=CommonDialog1.Filename
The above code will


Set the filename1 by the selected filename from the common dialog contol. First displays the open dialog box and let the user select any file from any location and then set the filename1 by the selected filename. Both are true. B
Visual Basic Min and Max property can be used with Font common dialog box to determine

To controlling dialog box size The minimum and maximum size displayed in the size list None of the above B
Visual Basic To enable apply button in dialog box ; flag value should be set to




cdlCFApply cdlcfTTOnly cdcclHelpButton None of the above A
Visual Basic If the user wants to select the multiple files from file open and filesave dialog boxes then the flag must be set to




cdlOFNMultiselsectAllow cdlOFNAllowMultiselect It is not possible to select more than one file at a time None B
Visual Basic To draw a form on the screen which event is being called up



Draw Event Load Event Paint Event Either i or iii C
Visual Basic In form load event, if the following code is written then guess what will be the output :
Dim I as integer, J as integer
I=0
J=5
While I Print I
I=I+1
Wend





output will be 0 1 2 3 4 No output, blank form will be shown Compiler error None B
Visual Basic Data1.Recordset.FindFirst "State=NY"
The above code will find the record in a given database


Find the first record in the given database Find the first record in the given database in which the state is NY. The above command will find a record very fast where the state is NY None of the above B
Visual Basic Suppose there is a data control named data1. What will be the effect if the following code is inserted




Data1.recordset.movefirst The record pointer will move to the first record of the record set The pointer will move to the first record of the original table that contains the data It will refresh the recordset B
Visual Basic MDI form1.Arrange vbTileHorizontal; this code in a MDI form will



If there are more than one MDI form then arrange them all in horizontal manner Tiles all child form in horizontal manner Tiles the MDI form horizontally None of the above. B
Visual Basic DocumentForm() it is


A function of MDI forms An array of forms using as child into MDIform None of the above B
Visual Basic Visual Basic has ____________ number of editions 3 4 5 6 A
Visual Basic While running an application you can change the value of any variable and see it's effect through ___________ window.

intermediate Immediate current B
Visual Basic You can get a dropdown list and as well as can add some text directly to ____________ Control.

listbox command Combo Box. none of this C
Visual Basic In _______________ control you can get only drop-down list of the content but cannot add directly anything to that control.

text box combo box List Box Control C
Visual Basic _____________ property of any control cannot change at run time.

color name caption B
Visual Basic The maximum length of a variable is _____________ characters.
255 254 256 257 A
Visual Basic In visual basic, number of loop control structure is _____________.

4 5 6 7 C
Visual Basic In timer control _____________ is the most important property.

Interval. Front backcolor A
Visual Basic There are _________________ no. of built in windows dialog boxes provided by common dialogs control.

6 7 8 9 A
Visual Basic The extension name of a Visual Basic form is _____________.



.frm .txt .prj .vbp A

Your Title