代码之家  ›  专栏  ›  技术社区  ›  MunkiPhD

图形用户界面到业务对象的映射VB.Net

  •  0
  • MunkiPhD  · 技术社区  · 17 年前

    我目前遇到的问题是将多个GUI字段映射到对象属性(即表示层到业务逻辑层的映射)。更具体地说,这是在VB.Net 2.0 WinForms中。

    解决方案的性质要求我们在GUI上有4列显示相同类型的行为——每列由11个文本框组成(我们只使用这个小样本,因为问题超出了11个文本盒)。

    Textbox1.tag = "name"
    Textbox2.tag = "type"
    Textbox3.tag = "speed"
    

    dim objectToMapTo //the generic parent object which all my custom myObjects inherit from
    
    select case sender.parent.tag //the parent object that the property needs to map to
        case "column1"
             objectToMapTo = myObject1
        case "column2"
             objectToMapTo = myObject2
        case "column3"
             objectToMapTo = myObject3
        case "column4"
             objectToMapTo = myObject4
    end select
    
    select case sender.tag //the actual textbox's tag value which maps to the property
        case "name"
             objectToMapTo.Name = sender.text //sender.text is conceptual for 
            //the data that needs to be set -- i.e. this could be a calculated 
            //number based on the text, or simply a string, etc
        case "type"
             objectToMapTo.Type = sender.text
        case "speed"
             objectToMapTo.Speed = sender.text
        ...
    end select
    

    谢谢你的帮助。

    1 回复  |  直到 17 年前
        1
  •  1
  •   chrissie1    17 年前

    通过将标签设置为对象,可以解决第一个问题。因为标签不是字符串,而是对象类型。

    您通过反射解决的第二个问题是,标记中的值必须与属性名完全匹配。

    _objectToMapTo.GetType().InvokeMember(sender.tag,BindingFlags.Instance Or BindingFlags.Public,Nothing, _objectToMapTo, New Object() {sender.text})