📄 form1.vb
字号:
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Public Class Form1
Inherits System.Windows.Forms.Form
Dim gdate As Date = Date.Today() '定义gdate变量是一个日期型
Dim n As Integer = 0 '定义n变量跟踪列表中的当前行
#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 ListView1 As System.Windows.Forms.ListView
Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents RadioButton1 As System.Windows.Forms.RadioButton
Friend WithEvents RadioButton2 As System.Windows.Forms.RadioButton
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents MonthCalendar1 As System.Windows.Forms.MonthCalendar
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents dataset11 As System.Data.DataSet
Friend WithEvents OleDbDataAdapter1 As System.Data.OleDb.OleDbDataAdapter
Friend WithEvents OleDbConnection1 As System.Data.OleDb.OleDbConnection
Friend WithEvents OleDbSelectCommand1 As System.Data.OleDb.OleDbCommand
Friend WithEvents OleDbInsertCommand1 As System.Data.OleDb.OleDbCommand
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.ListView1 = New System.Windows.Forms.ListView
Me.GroupBox1 = New System.Windows.Forms.GroupBox
Me.Label2 = New System.Windows.Forms.Label
Me.Button2 = New System.Windows.Forms.Button
Me.Label1 = New System.Windows.Forms.Label
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.RadioButton2 = New System.Windows.Forms.RadioButton
Me.RadioButton1 = New System.Windows.Forms.RadioButton
Me.Button1 = New System.Windows.Forms.Button
Me.MonthCalendar1 = New System.Windows.Forms.MonthCalendar
Me.dataset11 = New System.Data.DataSet
Me.OleDbDataAdapter1 = New System.Data.OleDb.OleDbDataAdapter
Me.OleDbInsertCommand1 = New System.Data.OleDb.OleDbCommand
Me.OleDbConnection1 = New System.Data.OleDb.OleDbConnection
Me.OleDbSelectCommand1 = New System.Data.OleDb.OleDbCommand
Me.GroupBox1.SuspendLayout()
CType(Me.dataset11, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'ListView1
'
Me.ListView1.Location = New System.Drawing.Point(19, 9)
Me.ListView1.Name = "ListView1"
Me.ListView1.Size = New System.Drawing.Size(394, 172)
Me.ListView1.TabIndex = 0
Me.ListView1.View = System.Windows.Forms.View.Details
'
'GroupBox1
'
Me.GroupBox1.Controls.Add(Me.Label2)
Me.GroupBox1.Controls.Add(Me.Button2)
Me.GroupBox1.Controls.Add(Me.Label1)
Me.GroupBox1.Controls.Add(Me.TextBox1)
Me.GroupBox1.Controls.Add(Me.RadioButton2)
Me.GroupBox1.Controls.Add(Me.RadioButton1)
Me.GroupBox1.Controls.Add(Me.Button1)
Me.GroupBox1.ForeColor = System.Drawing.SystemColors.ControlText
Me.GroupBox1.Location = New System.Drawing.Point(19, 207)
Me.GroupBox1.Name = "GroupBox1"
Me.GroupBox1.Size = New System.Drawing.Size(307, 172)
Me.GroupBox1.TabIndex = 1
Me.GroupBox1.TabStop = False
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(125, 17)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(134, 26)
Me.Label2.TabIndex = 6
Me.Label2.Text = "Label2"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(19, 17)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(87, 35)
Me.Button2.TabIndex = 5
Me.Button2.Text = "输入日期"
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(192, 52)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(67, 17)
Me.Label1.TabIndex = 4
Me.Label1.Text = "金额"
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(192, 78)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(96, 21)
Me.TextBox1.TabIndex = 3
Me.TextBox1.Text = ""
'
'RadioButton2
'
Me.RadioButton2.Location = New System.Drawing.Point(19, 121)
Me.RadioButton2.Name = "RadioButton2"
Me.RadioButton2.Size = New System.Drawing.Size(96, 25)
Me.RadioButton2.TabIndex = 2
Me.RadioButton2.Text = "支出"
'
'RadioButton1
'
Me.RadioButton1.Location = New System.Drawing.Point(19, 78)
Me.RadioButton1.Name = "RadioButton1"
Me.RadioButton1.Size = New System.Drawing.Size(96, 25)
Me.RadioButton1.TabIndex = 1
Me.RadioButton1.Text = "收入"
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(192, 121)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(96, 34)
Me.Button1.TabIndex = 0
Me.Button1.Text = "填入表格"
'
'MonthCalendar1
'
Me.MonthCalendar1.Location = New System.Drawing.Point(86, 43)
Me.MonthCalendar1.Name = "MonthCalendar1"
Me.MonthCalendar1.TabIndex = 6
'
'dataset11
'
Me.dataset11.DataSetName = "DataSet2"
Me.dataset11.Locale = New System.Globalization.CultureInfo("zh-CN")
Me.dataset11.Namespace = "http://www.tempuri.org/DataSet2.xsd"
'
'OleDbDataAdapter1
'
Me.OleDbDataAdapter1.InsertCommand = Me.OleDbInsertCommand1
Me.OleDbDataAdapter1.SelectCommand = Me.OleDbSelectCommand1
Me.OleDbDataAdapter1.TableMappings.AddRange(New System.Data.Common.DataTableMapping() {New System.Data.Common.DataTableMapping("Table", "mytable", New System.Data.Common.DataColumnMapping() {New System.Data.Common.DataColumnMapping("date", "date"), New System.Data.Common.DataColumnMapping("income", "income"), New System.Data.Common.DataColumnMapping("outgoing", "outgoing"), New System.Data.Common.DataColumnMapping("sum", "sum")})})
'
'OleDbInsertCommand1
'
Me.OleDbInsertCommand1.CommandText = "INSERT INTO mytable([date], income, outgoing, [sum]) VALUES (?, ?, ?, ?)"
Me.OleDbInsertCommand1.Connection = Me.OleDbConnection1
Me.OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("date", System.Data.OleDb.OleDbType.DBDate, 0, "date"))
Me.OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("income", System.Data.OleDb.OleDbType.Decimal, 0, System.Data.ParameterDirection.Input, False, CType(18, Byte), CType(0, Byte), "income", System.Data.DataRowVersion.Current, Nothing))
Me.OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("outgoing", System.Data.OleDb.OleDbType.Decimal, 0, System.Data.ParameterDirection.Input, False, CType(18, Byte), CType(0, Byte), "outgoing", System.Data.DataRowVersion.Current, Nothing))
Me.OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("sum", System.Data.OleDb.OleDbType.Decimal, 0, System.Data.ParameterDirection.Input, False, CType(18, Byte), CType(0, Byte), "sum", System.Data.DataRowVersion.Current, Nothing))
'
'OleDbConnection1
'
Me.OleDbConnection1.ConnectionString = "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database L" & _
"ocking Mode=1;Data Source=""C:\data.mdb"";Mode=Share Deny None;Jet OLEDB:Engine Ty" & _
"pe=5;Provider=""Microsoft.Jet.OLEDB.4.0"";Jet OLEDB:System database=;Jet OLEDB:SFP" & _
"=False;persist security info=False;Extended Properties=;Jet OLEDB:Compact Withou" & _
"t Replica Repair=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Create System " & _
"Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;User ID=Admin;Jet OL" & _
"EDB:Global Bulk Transactions=1"
'
'OleDbSelectCommand1
'
Me.OleDbSelectCommand1.CommandText = "SELECT [date], income, outgoing, [sum] FROM mytable"
Me.OleDbSelectCommand1.Connection = Me.OleDbConnection1
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(432, 384)
Me.Controls.Add(Me.MonthCalendar1)
Me.Controls.Add(Me.GroupBox1)
Me.Controls.Add(Me.ListView1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.GroupBox1.ResumeLayout(False)
CType(Me.dataset11, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
'初始化程序段
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
OleDbDataAdapter1.Fill(dataset11)
ListView1.Columns.Add("日期", 100, HorizontalAlignment.Center) '在ListView中添加日期,收入,支出,总计四个列名
ListView1.Columns.Add("收入", 50, HorizontalAlignment.Center)
ListView1.Columns.Add("支出", 50, HorizontalAlignment.Center)
ListView1.Columns.Add("总计", 100, HorizontalAlignment.Center)
MonthCalendar1.Visible = False '置MonthCalendar控件不可见
Label2.Text = "当前日期: " + gdate '初始化Label2的值,显示当前日期
show_data()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
MonthCalendar1.Visible = True '当输入日期按钮按下的时候,显示MonthCalendar控件
MonthCalendar1.BringToFront() '把MonthCalendar控件放在最前面
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sum As Double
Dim myrow As DataRow
Dim color As Color
Dim mycomm As OleDbCommand
Dim cmdstr As String
If TextBox1.Text <> "" And (RadioButton1.Checked = True Or RadioButton2.Checked = True) Then
ListView1.Items.Add(gdate)
If n <> 0 Then
sum = Val(ListView1.Items(n - 1).SubItems(3).Text)
Else
sum = 0
End If
myrow = DataSet11.Tables("mytable").NewRow
myrow("date") = gdate
If RadioButton1.Checked = True Then
ListView1.Items(n).SubItems.Add(TextBox1.Text)
ListView1.Items(n).SubItems.Add("")
myrow("income") = Val(TextBox1.Text)
myrow("outgoing") = 0
sum = sum + Val(TextBox1.Text)
ListView1.Items(n).SubItems.Add(sum)
cmdstr = "INSERT INTO mytable VALUES(" + gdate.Date.ToShortDateString + "," + TextBox1.Text + ",0 ," + Str(sum) + ")"
If sum >= 0 Then
ListView1.Items(n).ForeColor() = color.Blue
Else
ListView1.Items(n).ForeColor() = color.Red
End If
Else
ListView1.Items(n).SubItems.Add("")
ListView1.Items(n).SubItems.Add(TextBox1.Text)
myrow("outgoing") = Val(TextBox1.Text)
myrow("income") = 0
sum = sum - Val(TextBox1.Text)
ListView1.Items(n).SubItems.Add(sum)
cmdstr = "INSERT INTO mytable VALUES(" + gdate.Date.ToShortDateString + ",0 ," + TextBox1.Text + "," + Str(sum) + ")"
If sum >= 0 Then
ListView1.Items(n).ForeColor() = color.Blue
Else
ListView1.Items(n).ForeColor() = color.Red
End If
End If
n = n + 1
myrow("sum") = sum
DataSet11.Tables("mytable").Rows.Add(myrow)
DataSet11.AcceptChanges()
mycomm = New OleDbCommand(cmdstr)
OleDbConnection1.Open()
mycomm.Connection = OleDbConnection1
mycomm.ExecuteNonQuery()
OleDbConnection1.Close()
Else
MessageBox.Show("请选择支出还是收入!")
End If
End Sub
Private Sub MonthCalendar1_DateSelected(ByVal sender As Object, ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles MonthCalendar1.DateSelected
gdate = MonthCalendar1.SelectionRange.Start()
MonthCalendar1.Visible = False
Label2.Text = "当前日期: " + gdate
End Sub
Private Function show_data()
Dim i, j As Integer
Dim record As String
For i = 0 To DataSet11.Tables("mytable").Rows.Count - 1
record = DataSet11.Tables("mytable").Rows(i).Item(0)
ListView1.Items.Add(record)
For j = 1 To 3
record = DataSet11.Tables("mytable").Rows(i).Item(j)
ListView1.Items(i).SubItems.Add(record)
If j = 3 And Val(record) > 0 Then
ListView1.Items(i).ForeColor() = Color.Blue
Else
ListView1.Items(i).ForeColor() = Color.Red
End If
Next
Next
n = DataSet11.Tables("mytable").Rows.Count
End Function
Private Sub OleDbDataAdapter1_RowUpdated(ByVal sender As System.Object, ByVal e As System.Data.OleDb.OleDbRowUpdatedEventArgs)
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -