ordersform.vb
来自「wrox出版社的另一套经典的VB2005数据库编程学习书籍,收集了书中源码,郑重」· VB 代码 · 共 1,392 行 · 第 1/5 页
VB
1,392 行
'*********************************************************
'For the instructions on how to run this application, see
'http://www.oakleaf.ws/concurrency/
'
'This application expects to run the Northwind sample
'database on 'localhost.' Change the connection string
'on the Project Properties' Settings page if you're running
'SQL Server 2005 Express or a remote SQL Server 2000 or
'2005 instance.
'**********************************************************
Option Explicit On
Option Strict On
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Public Class OrdersForm
Private Const strTitle As String = "Data Entry Error!"
Private blnAutoContinue As Boolean = True
Private blnIsNewCustomer As Boolean
Private blnIsNewOrder As Boolean
Private blnSkipConcurrency As Boolean
Private blnSkipCount As Boolean
Private blnSyncCustomerID As Boolean
'For concurrency tests
Private strTableErr As String
Private intErrorRow As Integer
Private intDelOrderID As Integer
Private intDelProductID As Integer
Private intDelDetailsID As Integer
Private intLocalDetails As Integer
Private intServerDetails As Integer
Private blnSkipDetailsDelete As Boolean
Private htCurrent As Hashtable = New Hashtable
Private alOrdersConcur As New ArrayList
Private alDetailsConcur As New ArrayList
'For text box validation example
Private epCompanyName As ErrorProvider
'For disconnected user emulation
Private strDataSetFile As String = Application.StartupPath + "\LocalDiffGram.xml"
Private strChangesFile As String = Application.StartupPath + "\LocalTestChanges.xml"
Private blnSaveNewOrders As Boolean
Private blnDisconnectCanceled As Boolean
'To persist Customers combo box list in App.config
Private intCustomerCount As Integer
Private strCustomerList As String
'HTML Help File variables
Private strIEFile As String = "\Program Files\Internet Explorer\IExplore.exe"
Private strHelpPath As String = Application.StartupPath + "\Help\"
'*********************************************
'Form_Load and Form_KeyPress event handlers
'and related control enable/disable procedures
'*********************************************
Private Sub OrdersForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Lock down the form until user action occurs
LockTextBoxes(True, True)
EnableOrdersGrid(False, True)
EnableOrder_DetailsGrid(False, True)
ShowCustomerEditButtons(False)
OrdersToolStrip.Visible = False
'Flag: chkDisconnected modification
'Load the user's CustomerID combo box from My.Settings
'CustomersList is a comma-separated string of CustomerIDs
'Added due to difficulty manually editing updated My.Settings.CustomersList
'User settings are in C:\Documents and Settings\UserName\Local Settings
'\Application Data\CompanyName\AppName_GeneratedSuffix\1.0.0.0\user.config
Dim blnCleanUserConfig As Boolean = True
If blnCleanUserConfig Then
'Remove typical CustomerIDs added in online help examples
Dim strCusts As String = My.Settings.CustomersList
strCusts = Replace(strCusts, "BOGUS", "")
strCusts = Replace(strCusts, "BOGUE", "")
strCusts = Replace(strCusts, "BOGUX", "")
strCusts = Replace(strCusts, ",,", ",")
If strCusts.EndsWith(",") Then
strCusts = strCusts.Remove(strCusts.Length - 1, 1)
End If
My.Settings.CustomersList = strCusts
My.Settings.Save()
End If
Dim astrCusts As String() = Split(My.Settings.CustomersList, ",")
With CustomerIDToolStripComboBox
'Load the dropdown list from the CustomerList (user setting)
.Items.Clear()
Dim intCtr As Integer
For intCtr = 0 To astrCusts.Length - 1
.Items.Add(astrCusts(intCtr))
Next intCtr
'Select the first item
.SelectedIndex = 0
End With
'HACK 02: Set up CompanyName text box error provider
'For CompanyNameTextBox validation example
epCompanyName = New ErrorProvider()
With epCompanyName
.SetIconAlignment(CompanyNameTextBox, ErrorIconAlignment.MiddleRight)
.SetIconPadding(CompanyNameTextBox, 2)
.BlinkRate = 500 'half-second
.BlinkStyle = System.Windows.Forms.ErrorBlinkStyle.AlwaysBlink
End With
'In the production version, the following would be used to determine
'if user has the required role to disconnect, reconnect and apply updates.
If My.Computer.Network.IsAvailable Then
If My.Computer.Network.Ping("localhost", 250) Then
Dim spUser As System.Security.Principal.IIdentity = My.User.CurrentPrincipal.Identity
If spUser.IsAuthenticated Then
If My.User.IsInRole("DisconnectedUsers") Then
'User is connected (does nothing in this version)
chkDisconnected.Checked = False
Else
'User can apply online updates only
'chkDisconnected.Enabled = False
End If
End If
End If
End If
End Sub
Private Sub OrdersForm_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.DoubleClick
If Len(frmDetails.txtDetails.Text) > 0 Then
'Workaround for inoperable F2 KeyDown intercept
frmDetails.Show()
End If
End Sub
Private Sub LockTextBoxes(ByVal blnLock As Boolean, ByVal blnLockID As Boolean)
'Set text boxes read-only or read-write
CustomerIDTextBox.ReadOnly = blnLockID
CompanyNameTextBox.ReadOnly = blnLock
ContactNameTextBox.ReadOnly = blnLock
ContactTitleTextBox.ReadOnly = blnLock
AddressTextBox.ReadOnly = blnLock
CityTextBox.ReadOnly = blnLock
RegionTextBox.ReadOnly = blnLock
PostalCodeTextBox.ReadOnly = blnLock
CountryTextBox.ReadOnly = blnLock
PhoneTextBox.ReadOnly = blnLock
FaxTextBox.ReadOnly = blnLock
End Sub
Private Sub ShowCustomerEditButtons(ByVal blnVisible As Boolean)
EditCustomerDataToolStripButton.Visible = blnVisible
'NewCustomerToolStripButton.Visible = blnVisible
SaveChangesToolStripButton.Visible = blnVisible
CancelEditToolStripButton.Visible = blnVisible
End Sub
Private Sub EnableOrdersGrid(ByVal blnEnable As Boolean, ByVal blnReadOnly As Boolean)
'Enable or disable grids and set read-only or read-write
With OrdersDataGridView
.Enabled = blnEnable
.ReadOnly = blnReadOnly
.AllowUserToAddRows = Not blnReadOnly
.AllowUserToDeleteRows = Not blnReadOnly
End With
End Sub
Private Sub EnableOrder_DetailsGrid(ByVal blnEnable As Boolean, ByVal blnReadOnly As Boolean)
With Order_DetailsDataGridView
.Enabled = blnEnable
.ReadOnly = blnReadOnly
.AllowUserToAddRows = Not blnReadOnly
.AllowUserToDeleteRows = Not blnReadOnly
End With
End Sub
'*****************************************
'Customers ToolStrip button event handlers
'*****************************************
Private Sub GetCustomerOrdersToolStripButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles GetCustomerOrdersToolStripButton.Click
'HACK 14: GetCustomerOrdersToolStripButton_Click event handler
'If online, load the DataTables with parameterized SELECT queries
'If disconnected, apply filters to BindingSources
Me.Cursor = Cursors.WaitCursor
Try
If chkDisconnected.Checked Then
CustomersBindingSource.Filter = "CustomerID = '" + CustomerIDToolStripComboBox.Text + "'"
OrdersBindingSource.Filter = "CustomerID = '" + CustomerIDToolStripComboBox.Text + "'"
Else
If NorthwindDataSet.Customers.Count > 1 And NorthwindDataSet.HasChanges Then
Dim strMsg As String = "You have unprocessed offline updates " + _
"pending and have clicked Get Orders, which will terminate all " + _
"processing of all offline updates." + vbCrLf + vbCrLf + _
"Are you absolutely sure you want to do this?"
If MsgBox(strMsg, MsgBoxStyle.Question Or MsgBoxStyle.YesNo, _
"Unprocessed Offline Updates Pending") = MsgBoxResult.No Then
Exit Sub
End If
End If
'Reset filters
CustomersBindingSource.RemoveFilter()
OrdersBindingSource.RemoveFilter()
With NorthwindDataSet
.Clear()
CustomersTableAdapter.FillByCustomerID(.Customers, CustomerIDToolStripComboBox.Text)
OrdersTableAdapter.FillByCustomerID(.Orders, _
CustomerIDToolStripComboBox.Text)
Order_DetailsTableAdapter.FillByCustomerID(.Order_Details, _
CustomerIDToolStripComboBox.Text)
'Save the diffgram
.WriteXml(strDataSetFile, XmlWriteMode.DiffGram)
End With
End If
blnSkipDetailsDelete = False
LockTextBoxes(True, True)
EnableOrdersGrid(True, True)
EnableOrder_DetailsGrid(True, True)
ShowCustomerEditButtons(True)
SaveChangesToolStripButton.Visible = False
CancelEditToolStripButton.Visible = False
OrdersToolStrip.Visible = True
'Concurrency errors button setup
btnCustConcurError.Visible = True
btnCustConcurError.Enabled = True
btnOrdersConcurError.Visible = True
chkDeleteDetail.Visible = True
btnOrdersConcurError.Enabled = True
chkDeleteDetail.Enabled = True
btnDetailsConcurError.Visible = True
btnDetailsConcurError.Enabled = True
If Len(CustomerIDTextBox.Text) > 0 Then
CustomerIDTextBox.Text = UCase(CustomerIDTextBox.Text)
blnIsNewCustomer = False
End If
'Enable editing (for convenience)
EditOrdersToolStripButton.PerformClick()
Catch ex As System.Exception
Me.Cursor = Cursors.Default
System.Windows.Forms.MessageBox.Show(ex.Message)
Finally
Me.Cursor = Cursors.Default
End Try
End Sub
Private Sub EditCustomerDataToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditCustomerDataToolStripButton.Click
'Enable the Customers text boxes
LockTextBoxes(False, True)
OrdersToolStrip.Visible = True
ShowCustomerEditButtons(True)
SaveChangesToolStripButton.Enabled = True
End Sub
'**************************************************************
'Event handlers and procedures for adding a new customer record
'**************************************************************
Private Sub NewCustomerToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewCustomerToolStripButton.Click
'Add a new customer record
If chkDisconnected.Checked Then
'HACK 15: NewCustomerToolStripButton_Click event handler for disconnected operations
'Enable writing to the combo box text box
CustomerIDToolStripComboBox.DropDownStyle = ComboBoxStyle.DropDown
CustomerIDToolStripComboBox.Text = ""
'Set up the data connectors
CustomersBindingSource.RemoveFilter()
'An alternative
CustomersBindingSource.Filter = Nothing
'Show no Orders or Order Details records
OrdersBindingSource.Filter = "CustomerID = 'X'"
Else
With NorthwindDataSet
If .HasChanges Then
Dim strMsg As String = "You can't add a new customer until you've " + _
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?