form1.vb
来自「vb 应用实例 简单应用 dddddddddddddddddddddddddd」· VB 代码 · 共 285 行
VB
285 行
'加入参考 (COM)
'Microsoft Office 10.0 Object Library
'Microsoft Word 10.0 Object Library
'Microsoft Excel 10.0 Object Library
'Microsoft Visual Basic for Applications Extensibility 5.3
'Microsoft Agent Server 2.0
'Microsoft Agent Control 2.0
'OLE Automation
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form 设计工具产生的程序代码 "
Public Sub New()
MyBase.New()
'此呼叫为 Windows Form 设计工具的必要项。
InitializeComponent()
'在 InitializeComponent() 呼叫之后加入所有的初始设定
End Sub
'Form 覆写 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 Form 设计工具的必要项
Private components As System.ComponentModel.IContainer
'注意: 以下为 Windows Form 设计工具所需的程序
'您可以使用 Windows Form 设计工具进行修改。
'请勿使用程序代码编辑器来修改这些程序。
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents CheckBox1 As System.Windows.Forms.CheckBox
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
Friend WithEvents Button3 As System.Windows.Forms.Button
Friend WithEvents Button4 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.ComboBox1 = New System.Windows.Forms.ComboBox
Me.Label1 = New System.Windows.Forms.Label
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.CheckBox1 = New System.Windows.Forms.CheckBox
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.Button2 = New System.Windows.Forms.Button
Me.DataGrid1 = New System.Windows.Forms.DataGrid
Me.Button3 = New System.Windows.Forms.Button
Me.Button4 = New System.Windows.Forms.Button
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(326, 45)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(106, 22)
Me.Button1.TabIndex = 1
Me.Button1.Text = "小帮手说"
'
'ComboBox1
'
Me.ComboBox1.Location = New System.Drawing.Point(115, 15)
Me.ComboBox1.Name = "ComboBox1"
Me.ComboBox1.Size = New System.Drawing.Size(192, 20)
Me.ComboBox1.TabIndex = 2
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(29, 15)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(66, 17)
Me.Label1.TabIndex = 3
Me.Label1.Text = "小帮手动作"
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(29, 45)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(278, 21)
Me.TextBox1.TabIndex = 4
Me.TextBox1.Text = "活在当下"
'
'CheckBox1
'
Me.CheckBox1.Location = New System.Drawing.Point(326, 15)
Me.CheckBox1.Name = "CheckBox1"
Me.CheckBox1.Size = New System.Drawing.Size(68, 22)
Me.CheckBox1.TabIndex = 5
Me.CheckBox1.Text = "再见"
'
'TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(29, 82)
Me.TextBox2.Multiline = True
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(278, 37)
Me.TextBox2.TabIndex = 6
Me.TextBox2.Text = "I am nothing, and everything. I am nowwhere, and everywhere."
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(326, 82)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(106, 23)
Me.Button2.TabIndex = 7
Me.Button2.Text = "Word拼字检查"
'
'DataGrid1
'
Me.DataGrid1.DataMember = ""
Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.DataGrid1.Location = New System.Drawing.Point(29, 134)
Me.DataGrid1.Name = "DataGrid1"
Me.DataGrid1.Size = New System.Drawing.Size(278, 105)
Me.DataGrid1.TabIndex = 8
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(326, 134)
Me.Button3.Name = "Button3"
Me.Button3.Size = New System.Drawing.Size(106, 23)
Me.Button3.TabIndex = 9
Me.Button3.Text = "载入"
'
'Button4
'
Me.Button4.Location = New System.Drawing.Point(326, 172)
Me.Button4.Name = "Button4"
Me.Button4.Size = New System.Drawing.Size(106, 22)
Me.Button4.TabIndex = 10
Me.Button4.Text = "导出 -> Excel"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(441, 259)
Me.Controls.Add(Me.Button4)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.DataGrid1)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.TextBox2)
Me.Controls.Add(Me.CheckBox1)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.ComboBox1)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "与Office整合"
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Private agentController1 As AgentObjects.Agent
Private agentCharacter1 As AgentObjects.IAgentCtlCharacter
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Agent object 联机到 Agent server, 以载入 character 位于 \Windows\msagent\chars\merlin.acs
agentController1 = New AgentObjects.Agent()
agentController1.Connected = True
agentController1.Characters.Load("merlin", "merlin.acs")
agentCharacter1 = agentController1.Characters("merlin")
'使用 MoveTo 将小帮手移动到左上角
With agentCharacter1
.MoveTo(CShort(Me.Location.X + 420), CShort(Me.Location.Y + 130))
.Show()
.Play("Announce")
.Speak("我是小帮手!")
.Play("GestureRight")
.Speak("活在当下,\pau=300\or\pau=500\...")
.MoveTo(CShort(Me.Location.X + 340), CShort(Me.Location.Y + 250))
.Play("GestureRight")
.Speak("试试其它的动作.")
End With
'可选的动作
Dim MyArray() As String = {"GetAttention", "GetAttentionReturn", "Explain", "Congratulate", "Announce", "Congratulate_2", "DontRecognize", "Write", "Surprised", "Suggest", "Wave"}
ComboBox1.DataSource = MyArray
Button4.Enabled = False
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
agentCharacter1.StopAll()
agentCharacter1.Play(ComboBox1.Text)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
agentCharacter1.Speak(TextBox1.Text)
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked Then
agentCharacter1.StopAll()
agentCharacter1.Hide()
Else
agentCharacter1.Show()
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'拼字检查
Dim wordApp1 As New Word.Application()
If wordApp1.CheckSpelling(TextBox2.Text) Then
MsgBox("拼字正确.")
Else
MsgBox("拼字不正确 !!!")
End If
End Sub
Private ds As DataSet
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'由 XML 加载数据 -> DataSet
ds = New DataSet()
ds.ReadXml("..\Customers.xml")
'DataSet -> DataGrid
DataGrid1.CaptionText = "客户资料"
DataGrid1.DataSource = ds.Tables(0)
Button4.Enabled = True
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
'汇出 -> Excel
Dim excelApp1 As New Excel.Application()
Dim excelBook1 As Excel.Workbook = excelApp1.Workbooks.Add
Dim excelWorksheet1 As Excel.Worksheet = CType(excelBook1.Worksheets(1), Excel.Worksheet)
excelApp1.Visible = True
With excelWorksheet1
.Columns().ColumnWidth = 15
.Range("A1").Value = "CustomerID"
.Range("A1").Font.Bold = True
.Range("B1").Value = "CompanyName"
.Range("B1").Font.Bold = True
.Range("C1").Value = "ContactName"
.Range("C1").Font.Bold = True
.Range("D1").Value = "ContactTitle"
.Range("D1").Font.Bold = True
.Range("E1").Value = "City"
.Range("E1").Font.Bold = True
Dim i As Integer = 2
Dim dr As DataRow
For Each dr In ds.Tables(0).Rows
.Range("A" & i.ToString).Value = dr("CustomerID")
.Range("B" & i.ToString).Value = dr("CompanyName")
.Range("C" & i.ToString).Value = dr("ContactName")
.Range("D" & i.ToString).Value = dr("ContactTitle")
.Range("E" & i.ToString).Value = dr("City")
i += 1
Next
End With
End Sub
End Class
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?