JunkieHF

Apartment Rent (VB Source Code for Beginners)

Jul 12th, 2014
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.    
  2.  
  3.     'Made by Junkie
  4.    '10/13/2013
  5.    Option Strict On
  6.     Public Class frmRentList
  7.      
  8.     #Region "Constant Declarations"
  9.      
  10.         Private Const FLOOR_NUM_MIN As Integer = 3
  11.         Private Const FLOOR_NUM_MAX As Integer = 20
  12.         Private Const RENT_LOWER_LIMIT As Integer = 1000
  13.         Private Const RENT_UPPER_LIMIT As Integer = 2500
  14.      
  15.     #End Region
  16.      
  17.     #Region "Form Level Variable Declarations"
  18.         Private numFloors As Integer
  19.         Private requestedFloor As Integer
  20.         Private rentList() As Integer
  21.     #End Region
  22.      
  23.     #Region "Validating Event Handlers"
  24.         'here we are validating the text boxes for name and the number of floors
  25.       Private Sub NameValidatingEvent(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles _
  26.                                                                                                                txtName.Validating,
  27.                                                                                                                txtNumFloors.Validating
  28.             Dim txtbox As TextBox = CType(sender, TextBox)
  29.             Dim datadescription As String = String.Empty
  30.             If sender Is txtName Then
  31.                 datadescription = "Name"
  32.             ElseIf sender Is txtNumFloors Then
  33.                 datadescription = "Number of Floors"
  34.             End If
  35.             e.Cancel = ValidateStringInput(datadescription, txtbox.Text)
  36.         End Sub
  37.         Private Sub NumberValidatingEvent(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles _
  38.             txtNumFloors.Validating
  39.             Const FLOORS_LOWER_LIMIT As Integer = 3
  40.             Const FLOORS_UPPER_LIMIT As Integer = 20
  41.             Dim txtBox As TextBox = CType(sender, TextBox)
  42.             If sender Is txtNumFloors Then
  43.                 e.Cancel = ValidateNumberInput("Number of Floors", txtBox.Text, FLOORS_LOWER_LIMIT, FLOORS_UPPER_LIMIT, numFloors)
  44.                 btnRentList.Enabled = Not (e.Cancel)
  45.             End If
  46.             If e.Cancel Then
  47.                 txtBox.Clear()
  48.             End If
  49.         End Sub
  50.     #End Region
  51.     #Region "Utility Modules"
  52.         Private Function ValidateStringInput(ByVal dataDescription As String, ByVal strInput As String) As Boolean
  53.             Dim invalid As Boolean = True
  54.             If Not String.IsNullOrEmpty(strInput) Then
  55.                 invalid = False
  56.             Else
  57.                 MessageBox.Show(dataDescription & " must be provided", "Empty " & dataDescription, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  58.                 invalid = True
  59.             End If
  60.             Return invalid
  61.         End Function
  62.         Private Function ValidateNumberInput(ByVal dataType As String, ByVal strInput As String, _
  63.                                               ByVal min As Integer, ByVal max As Integer, _
  64.                                               ByRef value As Integer) As Boolean
  65.             Dim invalid As Boolean = True
  66.      
  67.             If IsNumeric(strInput) Then
  68.                 value = Integer.Parse(strInput)
  69.                 If value >= min AndAlso value <= max Then
  70.                     invalid = False
  71.                 Else
  72.                     MessageBox.Show(dataType & " must be between " & min & " and " & max & "--try again", "Invalid " & dataType, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  73.                     invalid = True
  74.                 End If
  75.             Else
  76.                 MessageBox.Show(dataType & " must be between " & min & " and " & max & "--try again", "Invalid " & dataType, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  77.                 invalid = True
  78.             End If
  79.             Return invalid
  80.         End Function
  81.         Private Sub ClearFields()
  82.             txtName.Clear()
  83.             txtNumFloors.Clear()
  84.             txtFloorRequest.Clear()
  85.             lstRents.Items.Clear()
  86.             lblRentResults.Visible = False
  87.             btnRentList.Enabled = False
  88.             btnGetFloorRent.Enabled = False
  89.         End Sub
  90.         'getting the rent data
  91.       Private Sub CollectFloorRentData()
  92.             lstRents.Items.Add("Floor" & vbTab & "Rent")
  93.             If numFloors > 0 Then
  94.                 ReDim rentList(numFloors - 1)
  95.                 For i As Integer = 0 To rentList.Count - 1
  96.                     rentList(i) = GetData("Floor Rent: ", RENT_LOWER_LIMIT, RENT_UPPER_LIMIT)
  97.                     addtodisplaylist(i + 1, rentList(i))
  98.                 Next
  99.             End If
  100.         End Sub
  101.         'displaying the floor numbers and rent for each floor
  102.       Private Sub addtodisplaylist(ByVal numfloors As Integer, ByVal rentlist As Integer)
  103.             lstRents.Items.Add(numfloors.ToString & vbTab _
  104.                                & rentlist.ToString)
  105.         End Sub
  106.         Private Function GetData(ByVal dataDescription As String, ByVal lowerLimit As Integer, ByVal upperLimit As Integer) As Integer
  107.             'getting floor level data
  108.           Dim value As Integer
  109.             Dim response As String
  110.             Do
  111.                 response = InputBox("Enter the number of " & dataDescription & "(" & lowerLimit.ToString & " to " & upperLimit.ToString & "): ")
  112.                 ValidateNumberInput(dataDescription, response, lowerLimit, upperLimit, value)
  113.             Loop Until (value >= lowerLimit AndAlso value <= upperLimit)
  114.             Return value
  115.         End Function
  116.         'finding and verifying the user specified floor
  117.       Private Sub findfloordata()
  118.             Dim floorrequest As Integer = CInt(txtFloorRequest.Text)
  119.             If floorrequest <= numFloors Then
  120.                 DisplaySummaryData(rentList(floorrequest - 1))
  121.             Else
  122.                 MessageBox.Show("Floor request must be between 1 and " & numFloors, "Error" & MessageBoxButtons.OK & MessageBoxIcon.Exclamation)
  123.             End If
  124.         End Sub
  125.         'this is where the results are displayed in the rent total label
  126.    
  127.         Private Sub DisplaySummaryData(ByVal rentlist As Integer)
  128.             lblRentResults.Visible = True
  129.             lblRentResults.Text = ("Apartment Name " & txtName.Text & vbNewLine _
  130.                                 & "Floor Number " & txtFloorRequest.Text & vbNewLine _
  131.                                 & "Rent " & rentlist)
  132.         End Sub
  133.     #End Region
  134.         Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
  135.             'exit button
  136.           Me.Close()
  137.         End Sub
  138.         Private Sub frmRentList_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  139.      
  140.         End Sub
  141.         Private Sub btnRentList_Click(sender As Object, e As EventArgs) Handles btnRentList.Click
  142.             'displaying rent in the list box
  143.           CollectFloorRentData()
  144.      
  145.         End Sub
  146.         Private Sub btnGetFloorRent_Click(sender As Object, e As EventArgs) Handles btnGetFloorRent.Click
  147.             'displaying rent total for given floor
  148.           findfloordata()
  149.         End Sub
  150.         Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
  151.             'clear button
  152.           Me.lblRentResults.Text = ""
  153.             Me.lstRents.Text = ""
  154.             Me.txtFloorRequest.Text = ""
  155.             Me.txtName.Text = ""
  156.             Me.txtNumFloors.Text = ""
  157.         End Sub
  158.     End Class
Add Comment
Please, Sign In to add comment