Chapter Two-VISUAL
Chapter Two-VISUAL
VISUAL BASIC
Common Properties:
A property is a value that affects either the appearance of the external object, such
as the BackColor property or the Font property, or its behavior, such as the Enabled
property.
The Properties window is the place where you can change the value of a property at
design time. At execution time, the properties are changed by writing the necessary
code for that, such as the following:
PictureBob1.BackColor=0
Label1.Caption="Welcome"
Or use the reserved word "With" to change a group of properties for a specific object
at once without repeatedly typing the object's name:
With text1
.text="Welcome"
.Font.Bold=True
pg. 1
Chapter Two Visual Basic 6
.BackColor=VBBlack
.ForColor=VBWhite
EndWith
Some internal tools feature a default property that eliminates the need to write this
property after the object name. For example, the Caption property is the default
property for the Label tool, so we can write the following:
Label1="welcome"
Text1="First test"
As for the form window, you can access its properties without typing the form name
using the reserved word “Me” or even without typing it at all. All of the following
statements are true:
As for the form window, you can access its properties without typing the form name
using the reserved word Me or even without typing it at all. All of the following
statements are correct:
Form1.caption="XXXX"
Me.Caption="XXXX"
.Caption="XXXX"
checkbox tool:
This tool gives the user an opportunity to specify a specific option, either by
activating it or not. Therefore, this tool is easy to learn and important at the same
time. We will explain how to use it with an example, knowing that the activation
value is stored in the Value property, which takes one of the following values:
0- Unchecked
1- Checked
2- Grayed
Ex:
Private Sub Chcek1_Click
pg. 2
Chapter Two Visual Basic 6
If Check1.Value=1 then
Image1.visible=True
End If
End Sub
OptionButton tool:
This tool is called Radio Button in some books, and its function is similar to the
checkbox tool, except that the value of the Value property here is True or False,
and you will not be able to make the Value equal to True for more than one radio
button only, so it is preferable to place these buttons in the Frame tool.
ListBox tool:
This widget displays a group of texts inside a box containing scrollBars. The
Sorted property sorts the widget's contents in ascending order based on its
alphabetical letters. You can fill the widget's contents at design time via the List
property or at runtime using the AddItem method.
List1.AddItem "Frist"
List1.AddItem "Second"
Note:
If you are going to add hundreds or thousands of elements at the time of execution,
it is recommended to hide the tool temporarily and after adding it, re-show the tool
again, because the tool automatically redraws itself when any element is added to it,
which causes the device to slow down and the tool itself to jitter.
List1.visible=False
For x=0 to 10000
List1.AddItem x
Next
Iist1.Visible=True
New items are added to the end of the series of items if the value of the Sorted
property is False. You can also delete an item from the list using the RemoveItem
property or delete all items using the Clear method.
List1.RemoveItem 0
pg. 3
Chapter Two Visual Basic 6
List1.Clear
listIndex property:
It returns the value of the selected element in the tool and returns the value (-1) if
there is no selected element. As for the Text property, it returns the text of the
selected element.
list1.ListIndex=0
Print List1.text
listCount property:
This property returns the number of all existing items and is frequently used with the
List property, which enables you to access the item.
1- If the value of the Style property is equal to 0-dropdown, the text tool will
appear with an arrow, and clicking on it will cause the second part of the tool
to appear.
2- If the value of the Style property is equal to 1-Simple, both parts will appear
to the user
3- If the value of the property is equal to 2-dropDown list, it is the same as the
first property or value, except that the user will not be able to write in the text
box.
pg. 4
Chapter Two Visual Basic 6
Ex:
Private Sub Form1_Load()
Combo1.AddItem "Duaa"
Combo1.AddItem "Alrubaye"
End Sub
After placing the items in the list, one can be selected and a set of instructions can
be executed as a result of this selection
IF statement:
If condition Then
' Code to be executed if the condition is True.
Else
' Code to be executed if the condition is False.
End If
pg. 5
Chapter Two Visual Basic 6
• The Else part is optional. If you don't need to execute any code when the
condition is False, you can omit it.
EX:
Dim num As Integer
num = 10
In this example, if the value of num is greater than 5, a message box saying "The
number is greater than 5" will be displayed. Otherwise, the message box saying "The
number is not greater than 5" will be displayed.
You can also have multiple conditions using ElseIf:
If condition1 Then
' Code to be executed if condition1 is True
ElseIf condition2 Then
' Code to be executed if condition2 is True
Else
' Code to be executed if none of the conditions are True
End If
In Visual Basic 6, there are various methods to handle user input. Here's a brief
overview of some common methods:
1. **InputBox Function:**
- The `InputBox` function displays a prompt in a dialog box and waits for the user
to enter text or click a button.
- Example:
```vb
Dim userInput As String
pg. 6
Chapter Two Visual Basic 6
- In this example, an input box will prompt the user to enter their name, and the
entered value will be stored in the `userInput` variable.
2. **TextBox Control:**
- You can use the `TextBox` control on a form to allow users to input text.
- Example:
```vb
' Assume you have a TextBox named txtUserInput on your form
Dim userInput As String
userInput = txtUserInput.Text
MsgBox "You entered: " & userInput
```
- This code retrieves the text entered by the user in a TextBox named
`txtUserInput` and displays it in a message box.
```vb
' Assume you have a ComboBox named cboChoices on your form
Dim selectedChoice As String
selectedChoice = cboChoices.Text
MsgBox "You selected: " & selectedChoice
```
- This code retrieves the selected item from a ComboBox named `cboChoices` and
displays it in a message box.
pg. 7
Chapter Two Visual Basic 6
```vb
Private Sub txtUserInput_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then ' Check if Enter key is pressed
MsgBox "Enter key pressed!"
End If
End Sub
```
pg. 8