📄 frmmain.vb
字号:
Public Class frmMain
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents btnLoad As System.Windows.Forms.Button
Friend WithEvents btnShowDetail As System.Windows.Forms.Button
Friend WithEvents dgPupils As System.Windows.Forms.DataGrid
Friend WithEvents btnNew As System.Windows.Forms.Button
Friend WithEvents btnDelete As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.btnLoad = New System.Windows.Forms.Button
Me.btnShowDetail = New System.Windows.Forms.Button
Me.dgPupils = New System.Windows.Forms.DataGrid
Me.btnNew = New System.Windows.Forms.Button
Me.btnDelete = New System.Windows.Forms.Button
CType(Me.dgPupils, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'btnLoad
'
Me.btnLoad.Location = New System.Drawing.Point(296, 16)
Me.btnLoad.Name = "btnLoad"
Me.btnLoad.Size = New System.Drawing.Size(96, 32)
Me.btnLoad.TabIndex = 1
Me.btnLoad.Text = "Load"
'
'btnShowDetail
'
Me.btnShowDetail.Location = New System.Drawing.Point(296, 60)
Me.btnShowDetail.Name = "btnShowDetail"
Me.btnShowDetail.Size = New System.Drawing.Size(96, 32)
Me.btnShowDetail.TabIndex = 2
Me.btnShowDetail.Text = "Show Detail"
'
'dgPupils
'
Me.dgPupils.DataMember = ""
Me.dgPupils.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.dgPupils.Location = New System.Drawing.Point(8, 8)
Me.dgPupils.Name = "dgPupils"
Me.dgPupils.Size = New System.Drawing.Size(280, 304)
Me.dgPupils.TabIndex = 3
'
'btnNew
'
Me.btnNew.Location = New System.Drawing.Point(296, 104)
Me.btnNew.Name = "btnNew"
Me.btnNew.Size = New System.Drawing.Size(96, 32)
Me.btnNew.TabIndex = 4
Me.btnNew.Text = "New"
'
'btnDelete
'
Me.btnDelete.Location = New System.Drawing.Point(296, 148)
Me.btnDelete.Name = "btnDelete"
Me.btnDelete.Size = New System.Drawing.Size(96, 32)
Me.btnDelete.TabIndex = 5
Me.btnDelete.Text = "Delete"
'
'frmMain
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(404, 326)
Me.Controls.Add(Me.btnDelete)
Me.Controls.Add(Me.btnNew)
Me.Controls.Add(Me.dgPupils)
Me.Controls.Add(Me.btnShowDetail)
Me.Controls.Add(Me.btnLoad)
Me.Name = "frmMain"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "PCW School App"
CType(Me.dgPupils, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Private dsPupils As DataSet
Friend Sub LoadData()
dsPupils = SchoolData.SchoolData.DataModule.GetPupils
Me.dgPupils.DataSource = dsPupils
Me.dgPupils.DataMember = "pupils"
Me.dgPupils.ReadOnly = True
'design the grid
Dim ts1 As New DataGridTableStyle
ts1.MappingName = "pupils"
' Set other properties.
ts1.AlternatingBackColor = Color.LightGray
' Add a GridColumnStyle and set its MappingName
Dim idcol As New DataGridTextBoxColumn
idcol.MappingName = "id"
idcol.HeaderText = "ID"
idcol.Width = 30
ts1.GridColumnStyles.Add(idcol)
' Add a second column style.
Dim TextCol As New DataGridTextBoxColumn
TextCol.MappingName = "firstname"
TextCol.HeaderText = "First Name"
TextCol.Width = 100
ts1.GridColumnStyles.Add(TextCol)
Dim namecol As New DataGridTextBoxColumn
namecol.MappingName = "LastName"
namecol.HeaderText = "Last Name"
namecol.Width = 100
ts1.GridColumnStyles.Add(namecol)
'clear any existing style
dgPupils.TableStyles.Clear()
' Add the DataGridTableStyle instances to
' the GridTableStylesCollection.
dgPupils.TableStyles.Add(ts1)
End Sub
Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
Me.LoadData()
End Sub
Private Sub btnShowDetail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowDetail.Click
Dim cm As CurrencyManager
If IsNothing(dsPupils) Then Exit Sub
cm = CType(BindingContext(dsPupils, "pupils"), CurrencyManager)
'get the ID
Dim id As Integer
Dim dr As DataRowView = CType(cm.Current, DataRowView)
id = CType(dr.Item("ID"), Integer)
'get the selected pupil
Dim dsPupil As DataSet = SchoolData.SchoolData.DataModule.GetPupil(id)
'show the form
Dim frm As New frmPupil
frm.MainForm = Me ' set ref. to this form, enables us to call LoadData easily
frm.LoadData(dsPupil)
frm.Show()
End Sub
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
MyBase.OnLoad(e)
Dim sPath As String = Application.ExecutablePath
sPath = System.IO.Path.GetDirectoryName(sPath)
If sPath.EndsWith("\bin") Then
sPath = sPath.Substring(0, Len(sPath) - 4)
End If
SchoolData.SchoolData.DataModule = New SchoolData.SchoolData(sPath)
End Sub
Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
'get the selected pupil
Dim dsPupil As DataSet = SchoolData.SchoolData.DataModule.GetNewPupil
'show the form
Dim frm As New frmPupil
frm.MainForm = Me ' set ref. to this form, enables us to call LoadData easily
frm.LoadData(dsPupil)
frm.Show()
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
Dim cm As CurrencyManager
If IsNothing(dsPupils) Then Exit Sub
cm = CType(BindingContext(dsPupils, "pupils"), CurrencyManager)
'get the current row
Dim dr As DataRow = CType(cm.Current, DataRowView).Row
'prompt to save changes
If MsgBox("OK to delete pupil: " + GetNameFromDataRow(dr) + "?", MsgBoxStyle.OKCancel, "Hands On Programming") = MsgBoxResult.OK Then
'delete it from the dataset
dr.Delete()
'get just the changed data
Dim dsChanged As DataSet = dsPupils.GetChanges
SchoolData.SchoolData.DataModule.SavePupils(dsPupils)
Else
'reset the dataset
dsPupils.RejectChanges()
End If
End Sub
Private Function GetNameFromDataRow(ByVal dr As DataRow) As String
'purpose: Get name of pupil from datarow, allowing for nulls
Dim result As String
If Not dr.IsNull("firstname") Then
result = CType(dr.Item("firstname"), String) + " "
End If
If Not dr.IsNull("lastname") Then
result += CType(dr.Item("lastname"), String)
End If
Return result
End Function
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -