zoukankan      html  css  js  c++  java
  • VS.NET Addin在Design time获取控件值

    客户要写一个Visual Studio .NET的Add-in,需要在design time获取form上控件的值,以下是用Reflection的做法

         

           Dim win As Window = applicationObject.ActiveWindow
                
    Dim d As ComponentModel.Design.IDesignerHost = win.Object
                iss 
    = d.GetService(GetType(ComponentModel.Design.ISelectionService))
                
    Dim c As ComponentModel.Component = iss.PrimarySelection
                
    Dim pi As Reflection.PropertyInfo = CObj(c).GetType().GetProperty("Visible")
                
                
    Dim val As Object = pi.GetValue(c, Nothing)
                
    Dim tc As System.ComponentModel.TypeConverter = ComponentModel.TypeDescriptor.GetConverter(val)
                
    MsgBox(c.Site.Name & "." & pi.Name & " = " & tc.ConvertToString(val))


    这样做有一个问题,当我要取TextBox.Visible这类属性的时候,返回值永远为true,因为reflect的是designer中的textbox对象,它的visible值永远为true。正确的做法是通过PropertyDescriptorCollection来读取Properties window里面的值,代码如下:

      
       
               
    'query Properties
                Dim properties As System.ComponentModel.PropertyDescriptorCollection
                properties 
    = System.ComponentModel.TypeDescriptor.GetProperties(c)
                
    Dim prop As System.ComponentModel.PropertyDescriptor
                prop 
    = properties("Visible")
                
    MsgBox(c.Site.Name & "." & prop.Name & " = " & prop.GetValue(c))

    本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利
  • 相关阅读:
    HDU 5115 Dire Wolf (区间DP)
    HDU 4283 You Are the One(区间DP(最优出栈顺序))
    ZOJ 3469 Food Delivery(区间DP好题)
    LightOJ 1422 Halloween Costumes(区间DP)
    POJ 1651 Multiplication Puzzle(区间DP)
    NYOJ 石子合并(一)(区间DP)
    POJ 2955 Brackets(括号匹配一)
    POJ 1141 Brackets Sequence(括号匹配二)
    ZOJ 3537 Cake(凸包+区间DP)
    Graham求凸包模板
  • 原文地址:https://www.cnblogs.com/roger/p/132525.html
Copyright © 2011-2022 走看看