1. Trang chủ
  2. » Công Nghệ Thông Tin

Beginning Visual Basic 2005 Databases phần 10 doc

74 185 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 74
Dung lượng 760,64 KB

Nội dung

Private Sub copyToolStripButton_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles copyToolStripButton.Click EditCopy() End Sub 35. Click the Class Name combo box and choose pasteToolStripButton; and in the Method Name combo box, choose the Click event. Add the following code to the pasteToolStripButton_ Click procedure: Private Sub pasteToolStripButton_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles pasteToolStripButton.Click EditPaste() End Sub 36. Click the Class Name combo box and choose UndoToolStripButton; and in the Method Name combo box, choose the Click event. Add the following code to the UndoToolStripButton_ Click procedure: Private Sub UndoToolStripButton_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles UndoToolStripButton.Click EditUndo() End Sub 37. Click the Class Name combo box and choose AddToolStripButton; and in the Method Name combo box, choose the Click event. Add the following code to the AddToolStripButton_ Click procedure: Private Sub AddToolStripButton_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles AddToolStripButton.Click ActionAdd() End Sub 38. Click the Class Name combo box and choose UpdateToolStripButton; and in the Method Name combo box, choose the Click event. Add the following code to the UpdateToolStripButton_ Click procedure: Private Sub UpdateToolStripButton_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles UpdateToolStripButton.Click ActionUpdate() End Sub 39. Click the Class Name combo box and choose DeleteToolStripButton; and in the Method Name combo box, choose the Click event. Add the following code to the DeleteToolStripButton_ Click procedure: Private Sub DeleteToolStripButton_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles DeleteToolStripButton.Click ActionDelete() End Sub 663 Time Tracker Project UI 21_58894x appb.qxd 10/13/05 5:58 PM Page 663 This is all of the code that you need to add to the Admin form. At this point, you can save and run your project and test the menu items and toolbar buttons. You’ll be able to exit the application, cut, copy, paste, undo, and select all functions in the text boxes, and display the Help form. TimeSheet form code In this section, you add some common code to make the TimeSheet form functional. This is not database-related code, as that code will be added in the appropriate chapters. To add this code: 1. Right-click the TimeSheet form in the Solution Explorer window and choose View Code from the context menu. 2. Add some variable declarations to the form first. These variables will be accessible to all procedures in this form. Add the following variables at the top of your class: Public Class TimeSheet ‘Private variables Private intIndex As Integer Private blnEmployeeDisplay As Boolean = True Private strAppTitle As String 3. Get the application title from the executable name and set the current date in the status bar. The form’s Load procedure is the ideal place to perform these operations. Click (Timesheet Events) in the Class Name combo box; and in the Method Name combo box, choose the Load event. Add the following code to the TimeSheet_Load procedure: Private Sub TimeSheet_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load ‘Set the current date in the date panel in the status bar ToolStripDate.Text = Date.Today ‘Get the process title from the executable name strAppTitle = My.Application.Info.Title End Sub 4. Now add some code to exit the application. Click exitToolStripMenuItem in the Class Name combo box and the Click event in the Method Name combo box. Add the following code to the exitToolStripMenuItem_Click procedure: Private Sub exitToolStripMenuItem_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles exitToolStripMenuItem.Click Me.Close() End Sub 5. Now add some code to navigate from the Managers view to the Employees view. Click MyTimeSheetToolStripMenuItem in the Class Name combo box and the Click event in the Method Name combo box. Add the following code to the MyTimeSheetToolStripMenuItem_ Click procedure: 664 Appendix B 21_58894x appb.qxd 10/13/05 5:58 PM Page 664 Private Sub MyTimeSheetToolStripMenuItem_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles MyTimeSheetToolStripMenuItem.Click ‘Undock the Panel pnlManager.Dock = DockStyle.None ‘Move it out of the way pnlManager.Location = New Point(5000, 5000) ‘Set the Dock property to Fill ‘(this will cause the location to change to 0,0) pnlEmployee.Dock = DockStyle.Top ‘Set the view flag blnEmployeeDisplay = True End Sub 6. Add some code to navigate from the Employees view to the Managers view. Click EmployeeTimeSheetsToolStripMenuItem in the Class Name combo box and the Click event in the Method Name combo box. Add the following code to the EmployeeTimeSheetsToolStripMenuItem_Click procedure: Private Sub EmployeeTimeSheetsToolStripMenuItem_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles _ EmployeeTimeSheetsToolStripMenuItem.Click ‘Undock the Panel pnlEmployee.Dock = DockStyle.None ‘Move it out of the way pnlEmployee.Location = New Point(5000, 5000) ‘Set the Dock property to Fill ‘(this will cause the location to change to 0,0) pnlManager.Dock = DockStyle.Top ‘Set the view flag blnEmployeeDisplay = False End Sub 7. Finally, add some code to display the About form. Click aboutToolStripMenuItem in the Class Name combo box and the Click event in the Method Name combo box. Add the following code to the aboutToolStripMenuItem_Click procedure: Private Sub aboutToolStripMenuItem_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles aboutToolStripMenuItem.Click Dim objAbout As New About objAbout.ShowDialog(Me) objAbout.Dispose() objAbout = Nothing End Sub About form code In this section, you add some common code to make the About form functional. The About form can be called from both the Admin form and the TimeSheet form. 665 Time Tracker Project UI 21_58894x appb.qxd 10/13/05 5:58 PM Page 665 To add this code: 1. Right-click the About form in the Solution Explorer window and choose View Code from the context menu. 2. Set the Text property of the various labels on the About form when the form loads. Click the Class Name combo box and choose (About Events); and in the Method Name combo box, choose the Load event. Add the following code to the About_Load procedure: Private Sub About_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load ‘Set this form’s Text property by using the ‘Text property of the parent form Me.Text = “About “ & Owner.Text ‘Set the application icon using the parent form’s icon imgApplicationIcon.Image = Owner.Icon.ToBitmap() ‘Get a reference to the AssemblyInfo for this application Dim objAssemblyInfo As Type = sender.GetType() ‘Set the Text property for the title, version, copyright ‘and description labels lblTitle.Text = My.Application.Info.Title lblVersion.Text = My.Application.Info.Version.ToString lblCopyright.Text = My.Application.Info.Copyright lblDescription.Text = My.Application.Info.Description End Sub That’s all the code that you need for this project at this time. You can test the code for the About form by running your project and choosing the About menu item from the Help menu. When your About form is displayed, you see the information that was read from your Assembly, and it should look similar to the form shown in Figure B-10. Figure B-10 666 Appendix B 21_58894x appb.qxd 10/13/05 5:58 PM Page 666 C Exercise Solutions Chapter 2 Exercise 1 solution The first thing that you do after creating a new Windows application is set a reference to the System.Data.dll namespace. This is done by right-clicking the project in the Solution Explorer and choosing Add Reference from the context menu. Then in the .NET tab of the Add Reference dialog box, you should scroll down the list until you find System.Data.dll, select it, and then click OK. After adding the list box to your form, you switch to the Code Editor for Form1 and then add the following Imports statements: Imports System.Data Imports System.Data.OleDb Public Class Form1 In the Load event for the form, you should have code similar to the code shown here to populate the list box: Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load ‘Declare variables and objects Dim strConnectionString As String = _ “Provider=Microsoft.Jet.OLEDB.4.0;” & _ “Data Source=C:\Program Files\Microsoft Office\Office11\” & _ “Samples\Northwind.mdb;” Dim objConnection As New OleDbConnection(strConnectionString) Dim strSQL As String = _ “SELECT FirstName, LastName FROM Employees” Dim objCommand As New OleDbCommand(strSQL, objConnection) Dim objDataAdapter As New OleDbDataAdapter(objCommand) Dim objDataTable As New Data.DataTable(“Employees”) 22_58894x appc.qxd 10/13/05 5:55 PM Page 667 Dim objDataRow As DataRow Try ‘Open the database connection objConnection.Open() ‘Fill the DataTable object objDataAdapter.Fill(objDataTable) ‘Load the list box on the form For Each objDataRow In objDataTable.Rows ListBox1.Items.Add(objDataRow.Item(“FirstName”) & “ “ & _ objDataRow.Item(“LastName”)) Next Catch OleDbExceptionErr As OleDbException ‘Write the exception Debug.WriteLine(OleDbExceptionErr.Message) Catch InvalidOperationExceptionErr As InvalidOperationException ‘Write the exception Debug.WriteLine(InvalidOperationExceptionErr.Message) End Try ‘Close the database connection objConnection.Close() ‘Clean up objDataRow = Nothing objDataTable.Dispose() objDataTable = Nothing objDataAdapter.Dispose() objDataAdapter = Nothing objCommand.Dispose() objCommand = Nothing objConnection.Dispose() objConnection = Nothing End Sub Exercise 2 solution The code created for this exercise should look very similar to the last Try It Out in this chapter, except that instead of using a DataTable and DataAdapter object, you use just a DataReader object and populate the list box on your form using the DataReader object. The code in the Load event of your form should look similar to the code shown here and your form results should look identical to those shown in Figure 2-3. Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load ‘Declare variables and objects Dim strConnectionString As String = _ “Provider=Microsoft.Jet.OLEDB.4.0;” & _ “Data Source=C:\Program Files\Microsoft Office\Office11\” & _ 668 Appendix C 22_58894x appc.qxd 10/13/05 5:55 PM Page 668 “Samples\Northwind.mdb;” Dim objConnection As New OleDbConnection(strConnectionString) Dim strSQL As String = _ “SELECT FirstName, LastName FROM Employees” Dim objCommand As New OleDbCommand(strSQL, objConnection) Dim objReader As OleDbDataReader Try ‘Open the database connection objConnection.Open() ‘Initialize the DataReader object objReader = objCommand.ExecuteReader() ‘Load the list box on the form While objReader.Read ListBox1.Items.Add(objReader.Item(“FirstName”) & “ “ & _ objReader.Item(“LastName”)) End While Catch OleDbExceptionErr As OleDbException ‘Write the exception Debug.WriteLine(OleDbExceptionErr.Message) Catch InvalidOperationExceptionErr As InvalidOperationException ‘Write the exception Debug.WriteLine(InvalidOperationExceptionErr.Message) End Try ‘Close the DataReader object objReader.Close() ‘Close the database connection objConnection.Close() ‘Clean up objReader.Dispose() objReader = Nothing objCommand.Dispose() objCommand = Nothing objConnection.Dispose() objConnection = Nothing End Sub Chapter 3 Exercise 1 solution After configuring your data source for the DataGridView control, your DataGridView control will be bound to the ProductsBindingSource component, which is automatically generated. Your DataGridView control will also contain columns for ProductName, UnitPrice, UnitsInStock, and UnitsOnOrder. When you run your project, the DataGridView control is populated with the data from these fields in your database. 669 Exercise Solutions 22_58894x appc.qxd 10/13/05 5:55 PM Page 669 Exercise 2 solution After adding your labels and text boxes to your form, you should click the Text property of the DataBindings property to configure your data source as you did in the “Binding Data to TextBox Controls” Try It Out exercise in this chapter. After your data source is configured, you set the Text property of the DataBindings property of each of the text boxes using the appropriate fields in the SuppliersBindingSource component, which was automatically added to your project. Finally, you add a BindingNavigator control to your form and set the BindingSource property to the SuppliersBindingSource component. Chapter 4 Exercise 1 solution The SELECT statement for your query should look like the example shown here: SELECT ProjectName, SequenceNumber FROM Projects ORDER BY ProjectName DESC; Exercise 2 solution The UPDATE statement for your query should look like the example shown here: UPDATE Projects SET ProjectDescription = @ProjectDescription WHERE ProjectID = @ProjectID; Chapter 5 Exercise 1 solution The first thing that you should do is set a reference to the System.Data namespace and add the following Imports statements: Imports System.Data Imports System.Data.OleDb The variables that need to be declared can be declared at the form level or at the procedure level. You should declare the following variables: ‘Form level variables Private strConnectionString As String = _ “Provider=Microsoft.Jet.OLEDB.4.0;” & _ “Data Source=C:\Chapter 5\ProjectTimeTracker.mdb;” Private objConnection As OleDbConnection Private objCommand As OleDbCommand Private objDataAdapter As OleDbDataAdapter Private objDataTable As DataTable 670 Appendix C 22_58894x appc.qxd 10/13/05 5:55 PM Page 670 The code to load the combo box could be placed in the Form Load event or in a button Click event and should look similar to this: ‘Initialize the Connection object objConnection = New OleDbConnection(strConnectionString) ‘Initialize the Command object objCommand = New OleDbCommand(“SELECT ProjectID, ProjectName “ & _ “FROM Projects”, objConnection) ‘Initialize the DataAdapter object and set the SelectCommand property objDataAdapter = New OleDbDataAdapter objDataAdapter.SelectCommand = objCommand ‘Initialize the DataTable object objDataTable = New DataTable ‘Populate the DataTable objDataAdapter.Fill(objDataTable) ‘Bind the DataTable to the ComboBox ComboBox1.DataSource = objDataTable ComboBox1.DisplayMember = “ProjectName” ComboBox1.ValueMember = “ProjectID” ‘Clean up objDataAdapter.Dispose() objDataAdapter = Nothing objCommand.Dispose() objCommand = Nothing objConnection.Dispose() objConnection = Nothing Exercise 2 solution If you coded the variables in Exercise 1 at the form level, you need only two additional variables as shown here: Private objDataReader As OleDbDataReader Private blnIsLoading As Boolean = True You need to modify the code that binds the DataTable to the combo box as follows to turn off the blnIsLoading flag: ‘Bind the DataTable to the ComboBox ComboBox1.DataSource = objDataTable ComboBox1.DisplayMember = “ProjectName” ComboBox1.ValueMember = “ProjectID” ‘Turn off the loading flag blnIsLoading = False 671 Exercise Solutions 22_58894x appc.qxd 10/13/05 5:55 PM Page 671 The code you added to the ComboBox1_SelectedValueChanged event handler should look similar to the code shown here: Private Sub ComboBox1_SelectedValueChanged(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles ComboBox1.SelectedValueChanged ‘Exit if the combo box is being loaded If blnIsLoading Then Exit Sub End If ‘Initialize the Connection object and open it objConnection = New OleDbConnection(strConnectionString) objConnection.Open() ‘Initialize the Command object objCommand = New OleDbCommand ‘Set the objCommand object properties objCommand.CommandText = “usp_SelectProject” objCommand.CommandType = CommandType.StoredProcedure objCommand.Connection = objConnection ‘Add the required parameter for the query objCommand.Parameters.Add(“@ProjectID”, OleDbType.Guid, 16).Value = _ New Guid(ComboBox1.SelectedValue.ToString) ‘Execute the Query objDataReader = objCommand.ExecuteReader() ‘If we have data then display the project description If objDataReader.HasRows Then objDataReader.Read() TextBox1.Text = objDataReader.Item(“ProjectDescription”) End If ‘Close the DataReader and Connection objDataReader.Close() objConnection.Close() ‘Clean up objDataReader = Nothing objCommand.Dispose() objCommand = Nothing objConnection.Dispose() objConnection = Nothing End Sub 672 Appendix C 22_58894x appc.qxd 10/13/05 5:55 PM Page 672 [...]... Property pages for your application by right-clicking the project in the Solution Explorer and choosing Properties from the context menu Then you click the Settings tab in the Property Pages and Visual Studio 2005 displays a dialog box informing you that new settings were imported from the app.config file At this point, you save your project to save the changes Next, you import the System.Data namespace... objData.DataReader.Close() ‘Close the database connection objData.CloseConnection() Catch ExceptionErr As Exception MessageBox.Show(ExceptionErr.Message, “Exercise 1”) End Try End Using End Sub Chapter 10 Exercise 1 solution The first thing that you do is set a reference to the WroxBusinessLogic.dll This is done by clicking the Browse tab in the Add Reference dialog box and browsing to the compiled DLL... Width = 200 End With DataGridView1.Columns.Add(objColumn) objColumn = New DataGridViewTextBoxColumn 690 Exercise Solutions With objColumn HeaderText = “Role Name” DataPropertyName = “RoleName” Width = 100 End With DataGridView1.Columns.Add(objColumn) ‘Clean up objDataAdapter.Dispose() objDataAdapter = Nothing objCommand.Dispose() objCommand = Nothing objConnection.Dispose() objConnection = Nothing objDataSet.Dispose()... DataPropertyName = “UserName” Width = 200 End With DataGridView1.Columns.Add(objColumn) objColumn = New DataGridViewTextBoxColumn With objColumn HeaderText = “Role Name” DataPropertyName = “RoleName” Width = 100 End With DataGridView1.Columns.Add(objColumn) ‘Clean up objDataAdapter.Dispose() objDataAdapter = Nothing objCommand.Dispose() objCommand = Nothing objConnection.Dispose() objConnection = Nothing objDataSet.Dispose() . Panel pnlManager.Dock = DockStyle.None ‘Move it out of the way pnlManager.Location = New Point(5000, 5000) ‘Set the Dock property to Fill ‘(this will cause the location to change to 0,0) pnlEmployee.Dock = DockStyle.Top ‘Set. _ EmployeeTimeSheetsToolStripMenuItem.Click ‘Undock the Panel pnlEmployee.Dock = DockStyle.None ‘Move it out of the way pnlEmployee.Location = New Point(5000, 5000) ‘Set the Dock property to Fill ‘(this will. your Assembly, and it should look similar to the form shown in Figure B -10. Figure B -10 666 Appendix B 21_58894x appb.qxd 10/ 13/05 5:58 PM Page 666 C Exercise Solutions Chapter 2 Exercise 1 solution The

Ngày đăng: 12/08/2014, 10:21

TỪ KHÓA LIÊN QUAN