Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Tham khảo tài liệu 'manning windows forms programming (phần 11)', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | Add a menu to display album properties continued Action Result 7 Display an error message if the album could not be opened. Note Here and throughout the remainder of the book we use the simplest form of the MessageBox dialog. Feel free to use an alternate form if you prefer. See chapter 8 for detailed information on the MessageBox class. if album null MessageBox.Show The properties for this album cannot be displayed. return 8 Display the AlbumEditDlg if the album is opened successfully. using AlbumEditDlg dlg new AlbumEditDlg album 9 If any changes are made by the user save these changes into the album file. Catch any errors that occur. if dlg.ShowDialog DialogResult.OK Save changes made by the user try album.Save catch Exception MessageBox.Show Unable to save changes to album. return 10 Also update any subitem text that might be affected by the user s changes. Update subitem settings item.SubItems MainForm. AlbumTitleColumn .Text album.Title bool hasPwd album.Password null album.Password.Length 0 item.SubItems MainForm. AlbumPwdColumn .Text hasPwd y n 11 Dispose of the album at the end of the method. album.Dispose We employ the using statement to ensure that our dialog is properly disposed of at the end of the handler. Also note how multiple exceptional handling blocks are used to catch errors that occur. You may wonder if it is expensive to perform such operations especially if you are familiar with exception-handling mechanisms in languages like C and C where it indeed can be an expensive proposition to call try multiple times. In C the exception handling is built into the language and the compiler so checking for exceptions as we do here is not much more expensive than an if statement. The expense comes if an exception actually occurs since the compiler must 466 CHAPTER 14 List views then construct the Exception object unravel the call stack and clean up any objects as required plus locate the appropriate catch block for the particular exception. The fact that