form1.vb
来自「Visual Basic .NET程序设计教程源代码」· VB 代码 · 共 137 行
VB
137 行
Imports System.Threading
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写 dispose 以清理组件列表。
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 Button1 As System.Windows.Forms.Button
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents TextBox3 As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.TextBox3 = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(24, 96)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 0
Me.Button1.Text = "调用线程"
'
'Label1
'
Me.Label1.Font = New System.Drawing.Font("宋体", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
Me.Label1.Location = New System.Drawing.Point(16, 16)
Me.Label1.Name = "Label1"
Me.Label1.TabIndex = 1
Me.Label1.Text = "输入求和的数值"
'
'Label2
'
Me.Label2.Font = New System.Drawing.Font("宋体", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
Me.Label2.Location = New System.Drawing.Point(32, 136)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(72, 23)
Me.Label2.TabIndex = 2
Me.Label2.Text = "计算结果"
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(136, 16)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.TabIndex = 3
Me.TextBox1.Text = ""
'
'TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(136, 56)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.TabIndex = 4
Me.TextBox2.Text = ""
'
'TextBox3
'
Me.TextBox3.Location = New System.Drawing.Point(136, 136)
Me.TextBox3.Name = "TextBox3"
Me.TextBox3.TabIndex = 5
Me.TextBox3.Text = ""
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(292, 173)
Me.Controls.Add(Me.TextBox3)
Me.Controls.Add(Me.TextBox2)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Function sum(ByVal a1 As Single, ByVal a2 As Single) As Single
Return a1 + a2
End Function
Public Class SumClass
Public a1 As Single
Public a2 As Single
Public sum As Single
Public Event ThreadComplete(ByVal sum As Single)
Public Sub CalcSum()
sum = a1 + a2
RaiseEvent ThreadComplete(sum)
End Sub
End Class
Dim t As Thread
Dim WithEvents mysum As SumClass
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
mysum = New SumClass
t = New Thread(AddressOf mysum.CalcSum)
mysum.a1 = TextBox1.Text
mysum.a2 = TextBox2.Text
t.Start()
End Sub
Sub SumEventHandler(ByVal sum As Single) Handles mysum.ThreadComplete
TextBox3.Text = sum
End Sub
End Class
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?