Thursday, July 19, 2012

Sample Program #8


Let us create a program that will exhibit a passing of data between listboxes. The events of our program is to put the entered text from txtname to lstdata1 and after putting it, we will erase the string inside the textbox. No empty string will be inputted inside the lstdata1 thus we will put a condition to prevent it to happen. If "To Right" or "To left" button is pressed, the selected data from lstdata1 will be transferred to lstdata2 or vice versa. If "All Right" or "All Left" button is pressed, all data inside the listbox will be transferred to the other listbox. Here is the code:


Private Sub cmdadd_Click()
  If Trim(txtname.Text) <> "" Then
    lstdata1.AddItem txtname.Text
  End If
  txtname.Text = ""
End Sub

Private Sub cmdallleft_Click()
  Dim index As Integer
  For index = 0 To lstdata2.ListCount - 1
    lstdata1.AddItem lstdata2.List(index)
  Next index
  lstdata2.Clear
End Sub

Private Sub cmdallright_Click()
  Dim index As Integer
  For index = 0 To lstdata1.ListCount - 1
    lstdata2.AddItem lstdata1.List(index)
  Next index
  lstdata1.Clear
End Sub

Private Sub cmdtoleft_Click()
  If lstdata2.ListIndex <> -1 Then
    lstdata1.AddItem lstdata2.List(lstdata2.ListIndex)
    lstdata2.RemoveItem lstdata2.ListIndex
  End If
End Sub

Private Sub cmdtoright_Click()
  If lstdata1.ListIndex <> -1 Then
    lstdata2.AddItem lstdata1.List(lstdata1.ListIndex)
    lstdata1.RemoveItem lstdata1.ListIndex
  End If
End Sub

Friday, July 13, 2012

Sample Program #7

















Let us create a program that will get the average of three(3) numbers and control the decimal place of the result using the format function.


Private Sub cmdave_Click()
  Dim a, b, c As Integer
  Dim d As Currency

  a = Val(txtn1.Text)
  b = Val(txtn2.Text)
  c = Val(txtn3.Text)
 
  d = (a + b + c) / 3
  txtave.Text = "The average is " + Format(Trim(Str(d)), "0.00") 
End Sub

Discussion:

The format string function  varies between 0 and # sign. The 0 forces the number format to display the zero even if the amount of the number does not reach the digit position while the # sign on the other hand will not. The purpose of using the # sign is to limit the number of digit to be displayed. For example, using #.## in the code above will make a display of .5 instead of 0.50, 3. instead of 3.00 and 1.5 instead of 1.50.

Sample Program #6















Let us create a program that will get the first and last letter of an entered string combining the selstart, sellength, seltext and len() function.



'LONG METHOD STYLE CODE
Private Sub cmdfind_Click()
  Dim a As Integer
 
  txta.SelStart = 0
  txta.SelLength = 1
  txtb.Text = "The first letter is " + UCase(txta.SelText)
 
  a = Len(txta.Text) - 1
  txta.SelStart = a
  txta.SelLength = 1
  txtb.Text = txtb.Text + " and the last letter is " + UCase(txta.SelText)
 
  txta.SelStart = a + 1
End Sub


'SHORT METHOD STYLE CODE
Private Sub cmdfind_Click()
 
  txta.SelStart = 0
  txta.SelLength = 1
  txtb.Text = "The first letter is " + UCase(txta.SelText)
 
  txta.SelStart = Len(txta.Text) - 1
  txta.SelLength = 1
  txtb.Text = txtb.Text + " and the last letter is " + UCase(txta.SelText)
 
  txta.SelStart = Len(txta.Text)
End Sub

Sample Program #5















Let us create a program that will count base on the given start and end value using the different types of loop.


'USING FOR LOOP
Private Sub cmdcal_Click()
  Dim a, b, c As Integer
 
  a = Val(txtn1.Text)
  b = Val(txtn2.Text)
 
  For c = a To b
    txtsum.Text = txtsum.Text + Trim(Str(c)) + " "
  Next c
End Sub
  
'USING DO LOOP UNTIL
Private Sub cmdcal_Click()  
  Dim a, b, c As Integer
 
  a = Val(txtn1.Text)
  b = Val(txtn2.Text)
 
  Do
    txtsum.Text = txtsum.Text + Trim(Str(a)) + " "
    a = a + 1
  Loop Until (a > b)
End Sub 

  
'USING DO LOOP WHILE
Private Sub cmdcal_Click()  
  Dim a, b, c As Integer
 
  a = Val(txtn1.Text)
  b = Val(txtn2.Text)

  Do
    txtsum.Text = txtsum.Text + Trim(Str(a)) + " "
    a = a + 1
  Loop While (a <= b)
End Sub


'USING DO WHILE LOOP
Private Sub cmdcal_Click()  
  Dim a, b, c As Integer
 
  a = Val(txtn1.Text)
  b = Val(txtn2.Text)
  Do While (a <= b)
    txtsum.Text = txtsum.Text + Trim(Str(a)) + " "
    a = a + 1
  Loop
End Sub

Sample Program #4


 













Let us create a program that will evaluate the smallest and the largest number from the three given inputs. We will enhance our condition using the AND and OR operators. In this example, we will use only AND.


Private Sub cmdcal_Click()
  Dim a, b, c As Integer
 
  a = Val(txtn1.Text)
  b = Val(txtn2.Text)
  c = Val(txtn3.Text)
 
  If (a < b) And (a < c) Then
    txtsum.Text = "The smallest number is " + Trim(Str(a))
  ElseIf (b < a) And (b < c) Then
    txtsum.Text = "The smallest number is " + Trim(Str(b))
  Else
    txtsum.Text = "The smallest number is " + Trim(Str(c))
  End If
 
  If (a > b) And (a > c) Then
    txtsum.Text = txtsum.Text + " and the largest is " + Trim(Str(a))
  ElseIf (b > a) And (b > c) Then
    txtsum.Text = txtsum.Text + " and the largest is " + Trim(Str(b))
  Else
    txtsum.Text = txtsum.Text + " and the largest is " + Trim(Str(c))
  End If
End Sub

'ANOTHER METHOD STYLE CODE
Private Sub cmdcal_Click()
  Dim a, b, c As Integer
 
  a = Val(txtn1.Text)
  b = Val(txtn2.Text)
  c = Val(txtn3.Text)
 
  If (a < b) And (a < c) Then
    txtsum.Text = "The smallest number is " + Trim(Str(a))
    If (b > c) Then
      txtsum.Text = txtsum.Text + " and the largest is " + Trim(Str(b))
    Else
      txtsum.Text = txtsum.Text + " and the largest is " + Trim(Str(c))
    End If
  ElseIf (b < a) And (b < c) Then
    txtsum.Text = "The smallest number is " + Trim(Str(b))
    If (a > c) Then
      txtsum.Text = txtsum.Text + " and the largest is " + Trim(Str(a))
    Else
      txtsum.Text = txtsum.Text + " and the largest is " + Trim(Str(c))
    End If
  Else
    txtsum.Text = "The smallest number is " + Trim(Str(c))
    If (b > a) Then
      txtsum.Text = txtsum.Text + " and the largest is " + Trim(Str(b))
    Else
      txtsum.Text = txtsum.Text + " and the largest is " + Trim(Str(a))
    End If
  End If
End Sub

Sample Program #3
















Let us put an evaluation of the calculated sum using condition statement. The evaluations would be, "greater than 10", "less than 10" and "equal to 10" enhancing Sample Program #2.

'LONG METHOD STYLE CODE
Private Sub cmdcal_Click()
  Dim a, b, c As Integer

  a = Val(txtn1.Text)
  b = Val(txtn2.Text)
  c = a + b
  If (c < 10) Then
    txtsum.Text = "The sum is " + Trim(Str(c)) + " and is less than 10"
  ElseIf (c > 10) Then
    txtsum.Text = "The sum is " + Trim(Str(c)) + " and is greater than 10"
  Else
    txtsum.Text = "The sum is " + Trim(Str(c)) + " and is equal to 10"
  End If
End Sub

'SHORT METHOD STYLE CODE
Private Sub cmdcal_Click()
  txtsum.Text = "The sum is " + Trim(Str(Val(txtn1.Text) + Val(txtn2.Text)))
  If (Val(txtn1.Text) + Val(txtn2.Text) < 10) Then
    txtsum.Text = txtsum.Text + " and is less than 10"
  ElseIf (Val(txtn1.Text) + Val(txtn2.Text) > 10) Then
    txtsum.Text = txtsum.Text + " and is greater than 10"
  Else
    txtsum.Text = txtsum.Text + " and is equal to 10"
  End If
End Sub





Discussion:

Here, the condition statement if,elseif and else are connected thus if either of the conditions is true, the computer will no longer enter to the next condition block.

Thursday, July 12, 2012

Sample Program #2



Let us create a program enhancing the result from Sample Program #1. Aside of stating only the sum, combine the statement "The sum is" to make a human like calculator.


'LONG METHOD STYLE CODE
Private Sub cmdcal_Click()
  Dim a, b, c As Integer

  a = Val(txtn1.Text)
  b = Val(txtn2.Text)
  c = a + b
  txtsum.Text = "The sum is " + Trim(Str(c))
End Sub

'SHORT METHOD STYLE CODE
Private Sub cmdcal_Click()
  txtsum.Text = "The sum is " + Trim(Str(Val(txtn1.Text) + Val(txtn2.Text)))
End Sub

Discussion:

Basing on the previous example, we simply add the statement (THE SUM IS) which is inside a double quotation mark. The double quotation mark will tell the compiler that this statement does not contain any variable. To fuse it with a variable, just add a plus sign in between.

Sample Program #1
















Let us create a program that will calculate the sum of two integers using the val(), str() and trim() function.


'LONG METHOD STYLE CODE
Private Sub cmdcal_Click()
  Dim a, b, c As Integer

  a = Val(txtn1.Text)
  b = Val(txtn2.Text)
  c = a + b
  txtsum.Text = Trim(Str(c))
End Sub


'SHORT METHOD STYLE CODE
Private Sub cmdcal_Click()
  txtsum.Text = Trim(Str(Val(txtn1.Text) + Val(txtn2.Text)))
End Sub

Discussion:

The val() function will convert any string type value to a numeric value while the str() on the other hand will convert the numeric value to string type. During the conversion of numeric data type to string, a space is added thus trim() function will remove the spaces