以文本方式查看主题 - Foxtable(狐表) (http://foxtable.net/bbs/index.asp) -- 专家坐堂 (http://foxtable.net/bbs/list.asp?boardid=2) ---- [求助]自动获取控件的属性 (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=61074) |
-- 作者:ncefans -- 发布时间:2014/12/6 14:32:00 -- [求助]自动获取控件的属性 尝试设计一个自动单据打印的模版,设计模版时将窗口中的控件属性保存到Excel表格中. 选择不同模版时,从Excel表格中读取各控件的属性. Dim Book As New XLS.Book \'定义一个Excel工作簿 Dim Sheet As XLS.Sheet = Book.Sheets(0) \'引用工作簿的第一个工作表 Dim r As Integer = 0 Dim items As String() = {"Name","Font.Name","Font.Size","Font.Style","Left","Top","Width","Height"} For c As Integer = 0 To items.Length -1 Sheet(r,c).Value = items(c) Next r+=1 For Each conl As Winform.Control In e.Form.Controls Sheet(r, 0).Value = conl.Name \'想改成自动从上面的数组items读取各属性的名称,再读取对应的属性值. Sheet(r, 1).Value = conl.Font.Name Sheet(r, 2).Value = conl.Font.Size Sheet(r, 3).Value = conl.Font.Style Sheet(r, 4).Value = conl.Left Sheet(r, 5).Value = conl.Top Sheet(r, 6).Value = conl.Width Sheet(r, 7).Value = conl.Height r+=1 Next \'打开工作簿 Book.Save("d:\\test.xls") Dim Proc As New Process Proc.File = "d:\\test.xls" Proc.Start() 上面蓝色部份代码想改成自动从上面的数组items读取各属性的名称,再读取对应的属性值,不如怎么样实现? 比如类似: conl.GetProperty(items(0)) 谢谢! |
-- 作者:Bin -- 发布时间:2014/12/6 15:31:00 -- For Each conl As Winform.Control In e.Form.Controls For c As Integer = 0 To items.Length -1 Sheet(r,c).Value = items(c) Next r+=1 Next
|
-- 作者:ncefans -- 发布时间:2014/12/6 15:40:00 -- 请问是什么意思呢? 我是想获取控件的属性所对应的值,属性的名称前面的代码已经有了.
[此贴子已经被作者于2014-12-6 15:41:23编辑过]
|
-- 作者:ncefans -- 发布时间:2014/12/6 20:34:00 -- 顶下,好像没有办法吗? |
-- 作者:有点甜 -- 发布时间:2014/12/7 9:31:00 -- 用反射获取值,参考
Dim txt As WinForm.TextBox = Forms("窗口1").Controls("TextBox1") |
-- 作者:ncefans -- 发布时间:2014/12/7 12:06:00 -- 谢谢. 可以了. 现在就是关于字体的目前还不行,比如 Font.Name,Font.Size,Font.Bold 正在找资料. 如果直接用Font返回的没有样式(Bold,Regular,Underline等等)
|
-- 作者:有点甜 -- 发布时间:2014/12/7 12:18:00 -- 用动态编译即可
http://www.foxtable.com/help/topics/1487.htm
|
-- 作者:ncefans -- 发布时间:2014/12/7 12:41:00 -- 谢谢甜版主,好办法. 因为只有Font.Name,Font.Size,Font.Bold三个是比较特别的,就直接手写了. 代码改成如下: Dim Book As New XLS.Book \'定义一个Excel工作簿 Dim Sheet As XLS.Sheet = Book.Sheets(0) \'引用工作簿的第一个工作表 Sheet(0,0).Value = "Window Width" Sheet(0,1).Value = e.Form.BaseForm.Width Sheet(0,2).Value = "Window Height" Sheet(0,3).Value = e.Form.BaseForm.Height Dim r As Integer = 1 Dim items As String() = {"Name","Left","Top","Width","Height","Visible","Printable"} For c As Integer = 0 To items.Length -1 Sheet(r,c).Value = items(c) Next Sheet(r,items.Length).Value = "Font.Name" Sheet(r,items.Length+1).Value = "Font.Size" Sheet(r,items.Length+2).Value = "Font.Bold" r+=1 For Each conl As Winform.Control In e.Form.Controls For c As Integer = 0 To items.Length -1 Sheet(r,c).Value = conl.Gettype.GetProperty(items(c)).GetValue(conl, Nothing) Next Sheet(r,items.Length).Value = conl.Font.Name Sheet(r,items.Length+1).Value = conl.Font.Size Sheet(r,items.Length+2).Value = conl.Font.Bold r+=1 Next \'打开工作簿 Book.Save("d:\\test.xls") Dim Proc As New Process Proc.File = "d:\\test.xls" Proc.Start() [此贴子已经被作者于2014-12-7 12:41:12编辑过]
|