<<

ﺍﻟﺑﺭﻣﺟﺔ ﺑﻠﻐﺔ ﻓﻳﺟﻭﻝ ﺑﻳﺳﻙ 6 : ﺍﻟﻣﺣﺎﺿﺭﺓ ﺍﻟﺳﺎﺑﻌﺔ Visual Basic 6 Lecture 7

The List Box:

The function of the List Box is to present a list of items where the user can click and select the items from the list or we can use the List Box control as a simple memory to save data. In order to add items to the list, we can use the AddItem method. The items in the list box can be identified by the ListIndex property, the value of the ListIndex for the first item is 0, the second item has a ListIndex 1 and so on.

Example1:

1

ﺍﻟﺑﺭﻣﺟﺔ ﺑﻠﻐﺔ ﻓﻳﺟﻭﻝ ﺑﻳﺳﻙ 6 : ﺍﻟﻣﺣﺎﺿﺭﺓ ﺍﻟﺳﺎﺑﻌﺔ Visual Basic 6 Lecture 7

Code:

Command1:

Private Sub Command1_Click() List1.AddItem Text1.Text Text1.Text = "" Means copy anything in the Text1 into List1 End Sub then after coping clear Text1

Command2:

Private Sub Command2_Click() If List1.ListIndex > -1 Then Means ListIndex start from 0 , the condition List1.RemoveItem List1.ListIndex must start before 0 End If End Sub

Command3:

Private Sub Command3_Click() List1.Clear End Sub

RUN

Click on Add text to add items. To remove any item we must select this item first then remove it.

2

ﺍﻟﺑﺭﻣﺟﺔ ﺑﻠﻐﺔ ﻓﻳﺟﻭﻝ ﺑﻳﺳﻙ 6 : ﺍﻟﻣﺣﺎﺿﺭﺓ ﺍﻟﺳﺎﺑﻌﺔ Visual Basic 6 Lecture 7

Example2: Write a program in VB6 including one Textbox, one Listbox and one Command, showing that when inputting four colors names from Textbox to the Listbox and select any color name in the Listbox then the Form background color will change.

Solution:

Code:

Command1:

Private Sub Command1_Click() List1.AddItem Text1.Text Text1.Text = "" End Sub

List1:

Private Sub List1_Click() If List1.Text = "Red" Then Form1.BackColor = vbRed End If If List1.Text = "Green" Then Form1.BackColor = vbGreen 3

ﺍﻟﺑﺭﻣﺟﺔ ﺑﻠﻐﺔ ﻓﻳﺟﻭﻝ ﺑﻳﺳﻙ 6 : ﺍﻟﻣﺣﺎﺿﺭﺓ ﺍﻟﺳﺎﺑﻌﺔ Visual Basic 6 Lecture 7

End If If List1.Text = "Blue" Then Form1.BackColor = vbBlue End If If List1.Text = "Magenta" Then ﻟﻮﻥ ﺃﺭﺟﻮﺍﻧﻲ Form1.BackColor = vbMagenta End If End Sub

The :

The function of the Combo Box is also to present a list of items where the user can click and select the items from the list. The user needs to click on the small arrowhead on the right of the combo box to see the items which are presented in a drop-down list. In order to add items to the list.

Example3:

4

ﺍﻟﺑﺭﻣﺟﺔ ﺑﻠﻐﺔ ﻓﻳﺟﻭﻝ ﺑﻳﺳﻙ 6 : ﺍﻟﻣﺣﺎﺿﺭﺓ ﺍﻟﺳﺎﺑﻌﺔ Visual Basic 6 Lecture 7

Code:

Command1: Private Sub Command1_Click() Combo1.AddItem Text1.Text Text1.Text = "" End Sub

Command2:

Private Sub Command2_Click() Same as ListBox If Combo1.ListIndex > -1 Then Combo1.RemoveItem Combo1.ListIndex End If End Sub

You can change the size of ComboBox when you change the Font of ComboBox

5

ﺍﻟﺑﺭﻣﺟﺔ ﺑﻠﻐﺔ ﻓﻳﺟﻭﻝ ﺑﻳﺳﻙ 6 : ﺍﻟﻣﺣﺎﺿﺭﺓ ﺍﻟﺳﺎﺑﻌﺔ Visual Basic 6 Lecture 7

Example4: design a program reading Student Name in a TextBox, Gender in a ComboBox, Age in a ComboBox, Department in a ListBox and print the output in a TextBox with red color font.

Solution:

6

ﺍﻟﺑﺭﻣﺟﺔ ﺑﻠﻐﺔ ﻓﻳﺟﻭﻝ ﺑﻳﺳﻙ 6 : ﺍﻟﻣﺣﺎﺿﺭﺓ ﺍﻟﺳﺎﺑﻌﺔ Visual Basic 6 Lecture 7

Code:

Form1:

