1.反射添加某Form到另一个Form
(1)新建工程,类型ClassLibrary(名为ClassLibrary1),在工程中添加Item,类型WindowsForm(名为Form1)。
(2)任意编辑Form1,保存,编译工程,找到DLL文件,存放在某位置(C:\Documents and Settings\l\My Documents\Visual Studio 2008\Projects\WindowsApplication2\ClassLibrary1\bin\Debug\ClassLibrary1.dll)
(3)主界面上Button1作用:将Form1添加到主界面。代码:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myas As Assembly
myas = Assembly.LoadFrom("C:\Documents and Settings\l\My Documents\Visual Studio 2008\Projects\WindowsApplication2\ClassLibrary1\bin\Debug\ClassLibrary1.dll") ’反射路径,自行修改或存在XML中。
Dim t As Type = myas.GetType("ClassLibrary1.Form1")
Dim obj As Object = Activator.CreateInstance(t)
Dim frm As Form = CType(obj, Form)
frm.TopLevel = False
Me.Panel1.Controls.Add(frm) ’主界面上的Panel
frm.FormBorderStyle = Windows.Forms.FormBorderStyle.None ’去掉Form四周边框,否则太丑,还占地方。。。
frm.Height = 100
frm.Width = 500
frm.Left = 0
frm.Top = (Me.Panel1.Controls.Count - 1) * 100
frm.Show()
End Sub
(4)Button2作用:从后向前删除1个Form1
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
Me.Panel1.Controls.RemoveAt(Me.Panel1.Controls.Count - 1)
Catch ex As Exception
End Try
End Sub
(5)Button3作用:Form1上有三个TextBox(TextBox1,TextBox2,TextBox3),点击后分别计算各个Form1中的TextBox1,TextBox2,TextBox3的总和。
For i = 0 To Me.Panel1.Controls.Count - 1
For j = 0 To Me.Panel1.Controls(i).Controls.Count - 1
If Me.Panel1.Controls(i).Controls(j).Name = "TextBox1" Then
If Me.Panel1.Controls(i).Controls(j).Text <> String.Empty Then
total1 = total1 + Integer.Parse(Me.Panel1.Controls(i).Controls(j).Text)
End If
ElseIf Me.Panel1.Controls(i).Controls(j).Name = "TextBox2" Then
If Me.Panel1.Controls(i).Controls(j).Text <> String.Empty Then
total2 = total2 + Integer.Parse(Me.Panel1.Controls(i).Controls(j).Text)
End If
ElseIf Me.Panel1.Controls(i).Controls(j).Name = "TextBox3" Then
If Me.Panel1.Controls(i).Controls(j).Text <> String.Empty Then
total3 = total3 + Integer.Parse(Me.Panel1.Controls(i).Controls(j).Text)
End If
End If
Next
Next
2.向Excel中写值
(代码最上方加上Imports Excel = Microsoft.Office.Interop.Excel)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.SaveXls()
System.GC.Collect()
End Sub
Private Sub SaveXls()
Dim saveDialog As New SaveFileDialog ’保存Excel路径的Dialog
saveDialog.FileName = ComboBox1.Text + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls"
saveDialog.Filter = "xls | *.xls"
If saveDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim xlapp As Excel.Application
Dim xlwkbook As Excel.Workbook
Dim xlwksheet As Excel.Worksheet
xlapp = New Excel.Application
xlwkbook = xlapp.Workbooks.Open("D:\abc.xls") ’路径地址自行修改或写在XML中自行读取
xlwksheet = xlwkbook.Worksheets(5) ’表示表单编号(sheet1,sheet2之类的),编号从1开始
xlwksheet.Cells(6, 2).Value = nameTextBox.Text ’给第六行,第二列的格赋值(Excel行和列都是1开始)
xlwksheet.Cells(11, 4).Value = workNameTextBox.Text
xlwksheet.Cells(13, 4).Value = workPlaceTextBox.Text
xlwksheet.Cells(15, 3).Value = priceTextBox.Text
xlwksheet.Cells(26, 5).Value = companyTextBox.Text
xlwkbook.SaveAs(saveDialog.FileName)
xlwkbook.Close()
xlapp.Quit()
xlapp = Nothing
xlwkbook = Nothing
xlwksheet = Nothing
End If
End Sub
在几个文本框中输入值后将值写入对应的Excel的对应Sheet的对应位置。然后保存。将保存方法和垃圾回收分开写可以杀死残留的Excel.exe进程(垃圾回收写在方法中会残留一个进程)。
明天继续~