📄 form1.vb
字号:
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写处置以清理组件列表。
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
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意:以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents txtOperand1 As System.Windows.Forms.TextBox
Friend WithEvents btnCalculate As System.Windows.Forms.Button
Friend WithEvents chkSave As System.Windows.Forms.CheckBox
Friend WithEvents lstResults As System.Windows.Forms.ListBox
Friend WithEvents lblResult As System.Windows.Forms.Label
Friend WithEvents cboOperator As System.Windows.Forms.ComboBox
Friend WithEvents numupdOperand2 As System.Windows.Forms.NumericUpDown
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.txtOperand1 = New System.Windows.Forms.TextBox()
Me.cboOperator = New System.Windows.Forms.ComboBox()
Me.lstResults = New System.Windows.Forms.ListBox()
Me.numupdOperand2 = New System.Windows.Forms.NumericUpDown()
Me.btnCalculate = New System.Windows.Forms.Button()
Me.chkSave = New System.Windows.Forms.CheckBox()
Me.lblResult = New System.Windows.Forms.Label()
CType(Me.numupdOperand2, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'txtOperand1
'
Me.txtOperand1.Location = New System.Drawing.Point(8, 16)
Me.txtOperand1.Name = "txtOperand1"
Me.txtOperand1.Size = New System.Drawing.Size(96, 21)
Me.txtOperand1.TabIndex = 0
Me.txtOperand1.Text = ""
'
'cboOperator
'
Me.cboOperator.Items.AddRange(New Object() {"+", "-", "*", "/"})
Me.cboOperator.Location = New System.Drawing.Point(112, 16)
Me.cboOperator.Name = "cboOperator"
Me.cboOperator.Size = New System.Drawing.Size(80, 20)
Me.cboOperator.TabIndex = 1
'
'lstResults
'
Me.lstResults.ItemHeight = 12
Me.lstResults.Location = New System.Drawing.Point(200, 160)
Me.lstResults.Name = "lstResults"
Me.lstResults.Size = New System.Drawing.Size(112, 88)
Me.lstResults.TabIndex = 2
'
'numupdOperand2
'
Me.numupdOperand2.Location = New System.Drawing.Point(200, 16)
Me.numupdOperand2.Name = "numupdOperand2"
Me.numupdOperand2.Size = New System.Drawing.Size(112, 21)
Me.numupdOperand2.TabIndex = 3
'
'btnCalculate
'
Me.btnCalculate.Location = New System.Drawing.Point(224, 64)
Me.btnCalculate.Name = "btnCalculate"
Me.btnCalculate.Size = New System.Drawing.Size(88, 24)
Me.btnCalculate.TabIndex = 4
Me.btnCalculate.Text = "计算"
'
'chkSave
'
Me.chkSave.Location = New System.Drawing.Point(136, 64)
Me.chkSave.Name = "chkSave"
Me.chkSave.Size = New System.Drawing.Size(80, 24)
Me.chkSave.TabIndex = 5
Me.chkSave.Text = "保存结果"
'
'lblResult
'
Me.lblResult.Location = New System.Drawing.Point(208, 112)
Me.lblResult.Name = "lblResult"
Me.lblResult.Size = New System.Drawing.Size(96, 24)
Me.lblResult.TabIndex = 6
Me.lblResult.Text = "计算结果"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(320, 254)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblResult, Me.chkSave, Me.btnCalculate, Me.numupdOperand2, Me.lstResults, Me.cboOperator, Me.txtOperand1})
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.numupdOperand2, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
Dim iOperand1 As Integer
Dim iOperand2 As Integer
Dim iResult As Integer
Dim cOperator As Char
Try
iOperand1 = CInt(txtOperand1.Text) '取得第一个运算数的值
iOperand2 = numupdOperand2.Value '取得第二个运算数的值
cOperator = cboOperator.Text '取得运算符
Select Case cOperator '进行运算
Case "+"
iResult = iOperand1 + iOperand2
Case "-"
iResult = iOperand1 - iOperand2
Case "*"
iResult = iOperand1 * iOperand2
Case "/"
iResult = iOperand1 / iOperand2
Case Else
MessageBox.Show("请选择运算符!")
Exit Sub
End Select
lblResult.Text = CStr(iResult)
If chkSave.Checked Then lstResults.Items.Add(CStr(iResult))
Catch ex As OverflowException
lblResult.Text = "运算发生溢出!"
Catch ex As InvalidCastException
lblResult.Text = "运算参数无效!"
Catch ex As Exception
lblResult.Text = ex.Message
End Try
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -