Zebra0.com

visual-basic examplesProfessor Joy: CMSC222 Class Work

Professor Joy: CMSC222 Class Work

Class Work Week 11

Scroll bars

The first example has two scroll bars each calls calculate.
Public Class Form1

    Private Sub ScrCost_Scroll(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.ScrollEventArgs) Handles ScrCost.Scroll
        LblCost.Text = FormatCurrency(ScrCost.Value / 100)
        Calculate()
    End Sub

    Private Sub ScrRate_Scroll(ByVal sender As System.Object, ByVal e As _ 
          System.Windows.Forms.ScrollEventArgs) Handles ScrRate.Scroll
        LblRate.Text = ScrRate.Value
        Calculate()
    End Sub
    Private Sub Calculate()
        Dim Cost As Double = ScrCost.Value / 100
        Dim Rate As Integer = ScrRate.Value
        Dim Result As Double = Cost * Rate / 100
        LblResult.Text = FormatCurrency(Result)
    End Sub
End Class
The second example uses global variables:
Public Class Form1
    Dim Cost As Double
    Dim Rate As Integer
    Private Sub ScrCost_Scroll(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.ScrollEventArgs) Handles ScrCost.Scroll
        Cost = ScrCost.Value / 100
        LblCost.Text = FormatCurrency(Cost)
        Calculate()
    End Sub

    Private Sub ScrRate_Scroll(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.ScrollEventArgs) Handles ScrRate.Scroll
        Rate = ScrRate.Value
        LblRate.Text = ScrRate.Value
        Calculate()
    End Sub
    Private Sub Calculate()
        Dim Result As Double = Cost * Rate / 100
        LblResult.Text = FormatCurrency(Result)
    End Sub
End Class
The third example lets one sub handle both scroll bars:
Public Class Form1

    Private Sub Calculate(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.ScrollEventArgs) Handles ScrCost.Scroll, ScrRate.Scroll
        Dim Cost As Double = ScrCost.Value / 100
        Dim Rate As Integer = ScrRate.Value
        LblCost.Text = FormatCurrency(Cost)
        LblRate.Text = ScrRate.Value
        Dim Result As Double = Cost * Rate / 100
        LblResult.Text = FormatCurrency(Result)
    End Sub
End Class

This web site, and all pages therein, are the sole property and responsibility of Zebra0.com.
It is not endorsed, sponsored, or provided by or on behalf of Montgomery College.