Private Sub Form_Load() Combo1.AddItem "Male" Combo1.AddItem "Female"

Combo2.AddItem "19" Combo2.AddItem "20" Combo2.AddItem "21" Combo2.AddItem "22"

List1.AddItem "Metal" List1.AddItem "Non Metal" End Sub

Command1:

Private Sub Command1_Click() Text2.Text = Text1.Text + " " + Combo1.Text + " " + Combo2.Text + " " + List1.Text

End Sub

7

ﺍﻟﺑﺭﻣﺟﺔ ﺑﻠﻐﺔ ﻓﻳﺟﻭﻝ ﺑﻳﺳﻙ 6 : ﺍﻟﻣﺣﺎﺿﺭﺓ ﺍﻟﺳﺎﺑﻌﺔ Visual Basic 6 Lecture 7

The Timer: The Timer control allows you to perform a task at a specified interval or to wait for a specified length of time.

Consider the following facts about the Timer control:

1. It is not visible to the user at Run-time 2. The actions that you want to happen at the specified interval are coded in the Timer's Timer event if there is no Command for these actions. 3. The interval value is specified in milliseconds (1000 milliseconds = 1 second).

Timer properties mostly used:

1. Enabled: Used to turn the timer on and off. When on (True), it continues to operate until the Enabled property is set to (False). 2. Interval: To act or repeat the code depending on the number of milliseconds between each invocation of the Timer Event. It takes an integer values (0 - 65535) milliseconds.

8

ﺍﻟﺑﺭﻣﺟﺔ ﺑﻠﻐﺔ ﻓﻳﺟﻭﻝ ﺑﻳﺳﻙ 6 : ﺍﻟﻣﺣﺎﺿﺭﺓ ﺍﻟﺳﺎﺑﻌﺔ Visual Basic 6 Lecture 7

Example5: Design A program to make your first name flashing in a .

Solution:

Code:

Form1: The Interval mostly written in the (Form_Load) Private Sub Form_Load( ) Timer1.Interval = 1000 End Sub

The event must be Timer1: written in the (Timer) Private Sub Timer1_Timer( )

Label1.Visible = Not (Label1.Visible) End Sub

(Not) means that (Label) will be Visible and Non Visible respectively.

9

ﺍﻟﺑﺭﻣﺟﺔ ﺑﻠﻐﺔ ﻓﻳﺟﻭﻝ ﺑﻳﺳﻙ 6 : ﺍﻟﻣﺣﺎﺿﺭﺓ ﺍﻟﺳﺎﺑﻌﺔ Visual Basic 6 Lecture 7

Example6: Repeat example5 with your full name. each name flash alone.

Solution:

Code:

Form1:

Private Sub Form_Load( ) Timer1.Interval = 1000 Timer2.Interval = 1020 Timer3.Interval = 1040 End Sub

Timer1:

Private Sub Timer1_Timer( ) Label1.Visible = Not (Label1.Visible) End Sub

Timer2:

Private Sub Timer2_Timer( )

Label2.Visible = Not (Label2.Visible) End Sub 1

ﺍﻟﺑﺭﻣﺟﺔ ﺑﻠﻐﺔ ﻓﻳﺟﻭﻝ ﺑﻳﺳﻙ 6 : ﺍﻟﻣﺣﺎﺿﺭﺓ ﺍﻟﺳﺎﺑﻌﺔ Visual Basic 6 Lecture 7

Timer3:

Private Sub Timer3_Timer( )

Label3.Visible = Not (Label3.Visible) End Sub

Example7: design a simple Count-Down display in a label, with STOP and Continue Buttons.

Solution:

Code:

Dim a As Integer Stop Button Command1: Private Sub Command1_Click( ) Timer1.Enabled = False End Sub Continue Button Command2: Private Sub Command2_Click( ) Timer1.Enabled = True End Sub 10

ﺍﻟﺑﺭﻣﺟﺔ ﺑﻠﻐﺔ ﻓﻳﺟﻭﻝ ﺑﻳﺳﻙ 6 : ﺍﻟﻣﺣﺎﺿﺭﺓ ﺍﻟﺳﺎﺑﻌﺔ Visual Basic 6 Lecture 7

Form1: Private Sub Form_Load() a = 10 Assumption Timer1.Interval = 1000 End Sub

Timer1: Private Sub Timer1_Timer() a = a - 1 Means (a) will decrease Label1.Caption = a second by second. If a = 0 Then Timer1.Enabled = False End If End Sub

Questions: 1. Design a simple Count-Up display in a label, with STOP and Continue Buttons. 2. Design a program to display your full name (1st name, 2nd name and 3rd name) respectively every two seconds. 3. Write a program to change the Form background color sequentially using RGB method, set the Timer = 500.

H.W 1. Design a Stopwatch (Minutes, Seconds and parts of seconds) using VB6, including Commands (Start, Stop, Continue and Reset)

11