30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 17 Files and Streams 785<br />

58 ' show error if user specified invalid file<br />

59 If (fileName = "" OrElse fileName = Nothing) Then<br />

60 MessageBox.Show("Invalid File Name", "Error", _<br />

61 MessageBoxBut<strong>to</strong>ns.OK, MessageBoxIcon.Error)<br />

62<br />

63 ' else enable all GUI but<strong>to</strong>ns, except for Open File but<strong>to</strong>n<br />

64 Else<br />

65 cmdOpen.Enabled = False<br />

66 cmdCredit.Enabled = True<br />

67 cmdDebit.Enabled = True<br />

68 cmdZero.Enabled = True<br />

69 End If<br />

70<br />

71 End Sub ' cmdOpen_Click<br />

72<br />

73 ' invoked when user clicks Credit Balances, Debit Balances<br />

74 ' or Zero Balances but<strong>to</strong>n<br />

75 Protected Sub cmdGet_Click(ByVal senderObject As Object, _<br />

76 ByVal e As System.EventArgs) Handles cmdCredit.Click, _<br />

77 cmdZero.Click, cmdDebit.Click<br />

78<br />

79 ' convert senderObject explicitly <strong>to</strong> object of type But<strong>to</strong>n<br />

80 Dim senderBut<strong>to</strong>n As But<strong>to</strong>n = CType(senderObject, But<strong>to</strong>n)<br />

81<br />

82 ' get text from clicked But<strong>to</strong>n, which s<strong>to</strong>res account type<br />

83 Dim accountType As String = senderBut<strong>to</strong>n.Text<br />

84<br />

85 ' used <strong>to</strong> s<strong>to</strong>re each record read from file<br />

86 Dim record As CRecord<br />

87<br />

88 ' read and display file information<br />

89 Try<br />

90<br />

91 ' close file from previous operation<br />

92 If (input Is Nothing) = False Then<br />

93 input.Close()<br />

94 End If<br />

95<br />

96 ' create FileStream <strong>to</strong> obtain read access <strong>to</strong> file<br />

97 input = New FileStream(fileName, FileMode.Open, _<br />

98 FileAccess.Read)<br />

99<br />

100 txtDisplay.Text = "The accounts are:" & vbCrLf<br />

101<br />

102 ' traverse file until end of file<br />

103 While True<br />

104<br />

105 ' get next CRecord available in file<br />

106 record = CType(reader.Deserialize(input), CRecord)<br />

107<br />

108 ' s<strong>to</strong>re record's last field in balance<br />

109 Dim balance As Double = record.Balance<br />

Fig. 17.12 FrmCreditInquiry class is a program that displays credit inquiries<br />

(part 3 of 7).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!