Adding the List Window Code

Now that you have the window designed, it's time to add the code behind the scenes. There are many graphical effects that you might want to add, but for reasons of simplicity, the number of “bonus” features has been kept to a minimum. This application is designed to let you focus on working with the Web Service in a typical client application.

Listing 13.1 shows the code for the list window that you need to write. Most of the code in your form is automatically generated and is omitted here for clarity. Much of the generated code deals with the database controls that you already added to the form.

Listing 13.1. frmListStocks.frm—Code for the List Window
						Public Class frmStockMonitor
						Inherits System.Windows.Forms.Form
						Private SS As New StockService.netxmethodsservicesstockquoteStockQuoteService()
						Private WithEvents frmEdit As frmEditStock
						Private Sub tmrInterval_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
 Handles tmrInterval.Tick
						UpdatePrices()
						End Sub
						Private Sub frmStockMonitor_Load(ByVal sender As System.Object, ByVal e As System
.EventArgs) Handles MyBase.Load
						daStocks.Fill(dsStocks.Tables(0))
						grdStocks.SetDataBinding(dsStocks.Tables(0), "")
						UpdatePrices()
						End Sub
						Private Sub UpdatePrices()
						Dim dr As DataRow
						For Each dr In dsStocks.Tables(0).Rows
						dr("CurrentPrice") = SS.getQuote(dr("StockID").ToString())
						dr("LastPriceUpdate") = Now()
						Next dr
						daStocks.Update(dsStocks.Tables(0))
						End Sub
						Private Sub frmStockMonitor_Resize(ByVal sender As Object, ByVal e As System.EventArgs)
 Handles MyBase.Resize
						grdStocks.Height = Height - (grdStocks.Top * 2)
						grdStocks.Width = Width - (grdStocks.Left * 2)
						grdStocks.PreferredColumnWidth = grdStocks.Width / 5
						End Sub
						Private Sub mnuFuncEdit_Click(ByVal sender As System.Object, ByVal e As System
.EventArgs) Handles mnuFuncEdit.Click
						Dim dr As DataRow
						dr = dsStocks.Tables(0).Rows(grdStocks.CurrentCell.RowNumber)
						frmEdit = New frmEditStock(dr("StockID"), dr("LowPriceAlert"), dr("HighPriceAlert"))
						End Sub
						Private Sub mnuFuncAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs)
 Handles mnuFuncAdd.Click
						frmEdit = New frmEditStock()
						End Sub
						Private Sub mnuFuncExit_Click(ByVal sender As Object, ByVal e As System.EventArgs)
 Handles mnuFuncExit.Click
						Me.Close()
						End Sub
						Private Sub frmEdit_EditComplete(ByVal strStock As String, ByVal dblLowPrice As Double,
 ByVal dblHighPrice As Double) Handles frmEdit.EditComplete
						Dim dr As DataRow
						For Each dr In dsStocks.Tables(0).Rows
						If dr("StockID") = strStock Then
						dr("LowPriceAlert") = dblLowPrice
						dr("HighPriceAlert") = dblHighPrice
						UpdatePrices()
						Exit For
						End If
						Next
						End Sub
						Private Sub frmEdit_AddComplete(ByVal strStock As String, ByVal dblLowPrice As Double,
 ByVal dblHighPrice As Double) Handles frmEdit.AddComplete
						Dim dr As DataRow = dsStocks.Tables(0).NewRow
						dr("StockID") = strStock
						dr("LowPriceAlert") = dblLowPrice
						dr("HighPriceAlert") = dblHighPrice
						dsStocks.Tables(0).Rows.Add(dr)
						UpdatePrices()
						End Sub
						Private Sub mnuFuncDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
 Handles mnuFuncDel.Click
						If MessageBox.Show("Delete this stock?", "Stock Monitor", _
						MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) = DialogResult.Yes Then
						dsStocks.Tables(0).Rows(grdStocks.CurrentCell.RowNumber).Delete()
						daStocks.Update(dsStocks)
						End If
						End Sub
						End Class
					

Starting at the top, we have some form-level variables that are declared. The SS variable is a reference to the StockService Web Service that has been referenced in the project already. The frmEdit variable refers to the stock editing form that you will create in the next section. This form will be raising events to the list window, so the variable is declared using the WithEvents keyword.

The first event handler, tmrInterval_Tick, in the listing is the Tick event of the Timer control. When the timer expires, the stock prices are updated via the UpdatePrices subroutine. This subroutine is also called when the window initially loads, shown in the Load event handler, frmStockMonitor_Load, next in the listing.

The Load event has two tasks—bind the DataGrid to the DataSet and update the prices via the Web Service. The binding is done by first filling the DataSet from the DataAdapter and then by setting the binding to the grid. After this is done, the UpdatePrices subroutine gets the latest prices for each stock in the table.

This update is done in the UpdatePrices subroutine, which loops through each row in the DataSet and gets the current price for each stock symbol from the Web Service. The Web Service being used has a getQuote method that accepts a stock symbol and returns the last price for the stock. This price is then stored in the DataSet, and when all prices have been retrieved, the DataSet is stored in the database.

The database has the ability to store a high and low price, as well, and if the new price comes back exceeding the high price or under the low price, this routine could generate some sort of message. This feature hasn't been implemented, but it would be easy to add in this routine.

The Resize event of the form is managed by the frmStockMonitor_Resize subroutine. This event is used to resize the DataGrid to fill the form. This is done by using the upper-left corner of the DataGrid as a reference point. The margins are generated on the top and left by resizing the grid to fill the remaining space. You can modify this routine to enforce a minimum size if you wanted, but that's outside the scope of the chapter.

The rest of the code that you need to create next deals with the interaction between the list window and the editing window. The Add and Edit menu choice handlers both create new instances of the edit window; however, the Edit menu choice also passes along the stock to be edited and its low and high price alert values. Those values are passed into one of the constructors, and the Add menu choice calls a different constructor that doesn't require any arguments.

After the edits are complete in the editor window, either the AddComplete or EditComplete event is raised and intercepted by the list window. In both cases, the stock information and low/high price values are retrieved from the event data. For a new record, the new stock is added to the DataSet. For a changed record, the existing record is found and updated with the new data. In both cases, the DataSet is then saved to the database by way of the SqlDataAdapter object on the form. Having the edit window transmit the data back to the list window eliminates the need to have database code in both windows and makes the code somewhat easier to follow.

The last event code you need to write, other than the trivial Exit menu choice code, is the code to delete a stock. After confirming the delete with the user, the stock is removed from the DataSet. The DataSet is saved to the database and the record is removed automatically. While this works fine for our simple database, most more complex databases would include related tables for that stock. Deleting the primary key while still having related records would cause referential integrity errors, so don't count on the database to do everything for you automatically.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.165.62